C# 打开Rest API时出现虚张声势错误
我已经创建了一个可以工作的RESTAPI(它返回我在postman中期望的数据) 但当我试着大摇大摆的时候,我得到了一个错误: 获取错误未定义/swagger/v1/swagger.json 招摇过市错误消息: 下面是我的startup类中的代码C# 打开Rest API时出现虚张声势错误,c#,swagger,swashbuckle,C#,Swagger,Swashbuckle,我已经创建了一个可以工作的RESTAPI(它返回我在postman中期望的数据) 但当我试着大摇大摆的时候,我得到了一个错误: 获取错误未定义/swagger/v1/swagger.json 招摇过市错误消息: 下面是我的startup类中的代码 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c =>
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);
}
我做错了什么使它不能工作。(当我在IIS Express上运行代码时会发生)
使用Microsoft.AspNetCore.Mvc;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用TP_服务模型;
//有关为空项目启用Web API的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=397860
命名空间TP_Service.Controllers
{
[路由(“api/[控制器]”)]
[ApiController]
公共类服务控制器:ControllerBase
{
私有只读命令上下文_上下文;
公共服务控制器(CommandContext上下文)
{
_上下文=上下文;
}
//获取:api/
[HttpGet]
公共操作结果GetServices()
{
返回上下文服务;
}
//获取api//5
[HttpGet(“{id}”)]
公共操作结果GetServiceDetails(int id)
{
ServiceVM ServiceVM=新ServiceVM();
serviceVM.service=_context.Services.Find(id);
serviceVM.ServiceTaskList=\u context.ServiceTasks.Select(
x=>newServiceTask()
{
Id=x.Id,
ServiceId=x.ServiceId,
CurrentAssemblyID=x.CurrentAssemblyID,
CurrentAssemblyName=x.CurrentAssemblyName,
TaskName=x.TaskName,
TaskQuoteName=x.TaskQuoteName,
TaskDescription=x.TaskDescription,
注释=x.注释,
ServiceTaskPartList=\u context.ServiceTaskParts.Select(
x=>newServiceTaskPart()
{
Id=x.Id,
TaskId=x.TaskId,
PartId=x.PartId,
PartName=x.PartName,
qtyneed=x.qtyneed,
QtyUsed=x.QtyUsed,
QtyReturned=x.QtyReturned,
QtyLeftOnSite=x.QtyLeftOnSite
}
).Where(STP=>STP.TaskId==x.Id).ToList()
}
).Where(ST=>ST.ServiceId==id).ToList();
返回serviceVM;
}
////后api/
//[HttpPost]
//公共作废帖子([FromBody]字符串值)
// {
// }
////放置api//5
//[HttpPut(“{id}”)]
//公共void Put(int id,[FromBody]字符串值)
// {
// }
////删除api//5
//[HttpDelete(“{id}”)]
//公共无效删除(int-id)
// {
// }
//}
}
检查您的操作方法。也许你在某处有重复的路线。例如,一个方法的路径是api/items
,如果有另一个方法也使用相同的路径,则不会打开swagger。这是您困惑的解决方案,可能是您想要的解决方案。这很可能是控制器中的错误。你能发布整个控制器代码吗?我已经将控制器添加到提交中
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();
});
}
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)
// {
// }
//}
}