11

I use HttpUtility.UrlEncode to encode any value that is used in a route.

I already solved an issue with encoding forward slashes. The new problem I have now is with spaces. A space is encoded as + .

This works on the VS integrated Webserver, but I have an issue with it in IIS7 on Windows Server 2008. If I have the URL http://localhost/Home/About/asdas+sdasd

I get the error 404.11 - Request contains double escape sequence.

I know I can just replace the space by "%20", but I dont want to care about propper encoding myself. Is there any ready to use UrlEncoder for MVC out there?

Mathias F
  • 15,906
  • 22
  • 89
  • 159

3 Answers3

20

' ' encoded to %20 use HttpUtility.UrlPathEncode.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
takepara
  • 10,413
  • 3
  • 34
  • 31
  • This may have changed, but the documentation for UrlPathEncode now says: `Do not use; intended only for browser compatibility. Use System.Web.HttpUtility.UrlEncode(System.String).` – AaronHolland Sep 24 '18 at 02:05
  • However, if you use UrlEncode you still get spaces encoded as +. And UrlPathEncode does not encode # either, so they are both broken. – AaronHolland Sep 24 '18 at 02:13
0

Any URL Encoding is most often designed to work on the path component of the url, the reason because different schemes have different characters in the safe list. Look for your libraries urlencoder and just use it in the path and above portion of the url.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
0
@HttpUtility.UrlPathEncode(path)

UrlPathEncode just encodes the path of the Url, rather than encoding the whole Url.

Dheeraj Palagiri
  • 1,829
  • 3
  • 23
  • 46