URL 中的变量带有斜杠,它是 URL 编码的,但 IIS 仍然可以看到斜杠?

nii*_*ico 5 c# asp.net-mvc-routing

我有一个控制器,其方法有一个在路由中传递的变量 - 控制器:

\n\n
[Route("cont/{MyParameter}")]\npublic ActionResult Index(string MyParameter)\n{\n...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我像这样在 URL 中传递一个值 (abcdef) 时,就可以了:

\n\n
http://localhost/my-website/cont/abcdef\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,当我传递带有编码斜杠的一个时,它失败了,就好像斜杠没有编码一样,例如:

\n\n
http://localhost/my-website/cont/abc%2fdef\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在 IIS 中收到“无法找到资源”的消息 - 请求的 URL 显示在以下内容中:

\n\n
Requested URL\n\xc2\xa0\xc2\xa0\xc2\xa0http://localhost:80/f-website/cont/abc/def\n
Run Code Online (Sandbox Code Playgroud)\n\n

它似乎添加了斜杠 - 尽管它们已被编码。

\n\n

但地址栏仍然显示原始 URL,其中包含 %2f。

\n\n

如何将 / 作为参数传入?我认为 URL 编码是这里的解决方案,但它显然没有效果。

\n\n

我在谷歌上找不到任何关于这个具体问题的信息。

\n

0lu*_*sz0 2

我发现的最佳解决方案是使用通配符:

    [HttpGet]
    [Route("my-action-url/{*parameter}")]
    public IActionResult MyAction(string parameter)
    {
      // Some code here
    }
Run Code Online (Sandbox Code Playgroud)