Is it possible to have a strongly typed GetVirtualPath method?
Based on the answer from this SO post , I'm trying to tweak it a bit so that I don't have to hardcode the route and route parameters, but instead (if possible) use a strongly typed controller action method: -
string path = RouteTable.Routes.GetVirtualPath(
new RequestContext(HttpContext,
RouteTable.Routes.GetRouteData(HttpContext)),
new RouteValueDictionary(
new
{
controller = "Foo",
action = "Bar"
})).VirtualPath;
The MVC Futures namespace (Microsoft.Web.MVC) has the ability to heavily type ActionLink ... so I was wondering if something like this could be done here.
Strongly typed GetVirtualpath ???
a source to share
The short answer is yes you can. The reason I know this is because the ActionLink
one you're talking about (Futures) has an overload that takes a strongly typed delegate Action
that converts it to RouteValueDictionary
for RouteLink
. I'm not sure if you can just use ExpressionHelper
. I think you can. Let me try to find an example ...
EDIT This might work:
string path = RouteTable.Routes.GetVirtualPath(
new RequestContext(HttpContext,
RouteTable.Routes.GetRouteData(HttpContext)),
Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression<YourController>(c=>c.YourAction())).VirtualPath;
a source to share