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

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


You should still be able to piece together what you need. You have access to the request object if your controller inherits from Controller.

If you are using VS2017, fire up a new ASPNet Core MVC app and replace the homecontroller with:

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 put in some of the stuff that might interest you in the "About" method, but you should explore the rest of the request class so you know what else is available.

As @Tseng pointed out, you might have a problem when running Kestrel behind IIS or Azure App Service, but if you use the IISIntegration package or AzureAppServices package (by installing the Nuget package and adding it in Program.cs to your WebHostBuilder), it should forward those headers to you. It works great for me in Azure, because I sometimes have to make decisions based on which hostname they hit. The IIS/Azure packages also forward the original remote IP address, which I log.


If you need this anywhere in your app than you should build a middleware.Define your static class and your extension method for adding the 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;    }}

It might be a little redundant to expose the HttpContext in this case but I find it very helpful.

You would than add it to the pipeline in your Configfure method which is located in Startup.cs

app.UseHttpContext()

From there it is simple to use it anywhere in your code.

var appBaseUrl = MyHttpContext.AppBaseUrl;


NPNelson answer works if with .Value.ToString()

var baseUrl = $"{this.Request.Scheme}://{this.Request.Host.Value.ToString()}{this.Request.PathBase.Value.ToString()}";