1

So basically I have an Custom Authorize attribute and at some point it throwns an exception. I want to catch the exception and display the message to the user.

this is the contoller

[HandleError]
public class TestController : BaseController
{
    [CustomAuthorize("someperm, someperm2")]
    public ActionResult GamblerUser()
    {
        return View();
    }
}

this is the code in FilterConfig

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

this is the code in global.asax.cs

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

and the web.config

<customErrors defaultRedirect="~/Shared/Error" mode="On">
</customErrors>

and finally the view itself

@model HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h2>Sorry Error Occured</h2> <br />
<h2>@Model.Exception.Message</h2>

and this is the message I get whenever the exception occurs.

Runtime Error Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

I know that I am missing something very basic but this thing is just giving me headache. I've look at tons of examples I just cant get this thing working.

Dimitri
  • 2,798
  • 7
  • 39
  • 59

1 Answers1

1

ViewBag is null. I would use a static title in the html or find a way around it like this.

Community
  • 1
  • 1
Rush Frisby
  • 11,388
  • 19
  • 63
  • 83