如何URL连接在ASP .NET MVC code参数 [英] How to URL encode parameters in ASP .NET MVC
问题描述
我有以下的code在我看来:
I have the following code in my view:
<%= Html.ActionLink(
"View item",
"Index",
"Items",
new
{
itemName = Model.ItemName
},
null) %>
我有一个问题,当项目名称包含一个尖锐的(#)或百分号(%)。
I have a problem when the item name contains a sharp (#) or the percent symbol (%).
-
在项目名称为
的名字#以#锐#
,控制器接收只有名称的前半部分,直到第一次急剧(只接收名
)。
When the item name is
"name#with#sharp#"
, the controller receives only the first part of the name until the first sharp (only receives"name"
).
在项目名称是的名称%和%%的
我得到一个错误: HTTP错误400 - 错误请求
When the item name is "name%with%percent"
I get an error: HTTP error 400 - Bad request.
我不知道这是URL编码的问题,因为它与其他抵触的字符,如作品:
I not sure if this is a problem with the URL encoding, because it works with other conflictive chars such as:
;
=
+
,
~
[blank]
你知道我怎么能解决这个问题?
Do you know how could I address this issue?
先谢谢了。
推荐答案
我假设你有一个路线设置和您的网址看起来像这样:
I'm assuming you have a route setup and your url looks something like this:
<一个href=\"http://localhost/Items/Index/name%25with%25percent\">http://localhost/Items/Index/name%25with%25percent - (这将炸毁)
,而不是这样的:
<一个href=\"http://localhost/Items/Index/?itemName=name%25with%25percent\">http://localhost/Items/Index/?itemName=name%25with%25percent - (查询字符串是确定)
http://localhost/Items/Index/?itemName=name%25with%25percent - (query string is ok)
因此,一个办法是删除您路线ITEMNAME属性(在你RouteCollection),这样Html.ActionLink将使用ITEMNAME作为查询字符串参数渲染的URL。
So an option would be to remove the "itemName" property from your route (in your RouteCollection) so that Html.ActionLink will render the Url using itemName as a QueryString parameter.
由于@Priyank说,这个问题是因为ITEMNAME是URL(而不是查询参数)的一部分,它包含非法字符。
As @Priyank says, the problem is because the itemName is part of the Url (not a QueryString parameter) and it contain illegal characters.
这篇关于如何URL连接在ASP .NET MVC code参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!