Razor 操作从字符串变量获取路由值
我的cshtml中有3个字符串变量:Razor 操作从字符串变量获取路由值,razor,asp.net-mvc-5,Razor,Asp.net Mvc 5,我的cshtml中有3个字符串变量: string strController = "Controller"; string strAction = "Action"; string strQuerystring = "Pacode=2"; 我正在尝试对锚标记生成操作: <a href="@Url.Action(@strAction , @strController,@strQuerystring )" title="test"> <span>@Test</s
string strController = "Controller";
string strAction = "Action";
string strQuerystring = "Pacode=2";
我正在尝试对锚标记生成操作:
<a href="@Url.Action(@strAction , @strController,@strQuerystring )" title="test">
<span>@Test</span>
</a>
预期输出:Controller\Action?Pacode=2
实际输出:Controller\Action?长度=8
当action方法期望作为对象路由值时,如何传递第三个字符串数据类型的参数?目前这是一个非常糟糕的问题。创建了一个扩展方法 如果有人发布更好的解决方案,那就太好了
public static string CustomUrlAction(this UrlHelper url, string controller = null, string action = null, string Query = null)
{
if (string.IsNullOrEmpty(controller)&& string.IsNullOrEmpty(action) && string.IsNullOrEmpty(Query ))
{
return "#";
}
if (string.IsNullOrEmpty(Query) )
{
return url.Action(action, controller);
}
return url.Action(action, controller) +"?"+ Query;
}
&称之为
<a href="@Url.CustomUrlAction(@strAction , @strController,@strQuerystring )" title="test">
<span>@Test</span>
</a>
Url.Action的第三个参数是objectroutevalues
,string
是object
,它只有一个属性(Length
),您得到Length=8
,因为“Pacode=2”
有8个字符,为什么不只是将其声明为object strQuerystring=new{Pacode=2};
?@stephen该值来自数据库。因此我将其作为字符串。