Correct 404 message for a missing ASP.Net MVC controller

前端 未结 2 1413
遇见更好的自我
遇见更好的自我 2021-01-03 10:15

I have an MVC 2 application that should always give a \'nice\' 404 page.

However currently I get a low level .Net one: \"Server Error in \'/sitename\' Applicatio

2条回答
  •  清歌不尽
    2021-01-03 10:17

    Setting it up at web.config level does not "just redirect to the page". On an MVC app, you give it a "{controller/action}" url an it will actually call that action:

     
        
    
    

    This will call the NotFound on the SystemController.

    In your action you can then for instance get the value of HttpContext.Request.RawUrl to see what the faulty request was: "/system/notfound?aspxerrorpath=/Invalid". In this case I tried to go to the InvalidController.

    A nice way to handle this things, by the way, is implementing ELMAH (or Error Logging Modules and Handlers. Scott Hanselman wrote an "introductory" post about it, but ELMAH is nowadays available as a NuGet package.

    You might want to take a look at this question/ansers on how to use it with ASP.NET MVC: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

提交回复
热议问题