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

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

    1) Make abstract Controller class.

    public abstract class MyController:Controller
    {
        public ActionResult NotFound()
        {
            Response.StatusCode = 404;
            return View("NotFound");
        }
    
        protected override void HandleUnknownAction(string actionName)
        {
            this.ActionInvoker.InvokeAction(this.ControllerContext, "NotFound");
        }
        protected override void OnAuthorization(AuthorizationContext filterContext) { }
    }  
    

    2) Make inheritence from this abstract class in your all controllers

    public class HomeController : MyController
    {}  
    

    3) And add a view named "NotFound" in you View-Shared folder.

提交回复
热议问题