我有一个动作,我想只限制角色“Admin”.我是这样做的:
[Authorize(Roles = "Admin")] public ActionResult Edit(int id)
手动进入Controller / Edit / 1路径后,我被重定向到登录页面.嗯,这也许不错,但我想显示404而不是它,并尝试坚持使用属性.那可能吗?
解决方法
Is that possible?
public class MyAuthorizeAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.Result = new ViewResult { ViewName = "~/Views/Shared/401.cshtml" }; } }
然后使用它:
[MyAuthorize(Roles = "Admin")] public ActionResult Edit(int id)