【发布时间】:2019-09-06 07:26:48
【问题描述】:
我想分离当前 URL 的一部分
示例:localhost:50981/Admin/AddCustomer.aspx
我想要的部分:AddCustomer 或
示例:localhost:50981/Request/Customer.aspx
我想要的部分:客户
【问题讨论】:
我想分离当前 URL 的一部分
示例:localhost:50981/Admin/AddCustomer.aspx
我想要的部分:AddCustomer 或
示例:localhost:50981/Request/Customer.aspx
我想要的部分:客户
【问题讨论】:
您可以使用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
【讨论】:
您可以在页面的onLoad 功能中使用AbsolutePath。
//AddCustomer or Customer
string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];
【讨论】: