开发者

MVC 3, defaultRedirect and the HandleErrorInfo model

I've attempting to redirect to a custom error page (via a specific controller) to deal HTTP error codes that haven't been explicitly handled.

To facilitate this, I have the following config

<customErrors mode="On" defaultRedirect="errors" />

And for my controller:

public class ErrorsController : Controller
{
    public ActionResult Index ()
    {
        return View ();
    }
}

The idea being I can eventually deal with and display a custom error details here, over and above what the Error.cshtml is displaying.

My issue is around getting the error information...I don't have a HandleErrorInfo instance that I can find, nor does Service.GetLastError () return anything...

Does anyone know how开发者_运维百科 to generate or get a HandleErrorInfo object? - I'd prefer to use this, and have it filled in somehow to fit in with the rest of the error handling.

Thanks,

Kieron


Add the HandleError attribute to your controller (or specific actions):

[HandleError]
public class HomeController

Then use this in your web.config to control the redirect behaviour based on different errors:

<system.web>
  <customErrors mode="On" defaultRedirect="Error.aspx">
    <error statusCode="403" redirect="NoAccess.htm"/>
    <error statusCode="404" redirect="FileNotFound.htm"/>
  </customErrors>
</system.web>

I'm sure you can tweak the redirect to use a custom /controller/action and this should post the HandleErrorInfo for you hopefully but never tried it.


If you are using Razor put [HandleError] at the top of your controller. Put your error page named Error.cshtml in the Shared folder.

The setting in your web.config then should be as follows:
customErrors mode="On"

Any redirect on the above statement will not be used.

That it is it. Unhandled exceptions will show your Error page. To test this in debug mode make sure customErrors mode ="On" is set in your main web.config.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜