MVC 5 Routing Issue - Mutiple Routes Wrong Aim at the Same View

I have configured several routes, but for some reason, despite the rules regarding different controllers and different views, different links are routed in the same way. See below, I have included RouteConfig in my file and link examples below:

RouteConfig.cs

using System.Web.Mvc; using System.Web.Routing; namespace WebApplication1 { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Account", url: "Account/{action}/{id}", defaults: new { controller = "Account", id = UrlParameter.Optional } ); routes.MapRoute( name: "Member", url: "Member/{action}/{id}", defaults: new { controller = "Member", id = UrlParameter.Optional } ); routes.MapRoute( name: "Root", url: "{action}/{id}", defaults: new { controller = "Home", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Details", url: "{controller}/{action}/{u}", defaults: new { controller = "Member", action = "Details", u = UrlParameter.Optional } ); routes.MapRoute( name: "Article", url: "{Home}/{Article}/{id}/{articleName}", defaults: new { controller = "Home", action = "Article" } ); routes.MapRoute( name: "Item", url: "{News}/{Item}/{id}/{itemName}", defaults: new { controller = "News", action = "Item" } ); } } } 

References

 http://localhost:11508/Home/Article/2/Participate http://localhost:11508/News/Item/2/Second-Test 

As you can see, links and rules are certainly unique, but for some reason the Item rule is ignored, it just passes Id 2 to the Home / Article view.

+6
source share
3 answers

You should not include the names of the controllers / actions in brackets - just pass them as is so that the path can be matched. Your last two routes should look like this:

  routes.MapRoute( name: "Article", url: "Home/Article/{id}/{articleName}", defaults: new { controller = "Home", action = "Article" } ); routes.MapRoute( name: "Item", url: "News/Item/{id}/{itemName}", defaults: new { controller = "News", action = "Item" } ); 

In addition, it is convenient to place such specific routes to any other routes, and not after the default routes.

UPDATE

In principle, this should be a separate question, but it is easier to simply answer it here.

From the comment:

how can I get http://localhost:11508/Member/Details?u=testuser to route to http://localhost:11508/Member/Details/testuser instead of the parameter displayed.

  • Create a controller action that takes this parameter, like this one:

     public ActionResult Details(string u, ...) { var model = new ... ... return View(model); } 
  • Register a route that takes the u parameter as part of a URL like this

      routes.MapRoute( name: "MyRoute", url: "Member/Details/{u}", defaults: new { controller = "Member", action = "Details", u = UrlParameter.Optional } ); 

    Here {u} actually declares the name of the parameter and how it should be used (parse / display) inside the URL.

  • Link to a URL like this:

     <a href="@Url.Action("Details", "Member", new {u = "testuser"})">linktext</a> 

In all these steps, u is the name of the parameter that you will use.

+1
source

The mapping accepts the first matching rule. An "Item" -Route will never be used because Article-Root will catch all requests that may match an "Item" -Route. Check the route order and remove {} news.

 routes.MapRoute( name: "Item", url: "News/Item/{id}/{itemName}", defaults: new { controller = "News", action = "Item" } ); 
+1
source

Your problem is in the order in which you register your routes. The rule is that you should register them with the lowest value. In other words, your default route should be the last.

With the way you do it right now, MVC gets to your default route, because your item route matches that, so as soon as it gets to it, it stops looking for other routes and uses it.

Move the item route at the top of your RegisterRoutes method, and it should work fine.

0
source

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


All Articles