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");
Related
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"));
I'm using ResourceLoader.GetString to get string resources from my .resw file. I'm able to retrieve resources without a dot in the key, but ones with a dot come back as an empty string. For example:
var rl = new Windows.ApplicationModel.Resources.ResourceLoader();
rl.GetString("HelpText"); // gets the string "Help"
rl.GetString("Forget.Text"); // gets "", even though it's defined in resw file as "Forgotten"
I've tried replacing the dot with various other characters:
rl.GetString("Forget_Text");
rl.GetString("Forget:Text");
rl.GetString("Forget-Text");
No luck. All the examples on MSDN skilfully avoid mentioning this little issue, so I'm a bit stumped. Can anyone help?
It is actually accessed via a forward-slash:
rl.GetString("Forget/Text");
How would you deal with a final dot? For example:
ResourceManager.GetString("My string.")
The forward slash works fine for dots embedded within the string, but not a final dot. Eg, this results in a compile error (error : PRI175: 0x80070057 - Processing Resources failed with error: The parameter is incorrect.):
ResourceManager.GetString("My string/")
The only way I've found around that is to substitute something for the dot before putting it into the resw file. For example, this looks weird but it works:
ResourceManager.GetString("My string0x2e")
Thank you.
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've got a .NET 3.5 web application written in C# doing some URL rewriting that includes a file path, and I'm running into a problem. When I call string.Split('/') it matches both '/' and '\' characters. Is that... supposed to happen? I assumed that it would notice that the ASCII values were different and skip it, but it appears that I'm wrong.
// url = 'someserver.com/user/token/files\subdir\file.jpg
string[] buffer = url.Split('/');
The above code gives a string[] with 6 elements in it... which seems counter intuitive. Is there a way to force Split() to match ONLY the forward slash? Right now I'm lucky, since the offending slashes are at the end of the URL, I can just concatenate the rest of the elements in the string[], but it's a lot of work for what we're doing, and not a great solution to the underlying problem.
Anyone run into this before? Have a simple answer? I appreciate it!
More Code:
url = HttpContext.Current.Request.Path.Replace("http://", "");
string[] buffer = url.Split('/');
Turns out, Request.Path and Request.RawUrl are both changing my slashes, which is ridiculous. So, time to research that a bit more and figure out how to get the URL from a function that doesn't break my formatting. Thanks everyone for playing along with my insanity, sorry it was a misleading question!
When I try the following:
string url = #"someserver.com/user/token/files\subdir\file.jpg";
string[] buffer = url.Split('/');
Console.WriteLine(buffer.Length);
... I get 4. Post more code.
Something else is happening, paste more code.
string str = "a\\b/c\\d";
string[] ts = str.Split('/');
foreach (string t in ts)
{
Console.WriteLine(t);
}
outputs
a\b
c\d
just like it should.
My guess is that you are converting / into \ somewhere.
You could use regex to convert all \ slashes to a temp char, split on /, then regex the temp chars back to \. Pain in the butt, but one option.
I suspect (without seeing your whole application) that the problem lies in the semantics of path delimiters in URLs. It sounds like you are trying to attach a semantic value to backslashes within your application that is contrary to the way HTTP protocols define and use backslashes.
This is just a guess, of course.
The best way to solve this problem might be modifying the application to encode the path in some other way (such as "%5C" for backslashes, maybe?).
those two functions are probably converting \ to / because \ is not a valid character in a URL (see Which characters make a URL invalid?). The browser (NOT C#, as you are inferring) is assuming that when you are using that invalid character, you mean /, so it is "fixing" it for you. If you want \ in your URL, you need to encode it first.
The browsers themselves are actually the ones that make that change in the request, even if it is behind the scenes. To verify this, just turn on fiddler and look at the URLs that are actually getting sent when you go to a URL like this. IE and Chrome actually change the \ to / in the URL field on the browser itself, FireFox doesn't, but the request goes through that way anyways.
Update:
How about this:
Regex.Split(url, "/");
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.