I currently have this route defined (among others):
"{controller}/{action}/{id}/{designation}" being:
"id" my primary key
"designation" only used for SEO and not taken into account.
now my problem is:
"http://server/Home/Index/1/teste" works but "http://server/Home/Index/1/teste " with a space in the end doesn't.
IIS is giving me a 404 and mvc is not even starting for this request.
Anyone experienced this behavior? Anything I need to change?
With best regards
Space cannot be used as a plain text character in a url. You have to encode it as:
%20
E.g.
http://www.testDomain.com/test%20page
Space is an invalid character in URL's. The browser should not even send it.
If you're calling this in code, try using HttpUtility.UrlEncode( path ) before sending / redirecting.
Look at this post:
"The resource cannot be found." error when there is a "dot" at the end of the url
it talks about similar problem with '.' (dot) character at the end of a url. Think it's the same problem as yours.
Related
I have an MVC web application. The URL for a particular area is coming in as:
http://localhost/General/Bpa%3fapplication%3dTrf%23/GeneralInputs
This causes a "The resource cannot be found." error. However, if I change the URL to
http://localhost/General/Bpa?application=Trf#/GeneralInputs
then everything works. I can see from using some route debugging tricks that the controller in the first case is: "Bpa?application=Trf#", whereas the second one is: "Bpa", which is correct. How can I account for this or substitute for the encoded characters?
The encoding of the first URL is wrong. If you look at RFC 3986 you will find in 2.4 the paragraph
When a URI is dereferenced, the components and subcomponents
significant to the scheme-specific dereferencing process (if any)
must be parsed and separated before the percent-encoded octets within
those components can be safely decoded, as otherwise the data may be
mistaken for component delimiters.
That means the URL is decomposed by unencoded characters (in this case the ? matters). If the encoded string #3f is used, then the framework would have to look for a controller named "Bpa?application=Trf#" and not "Bpa". Thus a 404 / resource not found is returned.
You should not fix it on the server side; you will have to change the place where the wrong url http://localhost/General/Bpa%3fapplication%3dTrf%23/GeneralInputs is generated.
You're going to want to use this on your url:
string fixedUrl = System.Uri.UnescapeDataString(yourUrlHere);
Hope that works out for you!
I'm trying to get my URL to escape but it's not working properly. Ironically, on my MacBook when I execute this part of code
Uri url = new Uri("http://www.example.com/?i=123%34", true);
// it returns http://www.example.com/?i=123%34 which is exactly what I want.
The problem is that my IDE says it's obsolete and it does not work on my Windows machine. It's the exact same project, and IDE. So I tried to find a solution, which someone suggested
Uri uri = new Uri(Uri.EscapeUriString("http://www.example.com/?i=123%34"));
// this returns http://www.example.com/?i=123%2534 which is what I DONT want.
So how do I approach this issue? I looked all over the web and I can't find any solutions. I need to know how to properly escape this URL. The second method posted above does not work like the first method above.
I verified the GET requests via Fiddler, so everything is indeed happening.
Update:
Again, I need the server to receive the URL exactly how the string is declared. I want the server to handle the conversion. I cannot substitute %25 for the % symbol. It MUST be received exactly how I the string is declared. Additionally, "http://www.example.com/?i=1234" is NOT what I want either.
The problem is with the configuration of your web server on Windows, that allows double escaping. Your original URL is http://www.example.com/?i=123%34, which when unescaped, becomes http://www.example.com/?i=1234.
Your web server on Windows, on the other hand, escapes the % character again instead of unescaping %34. Thus, it turns into http://www.example.com/?i=123%2534.
This is why you should not use characters like % in the URL before it gets escaped.
Edit -
I typed the following two URLs in Firefox to see how the parameters are received on the server.
The value of i in http://www.example.com/?i=123%34 is 1234.
The value of i in http://www.example.com/?i=123%2534 is 123%34
If the server must receive the % character, it must be escaped in order for it to be dispatched over HTTP. There's literally no other way to send it over the wire. If you don't escape the % character, it will be treated as an escape sequence along with 34 and automatically turn into 4 on the server.
If your network inspector shows you unescaped text in the request, it's because it's prettifying the URL before displaying it to you.
If you are okay with the string reading ht tp://www.example.com/?i=1234, you can try
Uri url = new Uri(Uri.UnescapeDataString("http://www.example.com/?i=123%34"));
How do I properly encode the following url?
http://mysite/myapp/content/Documents/Opening Leave Case for Supervisors-WC&TW.pdf
When I view the url after url encoding the file name I get this error:
"The request filtering module is configured to deny a request that contains a double escape sequence."
I'm certain that the problem lies with the ampersand in the file name but am unsure how to properly deal with it.
Here is what I have so far (that is not working):
string.Concat( "~/Content/Documents/", HttpUtility.UrlEncode( ContentLocation ) );
You might try HttpUtility.UrlPathEncode which transforms spaces to "%20" instead of "+". Other than that I see nothing wrong with what you're doing. http://msdn.microsoft.com/en-us/library/system.web.httputility.urlpathencode(v=vs.110).aspx
Also you'll need to post the context of where this error is occurring because the error doesn't occur on the conversion obviously.
I need to URL encode some periods since I have to pass some document path along and it is like this
http://example.com/test.aspx?document=test.docx
So test.docx is causing me an error of an illegal character. So I need to change it to
. --> %2E
I tried to use Server.UrlEncode
string b = Server.UrlEncode("http://example.com/test.aspx?document=test.docx");
but I get
"http%3a%2f%2fexample.com%2ftest.aspx%3fdocument%3dtest.docx"
So do I have to use like a string replace and do it manually and replace all periods with that code?
This is a really old question, but I ran into this searching for a similar problem. I stuck a "/" onto the end of my url's with periods in them and it got around the problem.
The period there isn't he problem (given that %2E doesn't solve the problem). A period is a perfectly valid URL character whatever the problem is it's not the period. Check the stack trace of the error being throw or post the complete error details.
And you shouldn't be URL encoding the entire path. Only the query string parameter value.
string b = "http://example.com/test.aspx?document=" + Server.UrlEncode("test.docx");
Are you still getting the error if you try it that way?
I wouldn't touch SharePoint with a ten foot pole. However, escaping the period wouldn't necessarily stop SharePoint from doing it's shenanigans. But I guess you should at least try it.
Server.UrlEncode("test.docx").Replace(".", "%2E");
In my asp.net mvc application I created the following link:
http://localhost:2689/en/Formula.mvc/351702++LYS+GRONN+5G+9%252f2++fds
I get error 400 (bad request).
I think it blocks at the %25 (forward slash).
What am I doing wrong?
--EDIT 3--
I tried not encoding anything at all but rather rely on the default encoding of Url.RouteUrl().
It seems that this doesn't encode the "/" for some reason.
If I encode it myself first, I end up with the doubel encoded %252f. This gives me a bad request for some reason..
Why?!
--EDIT 2--
I generated the last part of the URI as follows:
Take the id.toString
Take the HttpUtility.UrlEncode(name)
Take the HttpUtility.UrlEncode(code)
String.Format("{0}--{1}--{2}") with the values from the previous parts
Add it as a parameter to Url.RouteUrl()
After that my action gets this parameter again, splits it at -- and HttpUtility.Decode() the values back.
I do it this way because the two last parameters are optional, but functional parameters. IF they are defined in a previous step, they have to be carried along to the other pages.
Less abstract: A color can have multiple names, but if a user selected it by a particular name, it should be kept throughout all the other pages.
--EDIT 1--
It also looks like HttpUtility.UrlEncode() and Url.Encode() return different results :S
If I don't encode the "/", it acts as a separator=>no luck there.
If I encode it with Url.Encode() I end up with %2F => Code 400
If I encode it with HttpUtility.UrlEncode() I end up with %25 => code 400
Because 400 doesn't even let it through to asp.net-mvc, the route debugger is of no use :(
I was there a couple of days ago. If you can accept unreadable route-values in the URL try this:
URL-encoded slash in URL
%25 is actually encoded "%", so %252f is encoded "%2f".
%2f (encoded "/") is not allowed in URL unless you explicitly allow it in webserver's configuration.
Have you run the Routing debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
I haven't looked too much at the encoding - but note that if this is to be stored somewhere (or acted upon in some way), then a POST would be more appropriate. If the text on the right is actually representative of the data with id 351702 (a vanity url, much like /665354/whats-wrong-with-my-url-encoding), then you should humanize the text. Much as the spaces have been removed from the above. It is also common to have this as a separate level in the route that is simply discarded.
Generally, MVC urls should be comprehensible.
W3Schools works fine: http://www.w3schools.com/TAGS/html_form_submit.asp?text=hello/world
Here's the URL encoding reference: http://www.w3schools.com/TAGS/ref_urlencode.asp
You can't use a forward slash as a value in the URL. Here is a nice post about creating browser and SEO friendly URLS => http://www.dominicpettifer.co.uk/displayBlog.aspx?id=34
[Edit]
Whenever you create a route you associate it with a URL pattern (The default pattern is {controller}/{action}/{id}). And in this url pattern you are supposed to use the forward slash to separate different tokens. Hope that helps