Custom error pages on asp.net MVC3

前端 未结 6 917
无人及你
无人及你 2020-11-22 11:07

I\'m developing a MVC3 base website and I am looking for a solution for handling errors and Render custom Views for each kind of error. So imagine that I have a \"Error\" Co

6条回答
  •  囚心锁ツ
    2020-11-22 11:48

    You can display a user-friendly error page with the correct http status code by implementing Jeff Atwood's User Friendly Exception Handling module with a slight modification for the http status code. It works without any redirects. Although the code is from 2004(!), it works well with MVC. It can be configured entirely in your web.config, with no MVC project source code changes at all.

    The modification required to return the original HTTP status rather than a 200 status is described in this related forum post.

    Basically, in Handler.vb, you can add something like:

    ' In the header...
    Private _exHttpEx As HttpException = Nothing
    
    ' At the top of Public Sub HandleException(ByVal ex As Exception)...
    HttpContext.Current.Response.StatusCode = 500
    If TypeOf ex Is HttpException Then
        _exHttpEx = CType(ex, HttpException)
        HttpContext.Current.Response.StatusCode = _exHttpEx.GetHttpCode()
    End If
    

提交回复
热议问题