gpt4 book ai didi

c# - _start.cshtml View 未执行

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:03 26 4
gpt4 key购买 nike

我在我的应用程序中使用 Asp.Net MVC-4。我创建了一个 Controller ,例如 Person,在这个 Controller 中我有 3 个 Actions,例如 GetNameGetAgeGetDateOfBirth 。现在我想要的是进行检查,以便未登录的用户无法访问这些操作 View 。

为此,我正在尝试使用 _start.cshtml View 。我将其放置在 Views/Person/ 中,我期望任何用户从 Person 文件夹中访问任何 View 而不是 _start.cshtml View 应该在任何其他 View 之前运行。在 *_start.cshtml* 中,我放置了一个代码来检查当前用户是否登录。但是 _star.cshtml 没有执行。

谁能告诉我我做错了什么?

最佳答案

Now what i want is to place a check so that the user who are not loggedin can't get access to these action view

这绝对不是应该在 View 中完成的事情,而是应该在 Controller 操作上完成的,通过用 [Authorize] 装饰它们。属性。例如:

[Authorize]
public ActionResult SomeAction()
{
...
}

并且您还可以指定一个或多个角色:

[Authorize(Roles = "Admin")]
public ActionResult SomeAction()
{
...
}

您还可以使用此属性装饰 Controller ,这意味着其中的所有操作都需要授权。

关于c# - _start.cshtml View 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16081714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com