ASP.NET MVC - Catch the entire route and the default route

When trying to get my application to correctly handle 404 errors, I performed the entire route at the end of the route table, as shown below:

routes.MapRoute( "NotFound", _ "{*url}", _ New With {.controller = "Error", .action = "PageNotFound"} _ ) 

However, to get this, I had to remove the default route:

 {controller}/action/{id} 

But now that the default value has been removed, most of my action links no longer work, and the only way I found them to work again was to add separate routes for each controller / action.

Is there an easier way to do this rather than adding a route for each controller / action?

Is it possible to create a default route, which still allows you to work with the catch of any route if the user tries to go to an unknown route?

+57
asp.net-mvc asp.net-mvc-2
Oct 22 2018-10-21
source share
7 answers

Use route restrictions

In your case, you should define your default route {controller}/{action}/{id} and set a limit on it. Probably related to controller names, or perhaps even actions. Then place the trick behind it and it should work fine.

Therefore, when someone requests a resource that does not comply with the restriction, the catch-all route will match the request.

So. First define a default route with route restrictions, and then catch the entire 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" } ); 
+89
Jan 12 '11 at 11:29
source share
 //this catches all requests routes.MapRoute( "Error", "{*.}", new { controller = "PublicDisplay", action = "Error404" } ); 

add this route to the end of the route table

+14
Oct 22 '10 at 21:32
source share

And, the problem is that your default route captures all 3 segments of the URL. The problem here is that Routing starts before we determine who will process the request. Thus, any URL of three segments will correspond to the default route, even if it ends later when there is no controller to handle it.

One thing you can do is have your controller override the HandleMissingAction method. You should also use a tag to catch all 404 problems.

+7
Oct 23 '10 at 3:01
source share

Well, I found that there is no good way to do this. I set the redirectMode property for customErrors in ResponseRewrite .

 <customErrors mode="On" defaultRedirect="~/Shared/Error" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/Shared/PageNotFound"/> </customErrors> 

This gives me the desired behavior, but does not display a formatted page.

It’s not done well for me, as SEO goes. However, I believe that there is a solution that I am missing as SO is doing exactly what I want. The URL remained on the error page and throws 404. Check the Firewall in stackoverflow.com/fail.

+1
Oct 22 2018-10-22
source share

I would recommend this as the most readable version. This is necessary in your RouteConfig.cs and ErrorController.cs controller containing the "PageNotFound" action. This may return the view. Create a PageNotFound.cshtml file and it will be returned in response to 404:

  routes.MapRoute( name: "PageNotFound", url: "{*url}", defaults: new { controller = "Error", action = "PageNotFound" } ); 

How to read it:

 name: "PageNotFound" 

= create a new route template with an arbitrary name "PageNotFound"

 url:"{*url}" 

= use this pattern to match all other raw routes

 defaults: new { controller = "Error", action = "PageNotFound" } 

= determine the action to which the wrong path will be displayed (the "PageNotFound" action method in the error controller). This is necessary, since an incorrectly entered path will obviously not be associated with any action

+1
Feb 07 '16 at 23:36
source share

You should probably configure the 404 error file in the configuration section and then restore the default route.

FWIW, I think the default route requirement is also delayed.

0
Oct 22 2018-10-22
source share

My solution is 2 steps.

I initially solved this problem by adding this function to the Global.asax.cs file:

 protected void Application_Error(Object sender, EventArgs e) 

Where I tried casting Server.GetLastError () to an HttpException and then checked GetHttpCode. This solution is described in detail here:

ASP.NET MVC Custom Error Handling Application_Error Global.asax?

This is not the original source where I received the code. However, this only catches 404 errors that have already been redirected. In my case, this means any level 2 URL.

for example, these URLs displayed page 404:

www.site.com/blah

www.site.com/blah/blah

however, www.site.com/blah/blah/blah will simply say that the page was not found. Adding your track along the entire route AFTER all my other routes resolved this:

 routes.MapRoute( "NotFound", "{*url}", new { controller = "Errors", action = "Http404" } ); 

However, the NotFound route does not seem to route requests that have file extensions. This works when they are captured by different routes.

0
Feb 02 2018-12-12T00:
source share



All Articles