【问题标题】:Separating a part of the current URL分隔当前 URL 的一部分
【发布时间】:2019-09-06 07:26:48
【问题描述】:

我想分离当前 URL 的一部分

示例:localhost:50981/Admin/AddCustomer.aspx

我想要的部分:AddCustomer 或

示例:localhost:50981/Request/Customer.aspx

我想要的部分:客户

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以使用string.Split():

    var url = "localhost:50981/Admin/AddCustomer.aspx";
    var result = url.Split('/').Last().Split('.')[0];
    

    Asp.Net 中获取当前的Url 路径:

    var url = HttpContext.Current.Request.Url.AbsolutePath;
    

    注意
    如果您对如何获取 url 的不同部分感兴趣,请查看this 答案:

    var scheme = Request.Url.Scheme; // will get http, https, etc.
    var host = Request.Url.Host; // will get www.mywebsite.com
    var port = Request.Url.Port; // will get the port
    var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx
    

    【讨论】:

      【解决方案2】:

      您可以在页面的onLoad 功能中使用AbsolutePath

      //AddCustomer or Customer 
      string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-28
        • 2013-10-27
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        相关资源
        最近更新 更多