如何从 c# 中的控制器方法中的 URL 中提取 ID?

How can I extract an ID from an URL in controller method in c#?

我有一个 url 标签 ID:

http://localhost:19269/Member/Edit/35b46e40-b4b4-4e61-bcee-f5cc4f4fac94#tab_content2

当我提交 beginform 时,我想从 url 中提取(在我的控制器中)“#tab_content2”。

我尝试使用 "Request.Url.AbsolutePath" 但我只能访问 http://localhost:19269/Member/Edit/35b46e40-b4b4-4e61-bcee-f5cc4f4fac94 而不是锚点 link...

提前致谢

TL;DR:你不能

浏览器从不传输 URI 的哈希部分(# 符号之后的任何内容)。

From RFC 2396:

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

获取片段部分的唯一方法是使用javascript获取它,然后将其作为查询字符串参数或post发送表格内的数据。

可能的解决方法(如果您使用的是 jQuery):

$("#myForm").submit(function() {
    $("#myHiddenInput").val(window.location.hash);
});

假设 myForm 作为您的 HTML 表单的 ID,并且 myHiddenInput 作为用于此特定目的的 <input type="hidden"> 的 ID。

从服务器端,您必须将隐藏的输入映射到您的模型才能检索此数据。