【发布时间】:2013-02-20 10:39:58
【问题描述】:
如何在一个 URL 中允许多个句点?
示例网址:
http://mylocalhost:6968/MyHome/Edit/ABCD.Applications.ClinicalData%257cCache_Timeout
我试过了:
各种网址编码:
@{
//string encodedItem = Url.Encode(item.Key);
//string encodedItem = HttpUtility.UrlEncode(item.Key);
string encodedItem = Server.UrlEncode(item.Key);
}
Adding 路由处理程序:
<add name="UrlRoutingHandler" type="System.Web.Routing.UrlRoutingHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" path="Remote.mvc/*" verb="GET"/>
Modifyingweb.config system.web
<httpRuntime relaxedUrlToFileSystemMapping="true" />
这些都没有更正示例 URL。
相关代码
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Key)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
@{
//string encodedItem = Url.Encode(item.Key);
//string encodedItem = HttpUtility.UrlEncode(item.Key);
string encodedItem = Server.UrlEncode(item.Key);
}
@Html.ActionLink("Edit", "Edit", new { id = encodedItem }) |
@Html.ActionLink("Details", "Details", new { id = encodedItem }) |
@Html.ActionLink("Delete", "Delete", new { id = encodedItem })
</td>
</tr>
}
我正在点击编辑链接。
我也试过
@Html.ActionLink("Edit", "Edit", new { id = item.Key })
它的 html 输出为:
a href=/MyHome/Edit/ABCD.Applications.ClinicalData%7CCache_Timeout
单击此链接会导致错误:
HTTP Error 404.0
我在Controller.Edit 中设置了一个断点。断点永远不会被击中。任何时候我有一个表单的网址:
http://mylocalhost:6968/MyHome/Edit/ABCD.X
如果 X 是 . 之后的任何内容,则会发生此错误。如果我删除 X,就会触发断点。
【问题讨论】:
-
那是哪个 Url.encode?是 HttpUtility.UrlEncode 还是 HttpServerUtility.UrlEncode ? HttpUtility.UrlEncode 似乎可以处理 url 的查询参数/路径部分中的标点符号。在此处查看文档/备注部分:msdn.microsoft.com/en-us/library/4fkewx0t.aspx
-
@StevenMagana-Zook - 问题已更新。
-
你如何使用它弄乱的 URL?如果您改为使用 Uri 类构建最终 url 会怎样?例如:Uri uri = new Uri("mylocalhost:6968/MyHome/Edit/…);
-
@StevenMagana-Zook -
item.key来自Model,它来自数据库中的一个值。我也会插入相关代码。 -
你试过
@Html.ActionLink("Edit", "Edit", new { id = item.Key })吗?如果是,呈现的href属性是什么?单击此锚点时的结果是什么(您是否收到错误,如果是,请说明您遇到了什么错误)?
标签: c# asp.net-mvc-4