views:

144

answers:

1

Hi Folks,

based upon an answer from this SO post, i'm trying to tweak it a bit so that i don't need to hardcode the route and route params, 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 strongly type an ActionLink ... so i was wondering if this is possible to do something like that, here.

A strongly typed GetVirtualpath ???

+1  A: 

Short answer - yes you can. The reason i know this is because the ActionLink you speak of (Futures) has an overload that accepts a strongly typed Action delegate which converts it to a RouteValueDictionary for the RouteLink. I'm not sure if you can just use the ExpressionHelper however. I think you can. Let me try and 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;
cottsak