Divesh Soni
Divesh Soni

Reputation: 71

Fetch Url Segments

I want to fetch the url segments of the current page. I was using Web Forms earlier. In Web Forms I was using Request.Url.Segments method to do that. But this method is not working in ASP.NET Core 2.2.

string[] urlSegments = Request.Url.Segments;

Upvotes: 3

Views: 4908

Answers (2)

Daniel
Daniel

Reputation: 9829

You could easily achieve this with a simple .Split() like:

var stringArray = "stackoverflow.com/questions/57798723/fetch-url-segments".Split('/');

Outputs:

stringArray[0] // stackoverflow.com
stringArray[1] // questions

Upvotes: 0

Nan Yu
Nan Yu

Reputation: 27538

Uri.Segments works with .net core . You can use :

string[] urlSegments = new Uri(Request.GetDisplayUrl()).Segments;

GetDisplayUrl() is an extension method from the following namespace : Microsoft.AspNetCore.Http.Extensions :

using Microsoft.AspNetCore.Http.Extensions;

Upvotes: 6

Related Questions