Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ASP.NET核心API连接microsoft sql Server_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 如何使用ASP.NET核心API连接microsoft sql Server

C# 如何使用ASP.NET核心API连接microsoft sql Server,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我对ASP.NET一无所知。我想创建一个小API。我尝试将该API与Microsoft Sql Server连接。但它给出了一个错误,即 InvalidOperationException:找不到所需的服务。请在应用程序启动代码中对“ConfigureServices(…)”的调用中调用“IServiceCollection.AddMvc”,添加所有必需的服务 我的Startup.cs文件是 using System; using System.Collections.Generic; usin

我对ASP.NET一无所知。我想创建一个小API。我尝试将该API与Microsoft Sql Server连接。但它给出了一个错误,即

InvalidOperationException:找不到所需的服务。请在应用程序启动代码中对“ConfigureServices(…)”的调用中调用“IServiceCollection.AddMvc”,添加所有必需的服务

我的
Startup.cs
文件是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
//using EFGetStarted.AspNetCore.ExistingDb.Models;
using Microsoft.EntityFrameworkCore;
using myApp.Models;

namespace myApp
{
    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)
        {
            var connection = @"Server=(JKCS-AREBY\SQLEXPRESS)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0";
            services.AddDbContext<TodoContext>(options => options.UseSqlServer(connection));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Microsoft.Extensions.Options;
//使用EFGetStarted.AspNetCore.ExistingDb.Models;
使用Microsoft.EntityFrameworkCore;
使用myApp.Models;
名称空间myApp
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
var connection=@“Server=(JKCS-AREBY\SQLEXPRESS)\mssqllocaldb;Database=Blogging;Trusted_connection=True;ConnectRetryCount=0”;
services.AddDbContext(options=>options.UseSqlServer(connection));
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}

该错误在app.UseMvc()中指出。有人能帮我解决这个问题吗?

在services.AddDbContext之后… 只需添加服务。AddMvc() (正如错误消息建议的那样)

public void配置服务(IServiceCollection服务)
{
var connection=@“Server=(JKCS-AREBY\SQLEXPRESS)\mssqllocaldb;Database=Blogging;Trusted_connection=True;ConnectRetryCount=0”;
services.AddDbContext(options=>options.UseSqlServer(connection));
services.AddMvc()
}

在services.AddDbContext之后… 只需添加服务。AddMvc() (正如错误消息建议的那样)

public void配置服务(IServiceCollection服务)
{
var connection=@“Server=(JKCS-AREBY\SQLEXPRESS)\mssqllocaldb;Database=Blogging;Trusted_connection=True;ConnectRetryCount=0”;
services.AddDbContext(options=>options.UseSqlServer(connection));
services.AddMvc()
}

先生,您能再解释一下吗?您还需要什么解释?您必须调用
services.AddMvc()
。这个例外告诉你的和这个答案一样多。404与你的问题无关。您需要向相关人员提出新问题。先生,请您再解释一下,好吗?您还需要什么解释?您必须调用
services.AddMvc()
。这个例外告诉你的和这个答案一样多。404与你的问题无关。你需要用相关代码问新问题
    public void ConfigureServices(IServiceCollection services)
        {
            var connection = @"Server=(JKCS-AREBY\SQLEXPRESS)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0";
            services.AddDbContext<TodoContext>(options => options.UseSqlServer(connection));
 services.AddMvc()
        }