繁体   English   中英

jquery location.pathname 相当于 c#

[英]jquery location.pathname equivalent in c#

我想使用 c# 在搜索栏中获取 url 基本上我想知道 c# 中 location.pathname 的等价物

这是搜索栏中的 url

https://localhost:44363/modulotecnico

这是我的 jquery 代码

$(document).ready(function () {
    var test = $('#currentPage').val(location.pathname);  
    //test = /modulotecnico
});

/modulotecnico 是我想在我的 razor 页面中使用 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
}

谢谢您的帮助。

您可以像下面这样使用Request.RawUrl

@{
     string strPathAndQuery = Request.RawUrl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM