Blazor 无法解析类型 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' 的服务
Blazor Unable to resolve service for type 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider'
我在使用路由向控制器添加端点时遇到问题。有人能帮我吗?
这是 Program.cs 的代码 我收到此错误:
System.InvalidOperationException:尝试调用中间件 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware' 时无法解析类型 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' 的服务。
在 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp,类型类型,类型中间件)
在 lambda_method1(闭包,对象,HttpContext,IServiceProvider)
在 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_1.b__2(HttpContext 上下文)
在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DBConnection")));
//Link interfaces with classes
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
这里是控制器的代码。
[Route("api/[controller]")]// for specifing the route where this controller is accessible
[ApiController]
public class EmployeesController : ControllerBase
{
private readonly IEmployeeRepository _employeeRepository;
public EmployeesController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
[HttpGet]
public async Task<IActionResult> GetEmployees()
{
try
{
return Ok(await _employeeRepository.GetEmployees());
}catch(Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database");
}
}
}
你不见了AddSwaggerGen()
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
我在使用路由向控制器添加端点时遇到问题。有人能帮我吗? 这是 Program.cs 的代码 我收到此错误:
System.InvalidOperationException:尝试调用中间件 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware' 时无法解析类型 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' 的服务。 在 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp,类型类型,类型中间件) 在 lambda_method1(闭包,对象,HttpContext,IServiceProvider) 在 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_1.b__2(HttpContext 上下文) 在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DBConnection")));
//Link interfaces with classes
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
这里是控制器的代码。
[Route("api/[controller]")]// for specifing the route where this controller is accessible
[ApiController]
public class EmployeesController : ControllerBase
{
private readonly IEmployeeRepository _employeeRepository;
public EmployeesController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
[HttpGet]
public async Task<IActionResult> GetEmployees()
{
try
{
return Ok(await _employeeRepository.GetEmployees());
}catch(Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database");
}
}
}
你不见了AddSwaggerGen()
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();