Search code examples
c#asp.netvisual-studio-2015asp.net-mvc-5asp.net-core

Asp.NET Core Path string


I'm working in Asp.NET Core in VS 2015.

I'm trying to get the last directory in a URL.

I'm currently using string path = Context.Request.Path; which, for example, if I had the URL localhost.foo.com/bar/3, it would return /bar/3, Is there a way to get the 3 only? The 3 would be an ID for an object in my case.

And if not, is there a better way to return an object's ID that's used in the URL?


Solution

  • You can split your string by / and get last element:

    string path = Context.Request.Path;
    var lastValue = path.Split('/').Last();