Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of UseUrls for .NET Core 3.1/IHostBuilder

Previously, with .NET Core 2.2, I could add UseUrls to my Program.cs file to set the URL that the web server would run on:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseUrls("http://localhost:5100");

However, in .NET Core 3.1, the default format of Program.cs changed:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

I tried adding UseUrls to this in the same manner as I did with .NET Core 2.2, but it says that:

'IHostBuilder' does not contain a definition for 'UseUrls' and the best extension method overload 'HostingAbstractionsWebHostBuilderExtensions.UseUrls(IWebHostBuilder, params string[])' requires a receiver of type 'IWebHostBuilder'

How can I set the URL for the server to run on using .NET Core 3.1 (which uses IHostBuilder instead of IWebHostBuilder)?

like image 721
Merlin04 Avatar asked Dec 15 '19 20:12

Merlin04


People also ask

What is IHostBuilder in .NET Core?

ConfigureAppConfiguration(IHostBuilder, Action<IConfigurationBuilder>) Sets up the configuration for the remainder of the build process and application. This can be called multiple times and the results will be additive. The results will be available at Configuration for subsequent operations, as well as in Services.

How do I change the port in .NET Core?

To change the port the application is using, Open the file lunchSetting. json. You will find it under the properties folder in your project and as shown below. Inside the file, change applicationUrl port (below is set to 5000) to a different port number and save the file.

Is .NET Core 3.1 stable?

The extra two months (after . NET Core 3.0) allowed us to select and implement the right set of improvements over what was already a very stable base. . NET Core 3.1 is now ready to be used wherever your imagination or business need takes it.

What is IApplicationBuilder in .NET Core?

UseExceptionHandler(IApplicationBuilder) Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline. The request will not be re-executed if the response has already started.


1 Answers

The method ConfigureWebHostDefaults allows you to configure the web host. One of the thing you can do is change the urls: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#urls

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseUrls("http://localhost:5100");
            });
like image 77
meziantou Avatar answered Nov 10 '22 10:11

meziantou