C# 分离当前URL的一部分

C# 分离当前URL的一部分,c#,asp.net,C#,Asp.net,我想分离当前URL的一部分 示例:localhost:50981/Admin/AddCustomer.aspx 我想要的部分:AddCustomer 或 示例:localhost:50981/Request/Customer.aspx 我想要的部分:Customer您可以使用string.Split(): 要在Asp.Net中获取当前Url路径,请执行以下操作: 注意: 如果您对如何获取url的不同部分感兴趣,请查看答案: 您可以在页面的onLoad功能中使用AbsolutePath //Add

我想分离当前URL的一部分

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

我想要的部分:AddCustomer 或

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


我想要的部分:Customer

您可以使用
string.Split()

要在Asp.Net中获取当前
Url
路径,请执行以下操作:

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


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

//AddCustomer or Customer 
string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];
var url = HttpContext.Current.Request.Url.AbsolutePath;
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
//AddCustomer or Customer 
string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];