[英]error in showing view of a controller in asp.net core MVC
我正在用 asp.net core 实现一个项目。 我有几个控制器类。 我是通过 CRUD 方法创建的。 我有一个名为“ApiapplicantsController”的控制器,它的相关模型 Apiapplicant 是 Api 和申请人表的连接表。 在启动文件中,我将以下表达式指定为 URL 模式:
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.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using CSDDashboard.Data;
namespace CSDDashboard
{
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.AddControllersWithViews();
services.AddDbContext<CSDDashboardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CSDDashboardContext")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Apiapplicants}/{action=Index}/{id?}");
});
}
}
}
但是,在运行项目并输入 URL 后,出现以下错误:
处理请求时发生未处理的异常。 InvalidOperationException:尝试激活“CSDDashboard.Controllers.ApiapplicantsController”时无法解析“CSDDashboard.Models.CSSDDashboardContext”类型的服务。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
我在 appsetting 文件中创建了我的连接字符串,并为其指定了一个名为 CSDDashboard 的变量名。 appseting文件内容如下:
{
"AllowedHosts": "*",
"ConnectionStrings": {
"CSDDashboardContext": "Server=MOUSA-Z2;Database=CSSDDashboard; Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
我的 DBContext 类名称是 CSSDDashboardContext。 我已经通过 CRUD 方法创建了我所有的 Controllers 类。 但是,只有我的一个控制器使用 CSDDashboard 变量作为上下文类。 当我运行该项目并提供 URL 以调用 Vwreports 中的操作时,程序正常运行。
public class VwreportsController : Controller
{
private readonly CSDDashboardContext _context;
public VwreportsController(CSDDashboardContext context)
{
_context = context;
}
但是在其他控制器中 CSDDashboard 是未知的,系统使用 CSSDDashboard 作为上下文,当我调用其中一个控制器时,会向我显示提到的错误。 下面为其他有错误的控制器之一提供了示例:
public class ApisController : Controller
{
private readonly CSSDDashboardContext _context;
public ApisController(CSSDDashboardContext context)
{
_context = context;
}
如果有人帮助我解决问题,我将不胜感激。
问题是您没有注入名为“CSSDDashboardContext”的类,而只是注入名为“CSDDashboardContext”的类。
因此,所有在其构造函数中采用 CSDDashboardContext 的控制器都可以工作……而采用 CSSDDashboardContext 的控制器则不起作用。
您还需要注入 CSSDDashboardContext 类。
您需要像这样添加两个类:
//this one you are already adding it according to your code
services.AddDbContext<CSDDashboardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CSDDashboardContext")));
//Notice this is NOT the same class... Assuming this is a valid DBContext. You need to add this class as well.
services.AddDbContext<CSSDDashboardContext >(options =>
options.UseSqlServer(Configuration.GetConnectionString("CSDDashboardContext")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.