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

后端 未结 2 1465
耶瑟儿~
耶瑟儿~ 2021-01-12 00:00

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 d

2条回答
  •  执念已碎
    2021-01-12 00:19

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

提交回复
热议问题