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

后端 未结 19 2714
爱一瞬间的悲伤
爱一瞬间的悲伤 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:42

    My solution, in case someone finds it useful.

    In Web.config:

    
        
          
        
        ...
    
    

    In Controllers/ErrorController.cs:

    public class ErrorController : Controller
    {
        public ActionResult PageNotFound()
        {
            if(Request.IsAjaxRequest()) {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return Content("Not Found", "text/plain");
            }
    
            return View();
        }
    }
    

    Add a PageNotFound.cshtml in the Shared folder, and that's it.

提交回复
热议问题