How can I do this ASP.NET MVC 1.0 code in MVC 2.0?
I'm converting one of our ASP.NET MVC application from 1.0 to 2.0.
This is code that is erroring :-
string virtualPath = HttpContext.Request.ServerName() +
RouteTable.Routes.GetVirtualPath(
new RequestContext(HttpContext,
RouteTable.Routes.GetRouteData(HttpContext)),
ExpressionHelper.GetRouteValuesFromExpression<PostController>(
c =>c.Details("Post-Title")))
.VirtualPath;
Ok .. that's a l开发者_StackOverflow中文版ot of ugly code. So what's going on?
It looks like we're trying to get the virtual path of a route. The FULL path, that is. Further investing the reason, it's because this value is used in some Service code, which has no idea about MVC or websites, etc. It's independent from the View. It needs a virtualPath because it converts that to a TinyUrl (yeah, one of those url shortening services).
So ... how can I do this in ASP.NET MVC 2.0 (using VS2010 B2)?
What's the error message?
'ExpressionHelper' is an ambiguous reference between 'System.Web.Mvc.ExpressionHelper' and 'Microsoft.Web.Mvc.Internal.ExpressionHelper'
hmmm ......
a "using directive" to create an alias to the namespace ?
using ExpressionHelper = Microsoft.Web.Mvc.Internal.ExpressionHelper;
I think you just need to qualify it, like so:
string virtualPath = HttpContext.Request.ServerName() + RouteTable.Routes.GetVirtualPath( new RequestContext(HttpContext, RouteTable.Routes.GetRouteData(HttpContext)), **System.Web.Mvc.**ExpressionHelper.GetRouteValuesFromExpression( c =>c.Details("Post-Title"))) .VirtualPath;
精彩评论