E-Learning

Learn C# – How To Get the URL of The Current Page

If you are using C# for creating apps, you may want to use it to fetch the URL of the current page. This requirement is extremely common in web apps, or apps that take advantage of external or internal resources. In this tutorial, we will cover how to get the URL of the current page using C#. Without wasting much time, let’s gets started.

string url = HttpContext.Current.Request.Url.AbsoluteUri;

This will return the absolute URL. www.google.com, for example, or it will return localhost URL if you are in the local environment.

It should be kept in mind that Url.AbsoluteUri does return anything after the “#” symbol.

When working on an app, you might also want to get different results for different purposes. Let’s go through them, one by one.

We will use a hypothetical URL to work. Let it be

http://localhost:1345/app/homepage.aspx?query1=a&query2=b
string path = HttpContext.Current.Request.Url.AbsolutePath;

The path string variable will return “/app/homepage.aspx”.

string host = HttpContext.Current.Request.Url.Host;

The host string variable will return “localhost”.

string authority = HttpContext.Current.Request.Url.Authority

The authority string variable will return “localhost:1345”.

string port = HttpContext.Current.Request.Url.Port

The port string variable will return “1345”.

string app_path = HttpContext.Current.Request.ApplicationPath

The app_path string will return “/app”.

string path_and_query = HttpContext.Current.Request.Url.PathAndQuery

The path_and_query string will return “/app/homepage.aspx?query1=a&query2=b”.

As you can see, you can get different parts of the URL according to your requirement. Just make sure you store the variable correctly and don’t use concatenation to get different parts of the URL.

You can read more about URL properties here.

If you feel stuck and need more help regarding this function, you can post your queries in the comment section below.

You can also check on our website videos about C#. Below are some examples:

  • Galactic Tactical Fighters Online #gamedev (part 79) – C#
  • C# Unity5 Quake Multiplayer FPS (part 4) – C#

You can also follow some of our broadcasters who program in C#, as below:

 moatdd

 KamoBanger

Another cool way to find out interesting things about C# is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago