简体   繁体   中英

MVC 4 Redirecting from an Error

I have an ExceptionHandlerAttribute which is applied universally.

When an security exception occurrs I am trying to redirect to a page, but also have the URL change too. When a child action or other sub action has an error, it merges the error html page with the current page:

var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
var result = new ViewResult {
                   ViewName = viewName,
                   ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
               };
result.ViewBag.StatusCode = statusCode;
return result;

How can I get it to rediectly completely to the Account/Login or the Error/Security etc and not merge the pages as attached.

在此处输入图片说明

Here is what I did:

First define an action filter that derives from AuthorizeAttribute. When this attribute detects that the user is not authenticated AND it is an ajax request, it returns a 403 status (mvc does weird stuff with 401s). If it isn't ajax, do the normal stuff that AuthorizeAttribute does.

Next, in your layout file(s), set a global global error handler (assuming you are using jquery) that looks for 403 status and if it finds it calls location.replace on your logout url.

I am assuming this problem is happening in ajax calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM