C# 如何从查询字符串中获取#值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8757817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:16:55  来源:igfitidea点击:

How to get # value from query string?

c#asp.net.net

提问by PrateekSaluja

Please help me to get query string value from the URL.

请帮助我从 URL 获取查询字符串值。

http://test.com/test.aspx#id=test

I tried to access with

我试图访问

Request.QueryString["id"]

Its getting null value.Please suggest how to access id from the url.

它得到空值。请建议如何从 url 访问 id。

Thanks

谢谢

采纳答案by inspite

I agree with everyone that the #should be a ?, but just FYI:

我同意大家的看法,#应该是?,但仅供参考:

Note it isn't actually possible get the anchor off the URL, for example:

请注意,实际上不可能从 URL 中获取锚点,例如:

http://test.com/test.aspx#id=test

http://test.com/test.aspx#id=test

The problem is that # specified an anchor in the page, so the browsersees:

问题是#在页面中指定了一个锚点,所以浏览器看到:

http://test.com/test.aspx

http://test.com/test.aspx

And then looks in the page for

然后在页面中查找

<a id="test">Your 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.

由于这是客户端,您需要从 URL 中转义 # - 您无法在服务器上获取它,因为浏览器已经将其剥离。

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.

如果您想要 # 之后的部分,则必须在将请求发送到服务器之前使用 Javascript 复制它,并将该值放入查询字符串中。

More info here c# get complete URL with "#"

此处的更多信息c# 使用“#”获取完整 URL

回答by craig1231

Isnt it supposed to be?

不是应该吗?

http://test.com/test.aspx?id=test 

回答by Henk Holterman

Your URL is not valid.

您的网址无效。

 http://test.com/test.aspx#id=test

refers to a bookmarknamed id=test.

指名为的书签id=test

You should use

你应该使用

http://test.com/test.aspx?id=test

And then Request.QueryString["id"]will work.

然后Request.QueryString["id"]将工作。

回答by Grhm

A query string starts with a question mark ?not a hash #.

查询字符串以问号?而不是哈希开头#

Try:

尝试:

http://test.com/test.aspx?id=test 

Using a hash, you're asking to jump to a named anchor within the document, not providing a query string

使用哈希,您要求跳转到文档中的命名锚点,而不是提供查询字符串

回答by Caner Akdeniz

If you would like to use it as hash tag you can use:

如果您想将其用作哈希标签,您可以使用:

string value = Request.Url.ToString().Split('#')[1];

with this code, you will have your hash tag value.

使用此代码,您将拥有哈希标签值。