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
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();
}
I wanted to be able to pass different views based on the request but they all basically used the same process and didn't want to make an action for each. The prior answer doesn't seem to work any more so here is what I came up with. This is .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);
}
This will allow you to put your individual views as the name of the routes and use them to set the view.