当前上下文中不存在名为WebHost的内容。

21

我正在根据微软文档中的以下文章https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/,将ASP.NET Core 1.x迁移到v2.0。

我已经按照该文章中提到的所有更改几乎完成了迁移工作。但是仍然有一个错误导致了麻烦。

这里是我的Program.cs文件:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;

namespace MeridiaCoreAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostContext, config) =>
            {
            // delete all default configuration providers
            config.Sources.Clear();
                config.AddJsonFile("myconfig.json", optional: true);
            })
               .Build();
    }
}

以下是错误信息:

Suppression State
Error   CS0103  The name 'WebHost' does not exist in the current context

非常感谢任何解决方案、解决方法或提示。

2个回答

32

WebHost类位于Microsoft.AspNetCore程序集中,该程序集随Microsoft.AspNetCore.All NuGet包一起提供。因此,要解决您的问题,请安装此NuGet包并将以下using指令添加到源文件中:

using Microsoft.AspNetCore;


我的集成管理器不想要所有的dll,因为Microsoft.AspNetCore.All包含了100多个程序集来运行一个基本的.Net Core应用程序。有没有办法只包含依赖的程序集来构建和运行应用程序? - Manish Dubey
请查看此链接 - https://learn.microsoft.com/en-us/dotnet/core/deploying/ - CodeFuller
@ManishDubey 是的。我的答案在下面 - Erik Philips
1
所需的包名称是 Microsoft.AspNetCore。不要使用 .All 包,否则会遇到编译问题。 - mrmashal

4

正如CodeFuller的回答所示WebHost类可在程序集Microsoft.AspNetCore中使用。如果您不需要全部内容,只需获取Microsoft.AspNetCore包即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接