Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MVC Custom Error pages retain Route Values?

The MVC project that I am currently working on uses Regions so that we can localise pages etc.

I have spotted a problem with our Error page. We have turned the custom error pages on in the web.config file. If we are on a page lets say : /IT/News/Index and we get an error, when it redirects it will go to /Error and there will be no routevalue attached to it.

Is there away to ensure that the langauge routevalue is retained by the Error page?

I have searched around and cannot find a solution at the moment and was wondering if anyone else could help or point me in the right direction?

Hope that this all makes sense. Any help is much appreciated.

like image 572
Gaz Winter Avatar asked Jan 13 '23 18:01

Gaz Winter


2 Answers

If you're getting physically redirected to /Error then it's not because of the MVC HandleErrorAttribute. It's probably due to your Web.Config having system.web/customErrors defined for error handling. Using the HandleErrorAttribute causes it to inject a specific view instead of the view you would have normally returned but does not redirect you to a different action by default. The problem is when redirected because of customErrors, there is no inherant information available to tell you where they came from. But using HandleErrorAttribute DOES cause some info to be populated for you. Specifically it creates a HandleErrorInfo to use as a view model and passes that to the view you specify. For example, here's one that is reigstered in the /App_Start/FilterConfig.cs file.

public class FilterConfig
{
    public static void RegisterFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute {View = "Error"});
    }
}

When you redirect to an error View using the HandleErrorAttribute, certain information is populated for you. The HandleErrorInfo view model will contain the ControllerName of the original controller requested, and the ActionName of the original action. Also, the ViewData and the TempData from the original request will be copied into the ViewData and Temp data for the request to the Error view. With that information it should have what you need. Be aware that not all errors happen inside of an Action however, and exceptions that don't happen in an action will not be caught by the HandleErrorAttribute. So you'll still need to use something like customErrors (or system.webServer/httpErrors if you're doing it inside of IIS7+) to handle exceptions that occur elsewhere in your app.

Here's a link to the HandleErrorAttribute file on CodePlex in case you're wondering what it does. HandleErrorAttribute.cs

like image 193
Nick Albrecht Avatar answered Jan 22 '23 03:01

Nick Albrecht


I'm not sure if this solution meets you requirements. You can override in your base controller OnException and then redirect to a specific page.

  protected override void OnException(ExceptionContext filterContext)
  {
        string controller = filterContext.RouteData.Values["controller"].ToString();
        string action = filterContext.RouteData.Values["action"].ToString();
        //get other stuff from routing
        //here you can do redirect or other stuff

        //if handled exception
        //filterContext.ExceptionHandled = true;

        base.OnException(filterContext);
  }
like image 44
jan salawa Avatar answered Jan 22 '23 04:01

jan salawa