Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
215 views
Welcome To Ask or Share your Answers For Others

1 Answer

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");
            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...