How to stop asp.net mvc from redirecting to error controller when an exception occurs during json post

How to stop asp.net mvc from redirecting to an error page when it was json mail? It continues to try to redirect to the error path in my configuration file when an exception occurs, and I need it to return an exception ... also it does not even try and directs them to the correct path - it continues to search for the error. cshtml view in the Home section (the home folder for the views, as if it was trying to use my home operator), when they are all under the Error / controller installation folder, which is clearly described in my configuration file:

<customErrors mode="On" defaultRedirect="~/Error/Error"> <error statusCode="404" redirect="~/Error/NotFound" /> <error statusCode="403" redirect="~/Error/Forbidden" /> </customErrors> 

Thanks!

+4
source share
2 answers

Solution 1: You can have a BASE controller for all your controllers (this is a good idea), and in this controller you can override the OnException method as follows:

  protected override void OnException(ExceptionContext filterContext) { if (Request.IsAjaxRequest()) { // TODO } else { base.OnException(filterContext); } } 

Solution 2: You can create a filter of user actions to complete the task and use the method above. How can you register this filter in global.asax or decorate only those controllers / actions that you need.

The general idea is to use:

 Request.IsAjaxRequest() 

Hope this helps. Good luck and have fun :)

0
source

you can do this by registering the Application_Error event in Global.asax and then redirect it to the error controller with the data in this way or you can perform any function that you want, depending on the error code

 protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "oops"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "NoAccess"; break; case 404: routeData.Values["action"] = "NotFound"; break; } } IController errorsController = new ErrorController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); } 

then create a controller with oops / NotAccess / NotFound actions like

 public ActionResult oops(Exception exception) { //return Content("General failure", "text/plain"); // return View(); } public ActionResult NotFound() { // return Content("Not found", "text/plain"); // return View("oops"); } public ActionResult NoAccess() { //return Content("Forbidden", "text/plain"); //return View("oops"); } 

And bring back your own view with relevant information

+1
source

Source: https://habr.com/ru/post/1413203/


All Articles