This question already has answers here:
Retrieving anchor link in URL for ASP.NET
(3 answers)
Closed 9 years ago.
I have run into a trivial(?) problem when trying to get the whole URL of a c# page.
The url contains the "#"-link ref char. And i would like that to when I grab the URL
Eg. http://localhost/site/page.aspx?var=1&var=2#link
I have tried Request.URL, Request.Querystring etc, it only returns up to the "#"-char.
Is there any way to grab even the last part?
Thanks in advance
That is not possible using server code only. The part after the # is not sent in the request at all, it never leaves the browser.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
Your problem is that # specified an anchor in the page, so the browser sees:
http://localhost/site/page.aspx?var=1&var=2
And then looks in the page for
<a name="link">anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
Are you sure that the stuff after the # isn't sent to the server. I'm pretty sure i made a test with an ajax-app some years ago where the url could be copied and sent to people without javascript by only modifiying the stuff after the # in the url when browsing around with javascript enabled.
That was in PHP and the browser was probably IE6.
Related
I'm developing a C# MVC web-application where I'd like to directly link to a google search with a specific string. The final string is formatted in the following way: http://www.google.com/search?q=partialstring1+partialstring2+partialstring3
The html code looks as follows:
<p><br><a target="_blank" href=http://www.google.com/search?q=' + FormattedString + '> Google </a> ' + </p>
This html is again part of a final string which is should be called in an InfoBubble.
It works on any other browser (Firefox, Chrome, Opera ...) but it results in Error 400 when opening the link in Internet Explorer (11 and below).
It says Your client has issued a malformed or illegal request.
It works when I look up the specific string directly in Google and the resulting url looks exactly the same as the one I'd like to open from my application.
The formatted string gets rid of all umlauts (ä, ü, ö) and special characters (%&/§$) and works fine in Firefox when I copy and paste it there.
Is there some special IE-formatting for Google searches? I really don't know how to fix this issue, so any help would be appreciated.
I don't have any issue with loading the link in IE 11 at least. I have it setup like this with Razor formatting. Are you using something different for the views?
#{
var FormattedString = "partialstring1+partialstring2+partialstring3";
}
<p><br><a target="_blank" href="http://www.google.com/search?q=#FormattedString"> Google </a></p>
Which gives me a functional link in any browser.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to check some informations on remote Web pages with C#.
First question, is it possible to load completely (with Javascript) a Web page without using a Web Browser, just with the URL ?
Second question, is it possible to verify if a css selector is present on a remote page without browser, just with the URL ?
Thank you for listening !
If you mean 'remote' instead of 'distant' then - yes, kind off.
Your question begins asking about C# and then mentions javascript.
Not sure why the change in language context - but I will try to answer both.
Can you load a page using Javascript.
Yes: What you need to do, is perform a GET request on a URL.
The interesting thing about what you are doing with Javascript at this stage, is that any GET request you do from within a web browser context, will have access to the cookies and authentication that has been setup within that browsing session- particularly good for Test Automation. Eg: You need to download a PDF, but the PDF is protected by authentication checks.
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "http://www.google.com", false);
xmlhttp.send();
var data = JSON.parse(xmlhttp.responseText);
This returns the html content of the URL that you request, or the binary content if it is a binary file.
In the case where it is html content, you can then use javascript to change your current page source to be equal to the page source you just requested.
eg:
document.documentElement.innerHTML = myReturnedData;
The page will instantly load and display the returned page content.
if you want to verify if a CSS Selector exists / is valid for a remote page, then the first step you need to do, is to download the page source for that remote page.
I recommend using HtmlAgilityPack and its extension package
https://github.com/hcesar/HtmlAgilityPack.CssSelector
Once you install these, you will be able to instantiate the HtmlDocument and then query the document using the locator you specify.
var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load("test.html");
IList<HtmlNode> nodes = doc.QuerySelectorAll("div .my-class[data-attr=123] > ul li");
HtmlNode node = nodes.QuerySelector("p.with-this-class span[data-myattr]");
In my Project i don't want to show query string values to users. For that case i used URL Rewriting in asp.net. So my URL Looks like below.
http://localhost/test/default.aspx?id=1
to
http://localhost/test/general.aspx
The first URL will be rewrites to second URL, but it will still executes the default.aspx page with that query string value. This is working fine.
But my question is that, is there any way the user can find that original URL in browser?
The answer is no.
The browser can't tell what actual script ended up servicing the request - it only knows what it sent to the server (unless the server issued a redirect, but then the browser would make a new request to the redirect target).
Since URL rewriting takes an incoming request and routes it to a different resource, I believe the answer is yes. Somewhere in your web traffic you are requesting http://localhost/test/default.aspx?id=1 and it is being rewritten as the new request http://localhost/test/general.aspx.
While this may hide the original request from displaying in the browser, at some point it did send that original URL as an HTTP GET.
As suggested, use Firebug or Fiddler to sniff the traffic.
I figured answer for my question. We can easily found the rewritten urls. If we saw the view source of that page in browser then we can see that original url with querystring values.
I am workign with OAuth2 and and I am calling Google API. Google returns results after call is complete and I am supposed to read from the query parameters. Now, the kind of URL that Google Returns is weird and it has anchor # in it exactly where there should be a ?
and URL looks something like
http://localhost.contestfactory.com/enduser/#state=MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw&access_token=ya29.AHES6ZTjWwx7hHO4WnmfQ_lwJSpATCqA_DUZCC_ZIjdyPWA96OS0EN0&token_type=Bearer&expires_in=3600
Because of # in the URL my C# code fails to read beyond #. Is there anyway I can deal this problem in C#?
The part after the # is called the URL fragment (or hash).
It is never sent to the server.
what ur saying looks like hash fragment.. and it is never sen tto the server. what u can do instead make a JS script that changes all the "#" to "?" that way they will be treated as query string and will be sent to the server
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
A way to figure out redirection URL
C# Issue:
If we type the following url (see warning) http://migre.me/13rQa
It will redirect to oooooo1.ru
I like to know the redirected URL from C#.
Anybody knows how to do it.
P.S: These mentioned URL could be virus affected. Please use on own risk
If migre.me has some kind of an API, maybe they can provide you with a "preview" of the site you'll be redirected to.
If they don't, you'll have to create an HttpWebRequest and see the Uri of the response.
e.g.
WebRequest request = WebRequest.Create("http://migre.me/13rQa");
var response = request.GetResponse();
With that code, the response.ResponseUri property will have the address you'll be redirected.
Hope this helps you
If you're using HttpWebRequest, set AllowAutoRedirect to false. You'll then get to see the redirect yourself. Note that this only applies to HTTP-level redirects, not meta redirects within HTML.
(I don't know offhand whether it will throw an exception, but even if it does you can examine the response to find out the status code and the data including the redirection.)