views:

71

answers:

1

Good day!

I've recently switched from IIS 6.0 to IIS 7.x and I'm in search of error handling technique of my dream for ASP.NET MVC 2.

What I want to achive:

  • Handle all unhandled exceptions in one place (preferable in Global.asax handler)

  • Custom handlers for 404 and 403 errors (both for MVC controller\actions and static files). These handlers should not perform rewriting and should send HTTP error codes. For example if user navigates to http://example.com/non-existing-page/ he should remain on this URL, but get HTTP 404 status and custom 404 page.

  • Ability to trigger 404 and 403 errors programmatically from actions. For example if user specified non-existing page number in paging, like this: http://example.com/posts/page-99999/

  • It will be great if this error handling will work the same for VS Development Server (I know about IIS Express, but for now I should stick to VS Dev Server)

I've used this: http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx while being on IIS 6.0, but now on IIS 7.0 with integrated pipeline I see IIS error messages instead of my handlers.

Thanks in advance!

A: 

i use

protected void Application_Error(object sender, EventArgs e)

in my Global.asax.cs

to catch all unhanded exceptions, doing something like this inside it:

try
{
    Response.Clear();
    var errorController = new ErrorController();
    var result = errorController.Error(statusCode, exception);
    result.ExecuteResult(new ControllerContext(new RequestContext(new HttpContextWrapper(Context), routeData), errorController));
    Server.ClearError();
}
catch(Exception e)
{
    HttpContext.Current.Response.StatusCode = 500;
    HttpContext.Current.Response.Write(e.Message);
}

My Error controller looks like this:

public ActionResult Error(HttpStatusCode statusCode, Exception exception)
{
    var resource = new ErrorResource(statusCode, exception);
    this.response.StatusCode = resource.StatusCode;

#if !DEBUG
    return View("ReleaseError", resource);
#endif

    return View("DebugError", resource);            
}

I can then do:

throw new HttpException(404, "not found");

or

throw new HttpException(403, "not found);

etc programatically.

I think MVC2 introduced a new action result for error cases, not used it though, probably stinks like the rest of the framework.

Andrew Bullock
-1 because of your last comment. Too bad because I like your solution.
Tommy
if you think ASP.NET MVC is a good framework, you are seriously deluded. Honestly, I'm not being rude. Have you ever used Rails or OpenRasta? Everything about MVC is badly designed, the routing, the pseudo request "pipeline" (and i use that term in the loosest possible sense) the controller concept, action results and their invokation, error handling as we have here and the WebForms view engine. Seriously, tell my why any part of MVC is well put together?
Andrew Bullock
Thank you for the answers, what about custom error settings in IIS level itself?
artvolk
@Andrew, the down vote was not because I disagreed with your comment, but I feel that SO questions about a function/method are not the right place for subjective commentary on the merits of a framework. I think that part of what makes SO such a good site is the minimal interjections of "framework X is the sux" comments. Flame wars have been started over less :)
Tommy
@artvolk I have a flat HTML page for 500s set up in IIS for a worst case fall back just in case everything dies. This has to be just a html page (no server side code) because if iis is hitting this, something is seriously wrong (like asp.net isnt working)
Andrew Bullock
@Tommy fair enough, just had a bad day struggling with things i shouldnt have to be struggling with, so i added that jibe on the end :)
Andrew Bullock
I have some other problem with such approach for 404, but it seems I have solved it: http://stackoverflow.com/questions/3554844/asp-net-mvc-404-handling-and-iis7-httperrorsI agree about fallback 500 page to be static.
artvolk