c# get complete URL with “#” [duplicate]

2020-02-02 03:20发布

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

标签: c# asp.net url
3条回答
【Aperson】
2楼-- · 2020-02-02 03:26

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.

查看更多
家丑人穷心不美
3楼-- · 2020-02-02 03:32

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.

查看更多
家丑人穷心不美
4楼-- · 2020-02-02 03:40

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.

查看更多
登录 后发表回答