E-Learning

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

c# url

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.

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

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

The host string variable will return “localhost”.

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

The port string variable will return “1345”.

The app_path string will return “/app”.

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!

Avatar
About author

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.