nii*_*ico 5 c# asp.net-mvc-routing
我有一个控制器,其方法有一个在路由中传递的变量 - 控制器:
\n\n[Route("cont/{MyParameter}")]\npublic ActionResult Index(string MyParameter)\n{\n...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我像这样在 URL 中传递一个值 (abcdef) 时,就可以了:
\n\nhttp://localhost/my-website/cont/abcdef\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当我传递带有编码斜杠的一个时,它失败了,就好像斜杠没有编码一样,例如:
\n\nhttp://localhost/my-website/cont/abc%2fdef\nRun Code Online (Sandbox Code Playgroud)\n\n我在 IIS 中收到“无法找到资源”的消息 - 请求的 URL 显示在以下内容中:
\n\nRequested URL\n\xc2\xa0\xc2\xa0\xc2\xa0http://localhost:80/f-website/cont/abc/def\nRun Code Online (Sandbox Code Playgroud)\n\n它似乎添加了斜杠 - 尽管它们已被编码。
\n\n但地址栏仍然显示原始 URL,其中包含 %2f。
\n\n如何将 / 作为参数传入?我认为 URL 编码是这里的解决方案,但它显然没有效果。
\n\n我在谷歌上找不到任何关于这个具体问题的信息。
\n我发现的最佳解决方案是使用通配符:
[HttpGet]
[Route("my-action-url/{*parameter}")]
public IActionResult MyAction(string parameter)
{
// Some code here
}
Run Code Online (Sandbox Code Playgroud)