MVC Html.ActionLink从URL删除空的querystring参数 [英] MVC Html.ActionLink removes empty querystring parameter from URL
问题描述
我正在使用Html.ActionLink(string linkText, string actionName, object routeValues)
重载将一些参数发送给Action方法.
I'm using the Html.ActionLink(string linkText, string actionName, object routeValues)
overload to send some params to an Action Method..
有时我需要传递一个空的参数值,例如:?item1=&item2=value
Sometimes I need to pass an empty parameter value like: ?item1=&item2=value
我以我创建并作为routeValues
传递的匿名类型指定参数,但是null
和string.Empty
都导致URL省略了空的item1
参数.
I'm specifying the param in the anonymous type I create and pass as routeValues
, but both null
and string.Empty
result in a URL that omits the empty item1
param.
new { item1 = null, item2 = "value" }
new { item1 = string.Empty, item2 = "value" }
new { item1 = "", item2 = "value" }
// all result in:
?item2=value
我可以手动创建URL,但是我想使用helper方法.
I can create the URL manually but I want to use the helper method.
建议?
推荐答案
创建一个EmptyParameter
类,如下所示:
Create an EmptyParameter
class as follows:
public class EmptyParameter
{
public override string ToString()
{
return String.Empty;
}
}
然后:
@Html.ActionLink("Action",
"Controller",
new { item1 = new EmptyParameter(), item2 = "value" });
这篇关于MVC Html.ActionLink从URL删除空的querystring参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!