gpt4 book ai didi

c# - 如何在 ASP.NET MVC Razor 中编码 '/'

转载 作者:太空狗 更新时间:2023-10-29 23:37:15 26 4
gpt4 key购买 nike

Controller :

public ActionResult Tool(string id)
{
// Code goes here . .
}

查看:

<a href="/Home/@item.Type/@Url.Encode(item.Title)" id="toolTitleLink">@item.Title</a>

从上面的代码 @item.Title 可以有特殊字符,比如 '/' 示例链接是 http://localhost:39727/Home/Tool/C+Compiler+For+当我尝试导航到该链接 Tool Controller 时,+Pic10%2f12%2f16+Mcus 未被调用。我使用了 @Url.Encode 但仍然没有调用 Controller

最佳答案

不幸的是,即使你使用 System.Uri.EscapeDataString而不是像这样的 Url.Encode:

<a href="/Home/@item.Type/@System.Uri.EscapeDataString(item.Title)" id="toolTitleLink">@item.Title</a>

呈现的页面将对斜杠进行编码(查看页面的“查看源代码”),浏览器仍会对其进行解码。

据我所知,您有两个选择:

  1. 使用不同的字符 - 自行转义(可以这么说 ;)) - 例如使用 tilda (~) 或其他任何字符 URL valid character你想要的,以替换正斜杠。

  2. 使用 a catch-all at the end 为操作创建一条特殊路线并从 Action 中解析事物。

关于c# - 如何在 ASP.NET MVC Razor 中编码 '/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37738078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com