2

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}")

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

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?

URLs are generated on server side in cshtml:

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

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

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

Community
  • 1
  • 1
Jean
  • 4,911
  • 3
  • 29
  • 50
  • 1
    [(Please) Stop Using Unsafe Characters in URLs](https://perishablepress.com/stop-using-unsafe-characters-in-urls/). If you want your URL to be "human readable", you should replace any spaces with `-` (dash, not underscore - which is also SEO-friendly). If not, then encoding is the correct approach because however you slice it a URL *must* be machine-readable. – NightOwl888 Jan 21 '17 at 19:45
  • @NightOwl888 Your link defines safe characters as `Alphanumerics [0-9a-zA-Z], special characters $-_.+!*'(),, and reserved characters used for their reserved purposes` therefore the '+' should be included. Is %20 SEO friendly? If so, I may let it with the %20.. – Jean Jan 21 '17 at 21:21
  • Space (`%20`) is not a safe character. Microsoft fixed the broken behavior of ASP.NET that converted a space to `+`, which now correctly encodes it as `%20`. If you need to use a space (because it is a search feature), then add your parameters to the query string and encode the space as `%20`. – NightOwl888 Jan 22 '17 at 03:10

1 Answers1

0

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")
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
  • I don't see why `+` or `.` would be special characters. I understand for `\ ` and `/ `, but not the others. And query string is not an option as the service is already deployed and needs backward compatibility (and no plans to change the urls structures anyway :) ). The search field does not involve these characters (even if the user can enter them, but not supposed to), they have been generated by the UrlEncode function (I removed it, but I don't understand why). – Jean Jan 21 '17 at 21:18
  • The problem with the dot is that it involves the structure of the domain/subdomain when it is in the direct path. The plus is a special char as well. – Ramy M. Mousa Jan 21 '17 at 22:50
  • Anyway, I understand your point, however that does not explain why having a "+" in a URL parameter leads to 404 in IIS / ASP.Net.Core... :( – Jean Jan 22 '17 at 00:07
  • 2
    Might be connected to this https://blogs.iis.net/thomad/iis7-rejecting-urls-containing – Skorunka František Nov 11 '17 at 14:35