无法在 URL 重写 ASP.Net MVC 中的 RouteConfig 中编码路由数据值
Cannot encode route data value in URL rewriting in RouteConfig in ASP.Net MVC
我正在开发一个 ASP.NET MVC 网站。在我的网站中,我正在 url 重写 RouteConfig 以获得更好的 SEO。但我对此有疑问。问题在于编码 URL 路由数据值。
我在 RouteConfig 中重写了一个 URL 是这样的:
routes.MapRoute(
"",
"places/category/{categoryName}/{category}",
new { controller = "Item", action = "List", categoryName = "", category = 0 },
new string[] { "AyarDirectory.Web.Controllers" }
);
在我的 HTML 中,我创建了一个锚点 link,如下所示:
<a href="@Url.Content("~/places/category/" + HttpUtility.UrlEncode(category.Name) + "/" + category.Id)">
Content
</a>
当我点击它时,如果名称不包含“+”,“%”等字符,编码后,它总是显示404。它的值不包含space和/(所以编码后没有像 % 和 +) 这样的字符,它工作正常。
我在行动中发现了价值,例如:
HttpUtility.UrlDecode(value)
为什么不起作用?如何编码路由数据值?即使该值包含 space,如果我不对值进行编码,它也能正常工作。结果 URL 将与 space.
类似
http://localhost:50489/places/guest house/4
所以如果我使用没有编码的值,它的值包含“/”,就会出错。为什么会发生这种情况,我该如何编码 URL 路由数据值?
您永远不应将 URL 硬编码到您的应用程序中。使用路由(而不是 URL 重写)的全部意义在于它允许您将所有 URL 创建逻辑存储在一个地方(即路由 table)。
路由不仅确保您拥有可维护的 URLs,它还提供了一种机制,可以自动 URL 对段进行编码。与其使用 Url.Content
,不如使用 Html.ActionLink
、Html.RouteLink
或 Url.Action
来使用 MVC 构建 URL。
Html.ActionLink
@Html.ActionLink("Content", "List", "Item",
new { categoryName = category.Name, id = category.Id }, null)
Html.RouteLink
如果需要通过名称调用特定路由,可以使用Html.RouteLink
.
@Html.RouteLink("Content", "TheRoute", new {
controller = "Item",
action = "List",
categoryName = category.Name,
id = category.Id })
但是,您需要为路线命名。
routes.MapRoute(
"TheRoute",
"places/category/{categoryName}/{category}",
new { controller = "Item", action = "List", categoryName = "", category = 0 },
new string[] { "AyarDirectory.Web.Controllers" }
);
NOTE: You can also use RouteLink
without supplying a route name. In this case it works similar to ActionLink
. This can be useful if you already have a RouteValueDictionary
instance with route values in it and just want to build a hyperlink to those route values.
Url.Action
如果需要在锚标签内添加HTML,可以使用Url.Action
.
<a href="@Url.Action("List", "Item", new { categoryName = category.Name, id = category.Id })">
HTML Content Here...
</a>
我正在开发一个 ASP.NET MVC 网站。在我的网站中,我正在 url 重写 RouteConfig 以获得更好的 SEO。但我对此有疑问。问题在于编码 URL 路由数据值。
我在 RouteConfig 中重写了一个 URL 是这样的:
routes.MapRoute(
"",
"places/category/{categoryName}/{category}",
new { controller = "Item", action = "List", categoryName = "", category = 0 },
new string[] { "AyarDirectory.Web.Controllers" }
);
在我的 HTML 中,我创建了一个锚点 link,如下所示:
<a href="@Url.Content("~/places/category/" + HttpUtility.UrlEncode(category.Name) + "/" + category.Id)">
Content
</a>
当我点击它时,如果名称不包含“+”,“%”等字符,编码后,它总是显示404。它的值不包含space和/(所以编码后没有像 % 和 +) 这样的字符,它工作正常。
我在行动中发现了价值,例如:
HttpUtility.UrlDecode(value)
为什么不起作用?如何编码路由数据值?即使该值包含 space,如果我不对值进行编码,它也能正常工作。结果 URL 将与 space.
类似http://localhost:50489/places/guest house/4
所以如果我使用没有编码的值,它的值包含“/”,就会出错。为什么会发生这种情况,我该如何编码 URL 路由数据值?
您永远不应将 URL 硬编码到您的应用程序中。使用路由(而不是 URL 重写)的全部意义在于它允许您将所有 URL 创建逻辑存储在一个地方(即路由 table)。
路由不仅确保您拥有可维护的 URLs,它还提供了一种机制,可以自动 URL 对段进行编码。与其使用 Url.Content
,不如使用 Html.ActionLink
、Html.RouteLink
或 Url.Action
来使用 MVC 构建 URL。
Html.ActionLink
@Html.ActionLink("Content", "List", "Item",
new { categoryName = category.Name, id = category.Id }, null)
Html.RouteLink
如果需要通过名称调用特定路由,可以使用Html.RouteLink
.
@Html.RouteLink("Content", "TheRoute", new {
controller = "Item",
action = "List",
categoryName = category.Name,
id = category.Id })
但是,您需要为路线命名。
routes.MapRoute(
"TheRoute",
"places/category/{categoryName}/{category}",
new { controller = "Item", action = "List", categoryName = "", category = 0 },
new string[] { "AyarDirectory.Web.Controllers" }
);
NOTE: You can also use
RouteLink
without supplying a route name. In this case it works similar toActionLink
. This can be useful if you already have aRouteValueDictionary
instance with route values in it and just want to build a hyperlink to those route values.
Url.Action
如果需要在锚标签内添加HTML,可以使用Url.Action
.
<a href="@Url.Action("List", "Item", new { categoryName = category.Name, id = category.Id })">
HTML Content Here...
</a>