How can I get the baseurl of my site in ASP.NET Core?

Say my website is located in the mywebsite folder on www.example.com, and I go to https://www.example.com/mywebsite/home/about .

How to get part of base URL in MVC? The part I'm looking for is https://www.example.com/mywebsite

The example given here does not work, since we do not have access to Request.Url in ASP.NET Core

+27
source share
4 answers

You still need to collect everything you need. You have access to the request object if your controller inherits from Controller .

If you are using VS2017, launch the new ASP.NET Core MVC application and replace the homcontontroller:

 public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult About() { ViewData["Message"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; return View(); } public IActionResult Contact() { ViewData["Message"] = "Your contact page."; return View(); } public IActionResult Error() { return View(); } } 

I just added some things that might interest you in the "About" method, but you should study the rest of the query class so that you know what else is available.

As @Tseng noted, you might have a problem starting Kestrel for IIS or the Azure application service, but if you use the IISIntegration package or the AzureAppServices package (by installing the Nuget package and adding it to Program.cs on your WebHostBuilder), it should pass these headers you. It works great for me on Azure because I sometimes have to make decisions based on what hostname they got. IIS / Azure packets also send the original remote IP address that I register.

+55
source

If you need this somewhere in your application, you should create middleware. Define your static class and extension method to add middleware to the service pipeline like this.

 public class MyHttpContext { private static IHttpContextAccessor m_httpContextAccessor; public static HttpContext Current => m_httpContextAccessor.HttpContext; public static string AppBaseUrl => $"{Current.Request.Scheme}://{Current.Request.Host}{Current.Request.PathBase}"; internal static void Configure(IHttpContextAccessor contextAccessor) { m_httpContextAccessor = contextAccessor; } } public static class HttpContextExtensions { public static void AddHttpContextAccessor(this IServiceCollection services) { services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); } public static IApplicationBuilder UseHttpContext(this IApplicationBuilder app) { MyHttpContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>()); return app; } } 

In this case, it may be a bit redundant to expose the HttpContext, but I find it very useful.

Then you add it to the pipeline in your Configfure method, which is located in Startup.cs.

 app.UseHttpContext() 

From there, just use it anywhere in your code.

 var appBaseUrl = MyHttpContext.AppBaseUrl; 
+11
source

NPNelson answer works if with .Value.ToString ()

 var baseUrl = $"{this.Request.Scheme}://{this.Request.Host.Value.ToString()}{this.Request.PathBase.Value.ToString()}"; 
0
source
 string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")); 

you can check more information here: How can I get the base url of my webapp in ASP.NET MVC?

-1
source

Source: https://habr.com/ru/post/1266982/


All Articles