jQuery location.pathname在C#中等效

问题描述

基本上我想使用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
}

谢谢您的帮助。

解决方法

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

@{
     string strPathAndQuery = Request.RawUrl;
}