-1

I want Separating a part of the current URL

Example:localhost:50981/Admin/AddCustomer.aspx

The part I want: AddCustomer or

Example:localhost:50981/Request/Customer.aspx

The part I want: Customer

amir
  • 25
  • 8

2 Answers2

1

You can use AbsolutePath in your onLoad function of page.

//AddCustomer or Customer 
string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];
ahmeticat
  • 1,899
  • 1
  • 13
  • 28
0

You can make use of string.Split():

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

To get the current Url path in Asp.Net:

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

Note:
If you are interested in how to get the different parts of an url have a look at this answer:

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
croxy
  • 4,082
  • 9
  • 28
  • 46