ASP.Net MVC Routing Strategy

I played with ASP.Net MVC for a while. I found that the hardest part is the routing table.

I found that most examples leave the default route in place. I found that this leads to a lot of errors when the default route redirects an action to the HomeController that does not exist. Leading to strange error messages where you expect to see a simple 404.

In the end, I decided to set up routing, where I explicitly define all the controller / action combinations that I want to allow, with a full interception at the end to redirect to page 404, which shows a reasonable error message.

Am I missing something? Or is this a really good way to do something?


Looking at the answers that I have, I think I better clarify the question.

I am trying to check the routing version of the website that I am creating. I noticed that when I go to the default route {controller} / {action} / {id}, all types of URLs where I would like to display a 404 error do get routing in the HomeController with an invalid action and lead to some ugly error instead.

I'm a bit confused because most code examples just go to the default route. Is there a reason it is, or is it normal to remove it?

The circuit I use looks a bit like this

routes.MapRoute( "About", "About", new {controller = "Page", action = "About"} ); routes.MapRoute( "SignIn", "SignIn", new {controller = "Page", action = "SignIn"} ); routes.MapRoute( "SignOut", "SignOut", new {controller = "Page", action = "SignOut"} ); routes.MapRoute( "Authenticate", "Authenticate", new { controller = "Authentication", action = "Authenticate" }); routes.MapRoute("CatchAll", "{*url}", new { controller = "Error", action = "Http404" }); 

I have a route specified for each action in the system. And enough to display 404 at the end. Is this a good way to do this, or is there an easier way to make the routing scheme flawless?

+4
source share
4 answers

If this is the default route you are using:

 routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); 

then the "HomeController" and the "Index" action will be used by default, unless your URL indicates otherwise.

For instance:

" http://www.something.com/ " will use the index of the Home index because it is the controller and the default action.

" http://www.something.com/foo " will use the Foo controller index action because "Index" is the default action.

" http://www.something.com/foo/bar " will use the foo controller's bar action

" http://www.something.com/foo/bar/1 " will use the "bar" action of the Foo controller, passing "1" as the parameter "id"

If you do not have a "FooController", everything that starts with " http://www.something.com/foo will fail. Similarly, if your FooController does not have a Bar action, then" http: //www.something .com / foo / bar "will fail.

Perhaps you already know everything that I wrote above. If so, will you post URLs that don't work, so we can better help?

+7
source

I prefer to set up routes explicitly for each action method.

Check this one .

+2
source

This is a very good routing debugging utility:

Debug Phil Haack Route

+1
source

This question is a little older, but I just stumbled upon it, having the same question.

I'm not too keen on determining a ton of routes manually, and I don't want to add a bunch of attributes to automatically create routes for me (Arnis link).

After some consideration, I decided to use the simple RouteConstraint that I created. I changed the default route to use it, and added my 404 file by default, for example:

  routes.MapRoute (
     "Default",
     "{controller} / {action} / {id}",
     new {controller = "Home", action = "Index", id = ""},
     new {controller = new ExistingControllerActionConstraint ()}
 );

 routes.MapRoute (
     "CatchAll",
     "{* catchall}",
     new {controller = "Home", action = "NotFound"}
 );

And the restriction class:

  public class ExistingControllerActionConstraint: IRouteConstraint
 {
     public bool Match (HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
     {
         Type t = Type.GetType ("MvcApplication_BareMinimum7.Controllers." + Values ​​["controller"] + "Controller");

         if (t == null)
         {
             return false;
         }

         MethodInfo mi = (from m in t.GetMethods () where m.Name == values ​​["action"]. ToString () select m) .FirstOrDefault ();

         return (mi! = null);
     }
 }

The cost of using Reflection and LINQ may outweigh the cost of registering tens of hundreds of routes, but it just seems to me cleaner.

0
source

Source: https://habr.com/ru/post/1286512/


All Articles