Asp.net mvc custom exception filter to force return of full view, not partial

I have a custom exception filter that I call by adding the [CustomExceptionFilter] attribute to my class. It works as I would like, but if the action method returns a partial view (via an ajax request), an exception (which is basically a redirect to an unauthorized page) loads a partial view using this page. Is there a way to make it reload the "parent "URL?

Here is the code for a custom exception filter

public class CustomExceptionFilter : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (filterContext.Exception.GetType() == typeof(CustomSecurityException)) { filterContext.ExceptionHandled = true; RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData); string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "NoAccess", action = "Index", message = filterContext.Exception.Message })).VirtualPath; filterContext.HttpContext.Response.Redirect(url, true); } } } 
+6
filter asp.net-mvc custom-exceptions
source share
8 answers

This is what you need to handle in the browser. Try handling error () when calling jQuery.ajax () (and obviously not returning the redirect ..).

+2
source share

I propose to allow the exception bubbles to the client and handle it, as Maxwell suggested.

In our previous project, we used a special actionfilter to handle ajax errors (borrowed from Suteki Shop ). Please note that the response status is 500 (internal server error). To respond to a request to invoke the Error () delegate, an error status is required to call JQuery.ajax ().

  public class HandleErrorWithAjaxAttribute : HandleErrorAttribute { public HandleErrorWithAjaxAttribute() { ShowStackTraceIfNotDebug = true; } public bool ShowStackTraceIfNotDebug { get; set; } public override void OnException(ExceptionContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { string content = ShowStackTraceIfNotDebug || filterContext.HttpContext.IsDebuggingEnabled ? filterContext.Exception.StackTrace : string.Empty; filterContext.Result = new ContentResult { ContentType = MediaTypeNames.Text.Plain, Content = content }; filterContext.HttpContext.Response.Status = "500 " + filterContext.Exception.Message .Replace("\r", " ") .Replace("\n", " "); filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; } else { base.OnException(filterContext); } } } 
+1
source share

I am using hanlder OnFailure in the form tag.

 <form id="AJAXForm" method="post" action="" onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, httpMethod: 'POST', updateTargetId: 'myPartialPage', onSuccess: Function.createDelegate(this, ajaxFormSucced), onFailure: Function.createDelegate(this, ajaxFormFailure) });" > 

... function ajaxFormSucced () {// Success code} function ajaxFormFailure () {// Fail code}

+1
source share

You can check if the request is an ajax request or not. You can, for example, do the following ...

 if (!filterContext.HttpContext.Request.IsAjaxRequest()){ //Return a ViewResult //filterContext.ExceptionHandled = true; //filterContext.Result = new ViewResult { ViewName = "Error" ... }; } else{ //An ajax request. //return a partial view } 

However, since Maxwell said you can let the bubble take off if it is an ajax request and handles an error on the client. You can globally configure how to handle exceptions in ajax requests, as described here

0
source share

Did you try to clear the answer? The controller can still customize the contents of the response.

 filterContext.HttpContext.Response.Clear() filterContext.Result = new JsonResult { Data = new { Message = message } }; filterContext.HttpContext.Response.StatusCode = 500; filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
0
source share

This link helped me ASP.NET MVC exception handling when sending to controller via Ajax using jQuery

Finally, when testing the javascript function, start with a warning on the first line. Any javascript errors in your function will probably stop the function in the middle of execution without feedback from JavaScript through the browser (depending on your setting).

0
source share

This will help you .

Just add the extension method .IsAjaxRequest and return the 403 status code to the browser, jquery ajaxError will handle its redirection to the login page

0
source share

As Maxwell says, handle the client using something like this

 function handleError(ajaxContext) { // Load parent } @using (Ajax.BeginForm("Index", "Home", new AjaxOptions { UpdateTargetId = "MyDiv", OnFailure = "handleError" })) 

What you have to do is make sure the controller code for the ActionResult for NoAccess contains the following code, so that you run your ajax error.

 HttpContext.Response.StatusCode = 401; 
0
source share

All Articles