.NET Uri class cheatsheet

June 21, 2017

The Uri class in .NET contains methods for getting at parts of the Uri. In your ASP.NET MVC project you can get the current URL from within a Controller using:

 Uri currentUrl = Request.Url;

From this object you can then get lots of information about the current URL. I find that each time I use it, I have to look up which method gives what. Here is a quick cheatsheet for the methods and properties:

For the URL: https://github.com/brainwipe/NJsonApiCore/search?utf8=%E2%9C%93&q=jsonapi&type=

Uri Property Value
AbsolutePath /brainwipe/NJsonApiCore/search
AbsoluteUri https://github.com/brainwipe/NJsonApiCore/search?utf8=%E2%9C%93&q=jsonapi&type=
Authority github.com
DnsSafeHost github.com
Fragment
Host github.com
HostNameType Dns
IsAbsoluteUri True
IsDefaultPort True
IsFile False
IsLoopback False
IsUnc False
LocalPath /brainwipe/NJsonApiCore/search
OriginalString https://github.com/brainwipe/NJsonApiCore/search?utf8=%E2%9C%93&q=jsonapi&type=
PathAndQuery /brainwipe/NJsonApiCore/search?utf8=%E2%9C%93&q=jsonapi&type=
Port 443
Query ?utf8=%E2%9C%93&q=jsonapi&type=
Scheme https
Segments / brainwipe/ NJsonApiCore/ search
UserEscaped False
UserInfo

C# .net console gist for generating this output and a handy Markdown table builder.


Rob Lang's profile picture

Written by Dr Rob Lang , PhD AI, lead web developer, hobby game developer. Twitter, YouTube, GitHub