How can I properly handle 404 in ASP.NET MVC?

后端 未结 19 2800
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 10:14

I am using RC2

Using URL Routing:

routes.MapRoute(
    \"Error\",
     \"{*url}\",
     new { controller = \"Errors\", action = \"N         


        
19条回答
  •  太阳男子
    2020-11-21 10:39

    ASP.NET MVC doesn't support custom 404 pages very well. Custom controller factory, catch-all route, base controller class with HandleUnknownAction - argh!

    IIS custom error pages are better alternative so far:

    web.config

    
      
        
        
      
    
    

    ErrorController

    public class ErrorController : Controller
    {
        public ActionResult PageNotFound()
        {
            Response.StatusCode = 404;
            return View();
        }
    }
    

    Sample Project

    • Test404 on GitHub
    • Live website

提交回复
热议问题