简体   繁体   English

尝试在 .NET CORE Web 应用程序中创建控制器时出错

[英]Error when trying to create a Controller in .NET CORE Web Application

I have a .NET CORE Web application created in Visual Studio 2017. It was created as a empty template.我有一个在 Visual Studio 2017 中创建的 .NET CORE Web 应用程序。它被创建为一个空模板。

The startup.cs has the below code startup.cs 有以下代码

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IInventoryServices, InventoryServices>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseMvcWithDefaultRoute();
    }

The program.cs is like below: program.cs 如下所示:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}

I tried to create a Controller.我试图创建一个控制器。 The type of the controller I selected to add was "MVC Controller with views, using Entity Framework".我选择添加的控制器类型是“MVC Controller with views, using Entity Framework”。 When trying to create, in the window i have specified the model class, and ticked for "Generate views", "Reference script libraries" and "Use layout page" which by the way are ticked by default.尝试创建时,在窗口中我指定了模型类,并勾选了“生成视图”、“参考脚本库”和“使用布局页面”,顺便说一下,默认情况下是勾选的。 The text-box to specify the Layout page is left blank.指定布局页面的文本框留空。

When trying to create the controller I get the below error:尝试创建控制器时,出现以下错误:

There was an error running the selected code generator: Scaffolding failed to edit Startup class to register the new Context using Dependency injection.运行选定的代码生成器时出错:脚手架无法编辑启动类以使用依赖项注入注册新的上下文。 Make sure there is a Startup class and a Configuration property in it确保其中有一个 Startup 类和一个 Configuration 属性

Couldn't figure out why this error is happening.无法弄清楚为什么会发生此错误。 Is it because of DI or Entity Context issue ?是因为 DI 还是实体上下文问题?

You would have to make sure that the DbContext is registered in the Startup class ConfigureServices() method as well.您还必须确保 DbContext 也在启动类ConfigureServices()方法中注册。

First Injection IConfiguration interface in Startup Class: Startup类中的第一次注入IConfiguration接口:

public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

then Add Below Code To ConfigureServices Method:然后添加以下代码到 ConfigureServices 方法:

services.AddDbContext<AppContext>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

and then Add ConnectionString Address in appsettings.json :然后在appsettings.json 中添加 ConnectionString 地址:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
  } 
}

You must first configure the dependency injection of the DbContext.必须先配置 DbContext 的依赖注入。 This tutorial explain step by step what you need to do.本教程逐步解释您需要做什么。

ASP.NET With Entity Framework 带有实体框架的 ASP.NET

Try Updating the packages in the projects using nuget package manager to the latest.尝试使用 nuget 包管理器将项目中的包更新到最新版本。

This solved the problem "I am using .Net 5.0" so this might work for you.这解决了“我正在使用 .Net 5.0”的问题,所以这可能对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试为Web API创建控制器时出错 - Error when trying to create a Controller for web api 尝试运行ASP .Net Core 2.1 Web应用程序时出现错误502.5 - Getting error 502.5 when trying to run a ASP .Net Core 2.1 Web application 尝试在 .net 核心 web api 中实现分页时出现 SqlException 错误 - SqlException error when trying to implement pagination in .net core web api 尝试创建控制器时出现“Web API 2 Ninject”错误 - Web API 2 Ninject “error occurred when trying to create a controller” 尝试从 ASP.NET Core 5 Web 应用程序运行 Api 控制器功能时找不到 404 - 404 not found when trying to run Api Controller function from ASP.NET core 5 web app .NET Core Web Application,将对象传递给控制器​​并将其转换为始终失败 - .NET Core Web Application, passing in an object to a controller and casting it always fails 在 Web api 控制器上调用方法时出错 - .Net Core 1.1 / EF 1.1 - Error when calling method on web api controller - .Net Core 1.1 / EF 1.1 尝试在 .NET Core 中更新数据库时出错 - Error when trying to update database in .NET Core ASP.NET Core Web Application using .NET 5.0: IFormFile always null when passing from view to controller - ASP.NET Core Web Application using .NET 5.0: IFormFile always null when passing from view to controller .NET Core-尝试将存储库添加到我的API控制器,但是当我执行每个控制器方法时,都会返回500错误 - .NET Core - Trying to add a repository to my API controller, but when I do every controller method returns a 500 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM