RouteValueDictionary的字符串URL

Rom*_*man 18 asp.net-mvc

是否有简单的方法将字符串URL转换为RouteValueDictionary集合?有些方法可以UrlToRouteValueDictionary(string url).

我需要这样的方法,因为我想根据我的路由设置'解析'URL,修改一些路由值,并使用urlHelper.RouteUrl()根据修改的RouteValueDictionary集合生成字符串URL.

谢谢.

Pio*_*pla 36

这是一个不需要模拟的解决方案:

var request = new HttpRequest(null, "http://localhost:3333/Home/About", "testvalue=1");
var response = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(request, response);
var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
var values = routeData.Values;
// The following should be true for initial version of mvc app.
values["controller"] == "Home"
values["action"] == "Index"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 有人已写过:[从URL创建RouteData实例](http://www.scottschluer.com/creating-a-routedata-instance-from-a-url/) (2认同)