【发布时间】:2021-08-16 09:33:25
【问题描述】:
我创建了一个可用的 Rest API(它返回我期望在邮递员中的数据)。
但是当我尝试大摇大摆的时候,我得到了这个错误:
获取错误未定义/swagger/v1/swagger.json
Swagger 错误消息:
这是我的启动类中的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "TP_Service", Version = "v1" });
});
services.AddDbContext<CommandContext>(opt =>
opt.UseSqlServer(Configuration["Data:CommandAPIConnection:ConnectionString"]));
services.AddMvc(option => option.EnableEndpointRouting =
false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}
和
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment() || env.IsProduction())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestApi v1"));
}
app.UseMvc();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我做错了什么使其无法正常工作。 (当我在 IIS Express 上运行代码时会这样)
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TP_Service.Models;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace TP_Service.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ServiceController : ControllerBase
{
private readonly CommandContext _context;
public ServiceController(CommandContext context)
{
_context = context;
}
// GET: api/<ServiceController>
[HttpGet]
public ActionResult<IEnumerable<Service>> GetServices()
{
return _context.Services;
}
// GET api/<ServiceController>/5
[HttpGet("{id}")]
public ActionResult<ServiceVM> GetServiceDetails(int id)
{
ServiceVM serviceVM = new ServiceVM();
serviceVM.service = _context.Services.Find(id);
serviceVM.ServiceTaskList = _context.ServiceTasks.Select(
x => new ServiceTask()
{
Id = x.Id,
ServiceId = x.ServiceId,
CurrentAssemblyID = x.CurrentAssemblyID,
CurrentAssemblyName = x.CurrentAssemblyName,
TaskName = x.TaskName,
TaskQuoteName = x.TaskQuoteName,
TaskDescription = x.TaskDescription,
Notes = x.Notes,
ServiceTaskPartList = _context.ServiceTaskParts.Select(
x => new ServiceTaskPart()
{
Id = x.Id,
TaskId = x.TaskId,
PartId = x.PartId,
PartName = x.PartName,
QtyNeeded = x.QtyNeeded,
QtyUsed = x.QtyUsed,
QtyReturned = x.QtyReturned,
QtyLeftOnSite = x.QtyLeftOnSite
}
).Where(STP => STP.TaskId == x.Id).ToList()
}
).Where(ST => ST.ServiceId == id).ToList();
return serviceVM;
}
//// POST api/<ServiceController>
//[HttpPost]
// public void Post([FromBody] string value)
// {
// }
// // PUT api/<ServiceController>/5
// [HttpPut("{id}")]
// public void Put(int id, [FromBody] string value)
// {
// }
// // DELETE api/<ServiceController>/5
// [HttpDelete("{id}")]
// public void Delete(int id)
// {
// }
//}
}
【问题讨论】:
-
检查您的操作方法。也许您在某处有重复的路线。比如一个方法的路由是
api/items如果有另外一个方法也在使用同样的路由,swagger是不会打开的。 -
这是您的困惑解决方案,可能是您想要的解决方案。 LINK
-
这很可能是您的控制器中的错误。你能发布整个控制器代码吗?
-
我已将控制器添加到提交中
标签: c# swagger swashbuckle