本文介绍了URL包含ASP.Net Core参数的'+'时出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当参数的空格编码为 + 而不是%20 时,我的服务器返回404错误.我不明白为什么.路线的格式为

My server is returning a 404 error when a parameter has a space encoded as a + instead of %20. I don't understand why.The route is of the form

[Route("/Search/PRM1/{prm1}/PRM2/{prm2}/PRM3/{prm3}")

我的第一个问题是如何让服务器理解这种URL?

My 1st question would be how to ask the server to understand this kind of URL?

https://example.com/Search/PRM1/prm1%20value/PRM2/prm2+value/PRM3/prm3%20value

我的第二个问题是如何使用 + 而不是%20 来自动生成更易于阅读的网址?SEO有区别吗?

My 2nd question would be how to automatically generate urls with + instead of %20, which are easier to read? Is there a difference regarding SEO?

URL在服务器端的cshtml中生成:

URLs are generated on server side in cshtml:

<a href="/Search/PRM1/@prm1/PRM2/@prm2/PRM3/@prm3">link</a>

使用prmX变量清除"文本(包括空格).使用 @ System.Net.WebUtility.UrlEncode(prm2)而不是 @ prm2

with prmX variables "clear" text (including spaces). The + URL has been generated when using @System.Net.WebUtility.UrlEncode(prm2) instead of @prm2

我已经检查了下面的链接,但是该解决方案似乎不适用于ASP.Net Core: WebAPI路由404出现尾随时网址中的空格

I have checked the link below, but the solution does not seem to work with ASP.Net Core:WebAPI route 404's when there is a trailing space in the URL

推荐答案

如果您要提供的搜索服务或字段可能涉及特殊字符或. + / \ 总是最好将它作为 Query String

If you're provding a searching service or a field that might involve special characters or . , + , / , \ it is always better to send it as a Query String

[Route("/Search/PRM1/{prm1}/PRM2/{prm2}/PRM3?prm3=your value here")

这篇关于URL包含ASP.Net Core参数的'+'时出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:54