问题描述
以前,使用 .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"类型的接收器
如何为使用 .NET Core 3.1(使用 IHostBuilder
而不是 IWebHostBuilder
)的服务器设置 URL?
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 主机.您可以做的一件事是更改网址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#urls
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 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!