ASP.NET: parse url having # (hash) sign - c#

I need to parse url that has something after # (hash) sign in my asp.net application. How to do it easily?
Thank you,

You're looking for the Uri class:
new Uri(someString).Fragment
Note that the hash is not sent to the server in an HTTP request.

url.Substring(url.IndexOf('#') + 1)
...where "url" is a string containing the url in question

This is called "hash sign" URI.
After client gets PAGE responsed including js,
the contents after '#' would be handled by client using responsed js to get "real" URL for redirection.
SEE: https://www.w3.org/2001/tag/2011/01/HashInURI-20110115#References

Omg, I meant fragment (after #) part on the server... Though I've looked thrugh and found that it seems to be impossible......

Related

How to encode IP for GET request in api in .NET

I'm trying to figure out how to send IP in GET request. I want to call GET request like : /api/endpoint/12.12.12.12. I tried to encode it but HttpUtility.UrlEncode won't encode dots for IP alone. When I try use %2E as dot then IIS throws 404.11 - The request filtering module is configured to deny a request that contains a double escape sequence.. How to I make it the right way?
Try to encode it in base 64. You can find how to do it here
/api/endpoint/MTIuMTIuMTIuMTI=
You could just do a string replace.
"12.12.12.12".Replace(".","%2E");
Add a slash at the end of the URL:
/api/endpoint/12.12.12.12/
this should work

'#' letter is missing in HttpContext.Request or HttpContext.Current.Request url?

My URL is: http://localhost/myApp/app/job/7#/Nmbr
When I tried to get the URL from HttpContext.Request or HttpContext.Current.Request,
the given url is : /myApp/app/job/7
# and everything after this letter are missing? Why? How can I get the real current url?
Http requests do not include anything after # (The browser does not send it with the request.)
# Is intended to allow for parameters to be specified for the browser only (Anchor target location, or with often with web 2.0 JavaScript parameters)
In order to pass parameters to the server use ? that appears before the #
Here is some information regarding fragments (The part of the URL after #)
The Fragment property gets any text following a fragment marker (#) in the URI, including the fragment marker itself. Given the URI http://www.contoso.com/index.htm#main, the Fragment property would return #main.
https://msdn.microsoft.com/en-us/library/system.uri.fragment(v=vs.110).aspx

Can't get parameter from url via query string

I am implementing google login on my site.
Problem is that when google redirect me back to my site (after confirmation) I can't get access token from query string.
This is URL:
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
I suspect that this #state=/profile make some issue but can't handle it.
Am I missing something?
If URL is like this , note that there is # after Default.aspx , it is not ?, then there is no direct way to get get querystring ( they are known as URL fragments not querystring), they are meant to be parse at client side and server side don't have access to URL Fragments.
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
Link contains # ,means an anchor, a position, on a webpage. The browser sends a GET request to the server containing only the address of the entire page, with no anchor, fragment or whatever. When the server returns the page, the browser knows where to position it so the location of the anchor is visible. In clientside or Javascript it is possible as it has access to the anchor.
Read this - How to get Url Hash (#) from server side
You could use
document.URL to get the url.
Then split the url by #state=/profile&
Then the second part of the array split by &.
Then each section split by first =
There may be a more elegant solution but this should work.

WebBrowser keep url/uri encoded dont decode

I have a web-browser in a win form application and I am experiencing issues when opening a URL.
The URL I pass in as a new URL instance is encoded with:
/ as %2f , ? as %3f and the
= as %3d
But when I debug my code I can see that the absolute URL or any of the other ones in the webbrowser.url.* is decoded as / , ? and =.
How do I keep the URL encoded? The URL will not work if It is not encoded like that.
I found a solution to my problem, when you have a URL that looks something like this:
domain.com/action/doaction/?identity=12354698789
And you want it encoded like this:
domain.com/logon?returnurl=action%2fdoaction%2f%3fidentity%3d12354698789
That does not work in your web browser. It decodes it to the first url.
I needed the id in the doaction controller so I used this code:
string orgId = ControllerContext.RouteData.Values["id"].ToString();
It returns that url, if unsure, debug and trace through, you will find the right key and value.
Why is it a problem?
If you want the undecoded URL, use the HttpRequest.RawUrl Property. The query string is automatically decoded by default and there is no public parameter that would turn it off.

301 Redirect with unicode characters - C#

I need to do a 301 redirect on a URL that may have Unicode characters in it.
HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)
Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.
You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)
So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.
I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.
That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx
which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.
After I got the Uri working with unicode, I pieced together the url and did a redirect:
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", uri.Scheme + "://" + uri.DnsSafeHost + uri.PathAndQuery + uri.Fragment);
Response.End();
This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.

Categories