Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple routes assigned to one method, how to determine which route was called?

I am working on a small ASP.NET MVC project at the moment. The project was released a few month ago. But changes should be implemented for usability and SEO reasons now. I decided to use attribute routing to create clean URLs.

At the moment a product page is called by:

hostname.tld/Controller/GetArticle/1234

I defined a new Route like this:

[Route("Shop/Article/{id:int}/{title?}", Name = "GetArticle", Order = 0)]
public ActionResult GetArticle(int id, string title = null) {
    // Logic
}

Everything works fine, but because of backwards compatibility and SEO reasons, the old route should be still available. And redirected with HTTP status code 301 to the new URL.

I've heard that it is possible to assign multiple routes to one action, like this:

[Route("Shop/Article/{id:int}/{title?}", Name = "GetArticle", Order = 0)]
[Route("Controller/GetArticle/{id:int}", Name = "GetArticle_Old", Order = 1)]
public ActionResult GetArticle(int id, string title = null) {
    // Logic
}

But I have no idea if this is a good solution or how to determine which route was called?

like image 283
John Doe Avatar asked Sep 21 '16 16:09

John Doe


People also ask

Can we define multiple routes for single action?

You can also write multiple routes onthe same action or controller. For example if you want to call Contact action method with /Home/contact url just write the [Route("home/contact")] on action method.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What is the method that registers all the routes into route table?

The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.

Can we map multiple URLs to the same action?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });


1 Answers

You can look at ControllerContext.RouteData to figure out which route they used when using multiple routes for one action.

public const string MultiARoute = "multiA/{routesuffix}";
public const string MultiBRoute = "multiB/subB/{routesuffix}";

[Route(MultiARoute)]
[Route(MultiBRoute)]
public ActionResult MultiRoute(string routeSuffix)
{

   var route = this.ControllerContext.RouteData.Route as Route;
   string whatAmI = string.Empty;

   if (route.Url == MultiARoute)
   {
      whatAmI = "A";
   }
   else
   {
      whatAmI = "B";
   }
   return View();
}
like image 161
stephen.vakil Avatar answered Sep 29 '22 03:09

stephen.vakil