Retrieving Anchor Link In URL for ASP.Net

百般思念 提交于 2019-11-26 21:00:32
Eoin Campbell

It's not possible to retrieve the #anchor from the server side in ASP.NET

This is a client-side flag to tell the browser to move to a specific place within the page.

You can use some Javascript in the body onLoad event to check for an anchor and send it back to the server using ajax.

var anchorValue;
var url = document.location;
var strippedUrl = url.toString().split("#");
if(strippedUrl.Length > 1)
anchorvalue = strippedUrl[1];

ref: http://www.wacdesigns.com/2008/01/16/retrieving-anchor-value-from-url/

Being more explicit, the anchor tag is never sent as part of the HTTP request by any browser, it is only interpreted locally within the browser. Neither ASP.NET nor any other web-server technology, Microsoft or otherwise will see the anchor on that request.

RFC 1808
Section 2.4.1 - "Note that the fragment identifier is not considered part of the URL."

As others have suggested the nearest you could get would be using client-side to read browser window location.

Eugeniu Torica

A fragment can be parsed from a url in C# in the following way:

var uri = new Uri("http://localhost?id=2#token=23");
var fragment = uri.Fragment; // will return #token=23

There is a problem however in that the browser won't send fragments to the server. If you receive requests from a service that includes this info in the request, it will available from the server side too.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!