In ASP.NET Core 2.1, you can retrieve the current web hostname and port in the Startup.cs file using the IWebHostEnvironment service. Here's an example:
using Microsoft.AspNetCore.Hosting; public class Startup { private readonly IWebHostEnvironment _environment; public Startup(IWebHostEnvironment environment) { _environment = environment; } public void ConfigureServices(IServiceCollection services) { // Retrieve the current web hostname and port var hostname = _environment.IsDevelopment() ? "localhost" : _environment.EnvironmentName + ".mydomain.com"; var port = _environment.IsDevelopment() ? "5000" : "80"; // Use the hostname and port in your application configuration // ... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // Configure the application // ... } } In this example, we're injecting the IWebHostEnvironment service into the Startup constructor. We then use the IsDevelopment() method to determine if we're running in a development environment or not. If we are, we set the hostname and port to localhost and 5000, respectively. If we're not in a development environment, we set the hostname to the current environment name (e.g. staging or production) followed by the domain name, and set the port to 80.
You can then use the hostname and port variables in your application configuration as needed. Note that this example assumes that you're using the default Kestrel web server. If you're using a different web server, you may need to modify this code to retrieve the hostname and port in a different way.
"ASP.NET Core 2.1 get current web hostname in Startup.cs"
var host = configuration.GetValue<string>("Host"); Startup.cs."ASP.NET Core 2.1 get current web port in Startup.cs"
var port = configuration.GetValue<int>("Port"); Startup.cs."ASP.NET Core 2.1 get current web root URL in Startup.cs"
var baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host}"; Startup.cs."ASP.NET Core 2.1 get current web hostname and port from HttpContext in Startup.cs"
var host = httpContextAccessor.HttpContext.Request.Host.Host; var port = httpContextAccessor.HttpContext.Request.Host.Port;
Startup.cs."ASP.NET Core 2.1 get current web base URL in ConfigureServices"
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
HttpContextAccessor in ConfigureServices to enable accessing HttpContext in Startup.cs."ASP.NET Core 2.1 get current web root URL in ConfigureServices"
var baseUrl = $"{context.Request.Scheme}://{context.Request.Host}"; ConfigureServices."ASP.NET Core 2.1 get current web base URL in Configure"
var baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host}"; Configure."ASP.NET Core 2.1 get current web base URL in ConfigureServices"
var baseUrl = $"{context.Request.Scheme}://{context.Request.Host}"; ConfigureServices."ASP.NET Core 2.1 get current web root URL using IHostingEnvironment"
var baseUrl = $"{environment.ContentRootPath}"; IHostingEnvironment in Startup.cs."ASP.NET Core 2.1 get current web root URL with HttpContextAccessor"
var baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host}"; HttpContextAccessor in Startup.cs.shell sqlplus ref draw textinput eslint bootbox communication jenkins-scriptler superscript