How can I send a GET request containing a colon to an ASP.NET MVC2 controller?

This works great:

GET /mvc/Movies/TitleIncludes/Lara%20Croft 

When I send a query containing a colon, for example:

 GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb 

... it generates a 400 error. The error says that ASP.NET has detected invalid characters in the URL.

If I try to escalate the url, the request will look like this:

 GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb 

... and that also gives me error 400.

If I replaced the colon with |

 GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb 

.., which was also reassigned as illegal, this time with a 500 error. Message: Illegal characters in transit.

URL escaping that | leads to the same error.


I really, really don't want to use the querystring parameter.


connected:
Sending URLs / Paths to ASP.NET MVC Controller Actions

+4
source share
3 answers

I found that the URL encoding does not work, but the custom encoding did.
I think ASPNET MVC uses the file system for parsing and routing, because a character in a URL that is not legal in the file system causes a 500 or 400 error.

So what I did was, replace the colons with unicode ยกSymbol in the JavaScript side, and then do the opposite in action. eg:

browser:

 function myEscape(s){ return s.replace(':', '%C2%A1').trim(); } 

in action, call this conversion before using the argument:

 private string MyCustomUnescape(string arg) { return arg.Replace("ยก", ":"); } 

The same approach works for slashes - just select a different Unicode character. Of course, if your string arguments themselves are Unicode, then you will have to use non-printable characters for the "encoded" forms.

+3
source

If SEO is not a problem, you can use base64 and then urlencode. After the first step, each character will be easily encoded. Decoding in .NET is as simple as using the helper in System.Web.HttpUtility and System.Convert.

+1
source

Related answers here: fooobar.com/questions/280762 / ...

Use the question mark and ampersands for the arguments and the URL to encode the arguments.

Example: GET / mvc / Movies / TitleIncludes? title = Lara% 20Croft% 3A% 20Tomb

I agree that it would be nice to encode things in the url too, but there probably is a good reason for this.

0
source

Source: https://habr.com/ru/post/1306245/


All Articles