C# 误差';名称';网络主机&x27;在当前上下文中不存在';
好的,这里有两个错误: 错误CS1061“IWebHost”不包含“Run”的定义,并且找不到接受“IWebHost”类型的第一个参数的可访问扩展方法“Run”(是否缺少using指令或程序集引用?)UserWebAPI C:\Users\cashamerica12345\Documents\UserWebAPI\UserWebAPI\Program.cs 17 Active 错误CS0103当前上下文UserWebAPI C:\Users\cashamerica12345\Documents\UserWebAPI\UserWebAPI\UserWebAPI\UserWebAPI\Program.cs中不存在名称“WebHost” 我所尝试的是确保我的Nuget软件包是正确的,起初我决定使用Microsoft.EntityFramework 2.1.0版而不是2.2.6版,很快更新到2.2.6版,并在项目属性中更改为使用.NET Core 2.2版,但不起作用,所以我回到了.NET Core 2.1版,仍然存在这个问题。我甚至尝试删除“obj”文件夹并重建解决方案。以下是我正在使用的Nuget软件包: Program.csC# 误差';名称';网络主机&x27;在当前上下文中不存在';,c#,asp.net-core,C#,Asp.net Core,好的,这里有两个错误: 错误CS1061“IWebHost”不包含“Run”的定义,并且找不到接受“IWebHost”类型的第一个参数的可访问扩展方法“Run”(是否缺少using指令或程序集引用?)UserWebAPI C:\Users\cashamerica12345\Documents\UserWebAPI\UserWebAPI\Program.cs 17 Active 错误CS0103当前上下文UserWebAPI C:\Users\cashamerica12345\Documents\
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace UserWebAPI
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UserWebAPI.Models
{
public class IdentityModel
{
public class ApplicationUser : IdentityUser
{
}
public class DataContext : IdentityDbContext<ApplicationUser>
{
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
}
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using UserWebAPI.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace UserWebAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
}
}
Startup.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace UserWebAPI
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace UserWebAPI.Models
{
public class IdentityModel
{
public class ApplicationUser : IdentityUser
{
}
public class DataContext : IdentityDbContext<ApplicationUser>
{
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
}
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using UserWebAPI.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace UserWebAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
}
}
使用Microsoft.AspNetCore.Identity;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用UserWebAPI.Models;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
命名空间UserWebAPI
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
}
}
}
该方法称为Start(),而不是Run()。假设是这样的:好吧,我期待运行,但我尝试了启动,它消除了第一个错误,但仍然说WebHost不存在称为“WebHost”的静态类(您似乎正在引用该类)确实似乎是.NET Core、2.0及更高版本所独有的。然而,在某些情况下,编译器在试图解释您给出的内容时遇到了重重困难,它甚至无法告诉您真正的问题在哪里。如果您缺少或额外的,则会发生这种情况代码>或{}
。考虑到您编写了代码,为什么还要使用Lambda语法?为什么不干脆把它变成一个bog standartt函数呢?我使用的是“Microsoft.AspNetCore”,这不是WebHost的来源吗?我没有写代码,我只是创建了一个新的ASP.net核心项目。所以基本上我可能在其他地方有问题?是的,我现在明白了,我没有看到任何红线,这让我很难理解为什么。但现在我明白了。该方法名为Start(),而不是Run()。假设是这样的:好吧,我期待运行,但我尝试了启动,它消除了第一个错误,但仍然说WebHost不存在称为“WebHost”的静态类(您似乎正在引用该类)确实似乎是.NET Core、2.0及更高版本所独有的。然而,在某些情况下,编译器在试图解释您给出的内容时遇到了重重困难,它甚至无法告诉您真正的问题在哪里。如果您缺少或额外的,则会发生这种情况代码>或{}
。考虑到您编写了代码,为什么还要使用Lambda语法?为什么不干脆把它变成一个bog standartt函数呢?我使用的是“Microsoft.AspNetCore”,这不是WebHost的来源吗?我没有写代码,我只是创建了一个新的ASP.net核心项目。所以基本上我可能在其他地方有问题?是的,我现在明白了,我没有看到任何红线,这让我很难理解为什么。但现在我明白了。