在我的 MVC 应用程序中,我调用 HttpUnauthorizedResult 类并指定 statusDescription 参数。
if (!canAdd) {
return new HttpUnauthorizedResult("You do not have access to add");
}
这也将我重定向到 AccountController 上的 Login 方法,然后我将它们重定向到相应的屏幕。
public ActionResult Login(string returnUrl)
{
if (WebSecurity.IsAuthenticated)
{
return RedirectToAction("AccessDenied");
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
我的问题是如何利用 Status Descripton 参数,在 AccessDenied View 中显示这些详细信息会很好。
最佳答案
得到了同样的问题。
我使用 TempData 设置消息,因为无法设置 Viewbag。
filterContext.Controller.TempData["message"] = "Access Denied";
filterContext.Result = new HttpUnauthorizedResult();
HttpUnauthorizedResult 将我重定向到我的帐户 Controller 的登录操作,我在其中检查(错误)消息:
if (TempData.Count > 0)
{
var message = TempData["message"];
ModelState.AddModelError("", message.ToString());
}
关于asp.net-mvc - 在 HttpUnauthorizedResult 上使用状态描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605318/