将字符串转换为(对象)RouteValue C#
我正在为UrlHelper编写一个扩展函数。此函数将接受三个字符串参数。第一个参数是ACTIOn,第二个是CONTROLLER,第三个是一些随机字符串 示例:将字符串转换为(对象)RouteValue C#,c#,urlhelper,C#,Urlhelper,我正在为UrlHelper编写一个扩展函数。此函数将接受三个字符串参数。第一个参数是ACTIOn,第二个是CONTROLLER,第三个是一些随机字符串 示例:url.customeURL(“Index”、“Home”、“view=someview”) 在接受所有这些参数后,扩展函数将返回一个URL,其中包含操作、控制器和类似的查询字符串 /Home/Index?view=someview 以下是我的功能: public static string CustomUrlAction(this Ur
url.customeURL(“Index”、“Home”、“view=someview”)代码>
在接受所有这些参数后,扩展函数将返回一个URL,其中包含操作、控制器和类似的查询字符串
/Home/Index?view=someview
以下是我的功能:
public static string CustomUrlAction(this UrlHelper helper, string action, string controller, string parameters)
{
return helper.Action(action, controller, new { parameters });
}
我当前实现面临的问题是,当我将参数传递给routeValues对象时,它会使URL如下所示:
/home/index?parameters=view=someview
/Home/Index?view=someview
而我希望它创建一个URL,如:
/home/index?parameters=view=someview
/Home/Index?view=someview
那么,我到底有没有办法做到这一点?我知道这可以通过Url.Action(“Index”,“Home”,new{“view=someview”})轻松完成。
我不知道是否有更好、更受支持的方法,但这似乎很有效,所以你可以使用它
public static string CustomUrlAction(this UrlHelper helper, string action, string controller, string parameters)
{
return String.Format("{0}?{1}",helper.Action(action, controller),parameters);
}
返回string.format(“{0}?{1}”,helper.Action(Action,controller),parameters)如何?好的,我很高兴,我会留下它作为回答。