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 months ago. But changes must be implemented for usability and SEO. I decided to use attribute routing to create clean URLs.

Currently the product page is being called:

hostname.tld / controller / GetArticle / 1234

I defined a new route as follows:

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

Everything works fine, but due to backward compatibility and SEO reasons, the old route should be accessible. And redirected with HTTP status code 301 to the new URL.

I heard that you can assign multiple routes for a single action, for example:

[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 whether this is a good solution or how to determine which route was invoked?

+4
2

ControllerContext.RouteData, , .

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();
}
+7

, . , , , . .Net Core 2.2.

 [HttpGet]
[Route("[controller]/ManageAccessView/{name}/{id}",Name = "ManageAccessView")]
[Route("[controller]/ManageAccessUsers/{name}/{id}", Name = "ManageAccessUsers")]
[Route("[controller]/ManageAccessKeys/{name}/{id}", Name = "ManageAccessKeys")]
public async Task<IActionResult> ManageAccessView(int id, string name)
{

  var requestedView = this.ControllerContext.ActionDescriptor.AttributeRouteInfo.Name;

  return View(requestedView);


}

.

0

All Articles