c#中等效的jquery location.pathname
Posted
技术标签:
【中文标题】c#中等效的jquery location.pathname【英文标题】:jquery location.pathname equivalent in c# 【发布时间】:2020-12-09 05:39:05 【问题描述】:我想使用 c# 在搜索栏中获取 url 基本上我想知道 c# 中 location.pathname 的等价物
这是搜索栏中的网址
https://localhost:44363/modulotecnico
这是我的 jquery 代码
$(document).ready(function ()
var test = $('#currentPage').val(location.pathname);
//test = /modulotecnico
);
/modulotecnico 是我希望在我的剃须刀页面中使用 c# 代码的结果我已经尝试了所有这些方法,但似乎没有一个有效
这是我在 c# 中尝试过的以及得到的结果
@
String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
//originalPath = https://localhost:44363/kanban/Contrato/19041240
String parentDirectory = originalPath.Substring(0, originalPath.LastIndexOf("/"));
//parentDirectory = https://localhost:44363/kanban/Contrato
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// url = https://localhost:44363/kanban/Contrato/19041240
string path = HttpContext.Current.Request.Url.AbsolutePath;
// path = /kanban/Contrato/19041240
string host = HttpContext.Current.Request.Url.Host;
// host = localhost
感谢您的帮助。
【问题讨论】:
你要重写 URL 吗?没有理由为什么 C# 请求 URL 应该与 javascript 中的不同。您可能需要查看 Request.UrlReferrer 我看过Request.UrlReferrer,值为null 我想我的问题是为什么您的第一个示例 (/modulotecnico) 中的 URL 与 c# 请求中的 URL 不同。同样,您是在进行任何 URL 重写还是有自定义路由设置? 'modulotecnico' 是 IIS 中的站点名称,因此是 URL 的前缀。本质上,为什么它应该是 '/modulotecnico' 而不是 c# 请求 URL 所说的? 【参考方案1】:您可以使用Request.RawUrl
,如下所示:
@
string strPathAndQuery = Request.RawUrl;
【讨论】:
不,它不起作用我得到以下 url /kanban/Contrato/19041240 我想要浏览器搜索栏中的 url。不过谢谢你的帮助。 欢迎来到 Stack Overflow。 Stack Overflow 上不鼓励仅使用代码的答案,因为它们没有解释它是如何解决问题的。请编辑您的答案以解释此代码的作用以及它如何回答问题,以便它对 OP 以及其他有类似问题的用户有用。以上是关于c#中等效的jquery location.pathname的主要内容,如果未能解决你的问题,请参考以下文章
C# 中是不是有 .isConnected 功能的 python 等效项