Parse MVC route custom file extension from RequestContext
-
15-11-2019 - |
Pregunta
I have set up two custom extensions to enable MVC in IIS6.
So the site can be accessed with a URL of either something like...
mysite/mycontroller.europe/myaction
or like...
mysite/mycontroller.america/myaction
What is the most robust way of finding the extension from the RequestContext
instance?
So I would like to be able to write something like...
var location = reqContext.......GetExtenstion(); // location = "europe"
and obviously have that work even if the setup of the site/directories changes a little.
Solución
Define a route:
routes.MapRoute(
"DefaultWithExtension",
"{controller}.{extension}/{action}",
new { controller = "Home", action = "Index", extension = "america" }
);
and then:
var extension = RequestContext.RouteData.GetRequiredString("extension");
Otros consejos
Also you can just define extension as a string parameter for all relevant actions for the controllers, in which case it will be directly available. e.g.
public ActionResult myaction(string extension)
This does still need the mapRoute entry defined above.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow