I am getting problems with ASP getting the URL when it has parentheses.
My site has URLs like this one: http://example.com/my-amazing-product-%28online%29
So, I am trying to get that URL using the instruction:
var requestURL = HttpContext.Current.Request.RawUrl;
But, that instruction is getting the URL: http://example.com/my-amazing-product-(online)
It automatically converts the parentheses from %28 to ( and from %29 to ), but I don't need that conversion, I need the original URL with %28 and %29
And I can't use replace to fix the URL. I have to get that URL directly from the request or something similar.
Can anyone explain that weird behavior and solve the problem?
Related
I add route to project.All work correct, but if i send normal url as first param its not correctly work.
Get["/{url}/{digit}"
If i send this params to server-all work correctly.
localhost:8888/google.com/2
But if i send param with http://www its not work.
localhost:8888/https://www.google.com/2
How correct pass url param to route? I think it because Nancy think that i send 3 input param.
If you really need to use GET instead of POST try HttpUtility.UrlEncode("https://google.com/2") to urlencode your url.
You have to encode your url which is send as a paramater:
Use:
var encodedString = Uri.EscapeDataString("https://www.google.com/2");
then your url will look like this and it shouldn't get any errors:
https%3A%2F%2Fwww.google.com%2F2
Sending the request:
localhost:8888/https%3A%2F%2Fwww.google.com%2F2
Or ou can use the
HttpUtility.UrlEncode();
method. For further information have a look at this.
Since you insist on changing the backend only, you could try using a regex to capture your route
Get["^(?<url>.*<digit>[0-9]+)$"]
This should match any url ending with atleast one number, and put everything before it in url like so:
Get["^(?<url>.*<digit>[0-9]+)$"] = parameters =>
{
var url = parameters.url;
var digit = parameters.digit;
};
I am currently unable to verify if this works as you want it to though, and to make sure you can adjust this yourself make sure to look into how to write regex
I am trying to use oauth2.
I make a get request, and then I get redirected at a callback url that I have set up before. The problem lies in the fact that the url parameters get preceded by the # sign and thus php, .net can't read them!
I get redirected in the following url in my browser:
http://localhost:1787/About.aspx?#access_token=f3EToovT2bQNNOQ&token_type=bearer&merchant_id=A6BGD4BH&response_type=token
Request.Params is empty, request.query string is empty. Even when I use php and print the $_REQUEST array still is empty!
How is this possible?
Whatever comes after the # doesn't mark as DATA being sent to the server, but a hash on the client side.
Try redirect using JavaScript only the hash:
window.location = window.location.pathname + '?' + window.location.hash.substring(1);
I have a ASP.NET MVC controller which is making call to another service using HttpClient class.
var url = "some url";
var client = new HttpClient();
var result= client.GetAsync(url);
The URL I am sending contains some special characters. How can encode special characters in ASP.NET MVC controller?
Thanks!!1
Try this:
url = HttpUtility.UrlEncode(url);
As you are considering a URL that will be used as such for a request (with HttpClient.getAsync) -- not as an argument within another URL -- you should use Uri.EscapeUriString.
Here is a comparison of three methods for the following URL:
var url = "http://some url?data=x y+z&user=1#ok";
HttpUtility.UrlEncode
Console.WriteLine(HttpUtility.UrlEncode(url));
http%3a%2f%2fsome+url%3fdata%3dx+y%2bz%26user%3d1%23ok
Obviously, this is not desired: the URL got damaged with / escaped, a + entered in the path, ...etc. The method seems useful for the query part of the URL, but not for the whole lot.
HttpUtility.UrlPathEncode
Console.WriteLine(HttpUtility.UrlPathEncode(url));
http://some%20url?data=x y+z&user=1#ok
This looks useful, although the space is a bit of a problem in the query part (notice the broken hyperlinking here, although browser can deal with it). But more importantly, the method is being deprecated:
Do not use; intended only for browser compatibility. Use UrlEncode.
Uri.EscapeUriString
Console.WriteLine(Uri.EscapeUriString(url));
http://some%20url?data=x%20y+z&user=1#ok
This seems to do the job well: %20 is an escape sequence that all modern browsers should support, also when occurring in the query part of the URL.
There is no need it encoding in Razor View Engine starting from 3rd version, and it's very convenient. Instead if you want to use tags you should use:
#Html.Raw(myString)
So basically just using Razor comes with encoding by default.
You should use HttpUtility.UrlPathEncode
When you use url = HttpUtility.UrlEncode(url) it doesn't work fine with spaces.
I am trying to read the html content from a given url , this is my simple c# code
WebClient client = new WebClient();
var downloadString = client.DownloadString("https://www.yahoo.com"); // suppose
but the problem is I cannot obtain the html content, after executing the code it tells in my IDE: "before you can move on please activate javascript" , but javascript is enabled in all my browers (firefox/explorer/chrome )
how to resolve this?
In your sample you misspelled the domain name. In other way it works just fine. http://screencast.com/t/3YibceMtSFu
I am using ASP.NET and c# to build my website.
I am facing some issues with newline constant in the url.
Here is my Url
http://localhost/TestProject/Login/Login.aspx?ReturnUrl=http://google.com?search=Software+Engineer%0ASalesforce.com
which contains a new line constant. (%0A)
In my code i am decoding the url to get some parameter, so i am using System.Web.HttpUtility.UrlDecode to decode the url and later after that, i am appending some session id to the url and redirecting the same.
Since the Url contains new line constant, it breaking my page and throws an exception
Value does not fall within the expected range.
if i remove the new line constant from the url its working fine as expected.
Any suggestions how to handle this?