Home > Back-end >  Get port and url for project
Get port and url for project

Time:02-15

Is it possible to get the port and url my project is running on? I.e if it is running on 7074 for http://localhost:, am I able to get that on runtime?

CodePudding user response:

If you are in the context of a request, yes, you can use the following:

var scheme = Request.Scheme; // https
var host = Request.Host.Host; // localhost
var port = Request.Host.Port; // 7074

CodePudding user response:

If you are using .net core web application, you can try to use IServerAddressesFeature to get your HostName & Port in Runtime

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    var serverAddressesFeature = app.ServerFeatures.Get<IServerAddressesFeature>();
    var address = serverAddressesFeature.Addresses.FirstOrDefault();
    //this address value is your expect
    //....
}
  • Related