ASP.NET MVC - Catch All Route And Default Route

后端 未结 8 1946
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:15

In trying to get my application to produce 404 errors correctly, I have implemented a catch all route at the end of my route table, as shown below:

 routes.M         


        
8条回答
  •  温柔的废话
    2020-11-28 05:09

    Use route constraints

    In your case you should define your default route {controller}/{action}/{id} and put a constraint on it. Probably related to controller names or maybe even actions. Then put the catch all one after it and it should work just fine.

    So when someone would request a resource that fails a constraint the catch-all route would match the request.

    So. Define your default route with route constraints first and then the catch all route after it:

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { controller = "Home|Settings|General|..." } // this is basically a regular expression
    );
    routes.MapRoute(
        "NotFound",
        "{*url}",
        new { controller = "Error", action = "PageNotFound" }
    );
    

提交回复
热议问题