404 error when URL contains a '+' for a parameter with ASP.Net Core - asp.net-mvc

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:
link
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

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

Related

ASP.NET Core MVC: Problem with routing argument that contains %2F (forward slash)

I have the following routing attribute in place:
[HttpGet("{id}/foo")]
My id is a base64 encoded value, and, as such, may contain a forward slash. I'm encoding this forward slash using %2F, however, it seems like ASP.NET Core MVC decodes the URL, then matches routes, as I'm getting a 404 if my id contains %2F.
Is there any way I can allow %2F in my id?
I know there are some answers out there (e.g. How to match web api 2 route with forward slashes in request parameters?), but with all of them, the parameter in question, is always the last, and only then you can use a wildcard (?).

why routes with % character in routes will display blank?

in rails when the routes or url has % will display blank page.
e.g www.domain.com/% ---- will display blank page
I check also some website like github.com/% but still display in blank page.
It is related to URL Encoding. In short.
URL encoding converts characters into a format that can be transmitted
over the Internet.
Then we have some basic terminology you might wish to know
URL - Uniform Resource Locator
Web browsers request pages from web servers by using a URL.
The URL is the address of a web page, like https://stackoverflow.com/
URL Encoding (Percent Encoding)
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has
to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a "%" followed by
two hexadecimal digits. URLs cannot contain spaces. URL encoding
normally replaces a space with a plus (+) sign or with %20.
Well, so when your route contains only %, it was recognized as the encoded string but it missed two hexadecimal digits followed so you will always get a blank page or a custom error page depends on how your site was configured.
Because browser will request to https://stackoverflow.com/% and it is not existing.
How to handle it
Basically, If your URL is in unwell format when requesting to the server. It will return you an HTTP 400 error code which means
10.4.1 400 Bad Request
The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without
modifications.
So what we need to do is just doing configuration on the server to redirect to a custom error page (in this case IIS Web Server of ASP.NET website). Example can found here

ASP.NET MVC Colon in URL

I've seen that IIS has a problem with letting colons into URLs. I also saw the suggestions others offered here.
With the site I'm working on, I want to be able to pass titles of movies, books, etc., into my URL, colon included, like this:
mysite.com/Movie/Bob:The Return
This would be consumed by my MovieController, for example, as a string and used further down the line.
I realize that a colon is not ideal. Does anyone have any other suggestions? As poor as it currently is, I'm doing a find-and-replace from all colons (:) to another character, then a backwards replace when I want to consume it on the Controller end.
I resolved this issue by adding this to my web.config:
<httpRuntime requestPathInvalidCharacters=""/>
This must be within the system.web section.
The default is:
<httpRuntime requestPathInvalidCharacters="<,>,*,%,&,:,\,?"/>
So to only make an exception for the colon it would become
<httpRuntime requestPathInvalidCharacters="<,>,*,%,&,\,?"/>
Read more at: http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestpathinvalidcharacters.aspx
For what I understand the colon character is acceptable as an unencoded character in an URL. I don't know why they added it to the default of the requestPathInvalidCharacters.
Consider URL encoding and decoding your movie titles.
You'd end up with foo.com/bar/Bob%58The%20Return
As an alternative, consider leveraging an HTML helper to remove URL unfriendly characters in URLs (method is URLFriendly()). The SEO benefits between a colon and a placeholder (e.g. a dash) would likely be negligable.
One of the biggest worries with your approach is that the movie name isn't always going to be unique (e.g. "The Italian Job"). Also what about other ilegal characters (e.g. brackets etc).
It might be a good idea to use an id number in the url to locate the movie in your database. You could still include a url friendly copy of movie name in your url, but you wouldn't need to worry about getting back to the original title with all the illegal characters in it.
A good example is the url to this page. You can see that removing the title of the page still works:
ASP.NET MVC Colon in URL
ASP.NET MVC Colon in URL
Colon is a reserved and invalid character in an URI according to the RFC 3986. So don't do something that violates the specification. You need to either URL encode it or use another character. And here's a nice blog post you might take a look at.
The simplest way is to use System.Web.HttpUtility.UrlEncode() when building the url
and System.Web.HttpUtility.UrlDecode when interpreting the results coming back. You would also have problems with the space character if you don't encode the value first.

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

This works fine:
GET /mvc/Movies/TitleIncludes/Lara%20Croft
When I submit a request that contains a colon, like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb
...it generates a 400 error. The error says ASP.NET detected invalid characters in the URL.
If I try url-escaping, the request looks like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb
...and this also gives me a 400 error.
If I replace the colon with a | :
GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb
..that was also rejeted as illegal, this time with a 500 error. The message: Illegal characters in path.
URL-escaping that | results in the same error.
I really, really don't want to use a querystring parameter.
related:
Sending URLs/paths to ASP.NET MVC controller actions
I found that URL encoding did not work, but custom encoding did.
I guess ASPNET MVC uses the filesystem to do the parsing and routing, because a character in the URL that is not legal in the filesystem, causes a 500 or 400 error.
So what I did was replace colons with the unicode
¡ character in the javascript side, and then do the converse in the action. like this:
browser:
function myEscape(s){
return s.replace(':', '%C2%A1').trim();
}
in the action, call this conversion before using the argument:
private string MyCustomUnescape(string arg)
{
return arg.Replace("¡", ":");
}
The same approach works for slashes - just pick a different unicode character. Of course if your string arguments themselves are unicode, then you'll have to use non-printable characters for the "encoded" forms.
If SEO is not a problem you may use base64 and then urlencode that. After the first step every character you'll have will be easily encoded. Decoding in .NET is as easy as using the helper in System.Web.HttpUtility and System.Convert.
Similar answered here:
https://stackoverflow.com/a/12037000/134761
Use question mark and ampersands for arguments and URL encode the arguments.
Example: GET /mvc/Movies/TitleIncludes?title=Lara%20Croft%3A%20Tomb
I agree it would be nice to encode things into the url as well, but there is probably a good reason not to.

Bad request 400 for HttpUtility.UrlEncoded URL segments

So, if there are url encoded segments in my apps MVC url, IIS throws a BAD REQUEST 400.
e.g.
http://u.lasoo.com.au/Offer/*9289--750W-Generic-ATX12V-Power-Supply-%252449dot99/6355
<--- the '*' and '%' are causing this error.
Notice that http://u.lasoo.com.au/Offer/The-Giant-Good-As-Gone-7-Day-Sale/6354 works fine.
What's the correct way to convert an arbitrary string into an accepted MVC URL segment?
UPDATE: the URl segment should resemble the original string. Base64 encoding completely transformed the string.
Instead of passing the info in the url you can pass it as a get parameter. Like this:
http://u.lasoo.com.au/Offer/?id=*9289--750W-Generic-ATX12V-Power-Supply-%252449dot99/6355
Have you tried UrlEncode? MSDN
Try a string replace to strip out or substitute symbols ":", "%", "*", "/" - any symbols illegal within a folder name. They seem to screw up everything royally and appear to be a design weakness of the URL routing system.

Resources