开发者

Adding an error message to the view model based on controller attribute in ASP.NET MVC

I have a custom attribute for authorization on my controller, the details of which can be viewed here.

If this custom attribute returns false, it returns to the login screen. 开发者_如何学CI want to add a error to my model for the login screen if the attribute returns false.

Any ideas?


For those who are looking for the answer, here is how I ended up doing it:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
   if (filterContext.HttpContext.User.Identity.IsAuthenticated)
   {
      var result = new ViewResult();
      result.ViewName = "NotAuthorized";
      result.MasterName = "_Layout";
      filterContext.Result = result;
   }
   else
      base.HandleUnauthorizedRequest(filterContext);
}


Look at the TempData feature. Basically, it is similar to ViewData, but lives to the next request (using session). So, store your model error here (wrap it up appropriately if needed) and look for it after redirect in login page action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜