本文介绍了字符串的URL RouteValueDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有作为简单的方法来字符串URL转换成RouteValueDictionary收藏?像 UrlToRouteValueDictionary一些方法(字符串URL)

Is there as easy way to convert string URL to RouteValueDictionary collection? Some method like UrlToRouteValueDictionary(string url).

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

I need such method because I want to 'parse' URL according to my routes settings, modify some route values and using urlHelper.RouteUrl() generate string URL according to modified RouteValueDictionary collection.

感谢。

推荐答案

下面是一个解决方案,不需要嘲讽:

Here is a solution that doesn't require mocking:

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"

希望这有助于。

这篇关于字符串的URL RouteValueDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:52