Accessing the ASP.NET Core Base URL | HOW TO – Code Samples



Accessing the ASP.NET Core Base URL | HOW TO – Code Samples

Accessing the ASP.NET Core Base URL | HOW TO - Code Samples

https://StartupHakk.com/?v=hazKn-sxv_g

#coding #codingbootcamp #softwaredeveloper #CodeYourFuture

Github Repo: https://github.com/slthomason/StartupHakk/blob/main/71_Accessing_ASPNETCORE_URL/Program.cs

How to Access the Base URL of your ASP.NET Core Application

You can find a great tsunami of info on the topic of accessing the Base URL of your ASP.NET Core app online, but I have gone ahead and trawled them for you (you’re welcome).

It breaks down like this:

1. You can hard-code the base URL / stuff it into appsettings.json Clearly, this is NOT ideal, so let’s just set that aside for now.

2. You can construct the base URL from the HttpContext itself while in the midst of any kind of HTTP action (say while interacting on an MVC or Blazor page). You can do so using the NavigationManager to help you out.

3. You can construct the base URL by injecting the IHttpContextAccessor so as not to muss about with an existing HTTP request. I’ll go over this below and also when this won’t help you.

4. Use some sneaky Pete details stored in your web app as it start up (more on this as we proceed)