在不同的剃须刀页面之间传递和验证数据

Pass and verify data between different razor pages

我正在使用 Asp.net Core2.1 Razor 页面技术。

我想问一下在剃须刀页面之间传递和验证参数的问题。 请注意,我问的是概念而不是编码:

现在假设我有博客,每个博客都由一个可以管理它的用户拥有。

用户使用这个进入管理页面 url :

https://localhost:44368/blogs/1/settings

如您所见,博客的 id 在 url:

public async Task<IActionResult> OnGetAsync(int? id)
{
        // here i check that the blog is exist by the id
        // and i check if the current user own the blog            
}

然后在设置页面中我有几个页面的链接,例如(文章) 并且用户可以管理这些文章。

https://localhost:44368/blogs/1/settings/articles

如你所见,我在 url 中仍然有博客 ID :

public async Task<IActionResult> OnGetAsync(int? id)
{
    // now this function in the articles page
    // again i check if the blog is exist
    // and again i check if the current user can manage the blog or not                             
}

这是正确的好做法吗?在每一页检查和验证

还是只有在进入设置页面时才检查?

或者我应该考虑在用户进入设置页面时只检查一次的方法,然后用户不能根据第一次检查进入其他页面!

保留 Web 端点 stateless 是一种很好的做法。

因此,您将 Id 传递给每个子操作并验证此输入的方法是正确的。

要实现仅检查一次 Id 的另一种方法,您需要在操作之间传递状态,例如作为会话状态。这种方法不太灵活。也许您希望将来某天能够从博客详细信息以外的其他页面打开设置?

还要记住,仅仅因为用户没有在页面上看到某个 link,就没有什么能阻止她进入,例如https://localhost:44368/blogs/1/settings/articles 直接进入浏览器的地址栏。因此,在任何情况下,您都需要对每个操作进行一些验证。