等效于.NET Core 3.1/IHostBuilder的UseUrls [英] Equivalent of UseUrls for .NET Core 3.1/IHostBuilder

查看:256
本文介绍了等效于.NET Core 3.1/IHostBuilder的UseUrls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,使用.NET Core 2.2,我可以将UseUrls添加到我的Program.cs文件中,以设置Web服务器将在其上运行的URL:

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

但是,在.NET Core 3.1中,Program.cs的默认格式已更改:

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

我尝试以与.NET Core 2.2相同的方式向其中添加UseUrls,但是它说:

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

'IHostBuilder'不包含'UseUrls'的定义,最佳扩展方法重载'HostingAbstractionsWebHostBuilderExtensions.UseUrls(IWebHostBuilder,params string [])'需要类型为'IWebHostBuilder'的接收器.

'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'

如何设置服务器的URL以使用.NET Core 3.1(使用IHostBuilder而不是IWebHostBuilder)运行?

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

推荐答案

方法ConfigureWebHostDefaults允许您配置Web主机.您可以做的一件事就是更改URL:

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

这篇关于等效于.NET Core 3.1/IHostBuilder的UseUrls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆