分隔当前网址的一部分

时间:2019-04-16 08:43:34

标签: c# asp.net

我要分隔当前网址的一部分

  

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

我要的部分:AddCustomer 或

  

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

我想要的零件:客户

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您可以使用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;

注意
如果您对如何获取网址的不同部分感兴趣,请查看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