【问题标题】:Get a swagger Error when opening Rest API打开 Rest API 时出现招摇错误
【发布时间】: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


【解决方案1】:

尝试导航到 https://localhost:{PortNo}/swagger/v1/swagger.json。您会注意到 api 配置是否有任何问题。

您还可以在开发工具中打开网络选项卡并寻找招摇的响应。 json

还要检查您的 api 路由。好像您有两个 http get 方法,以支持您必须更改路由配置(如果您还没有完成)。

【讨论】:

    【解决方案2】:

    请检查这部分

    • 检查您是否没有重复方法
    • 检查您的项目 xml 文档是否已启用(在项目属性中)
    • 检查所有必要的 nuget 安装(我将它们包括在此处)

    大摇大摆的nugets

    • Swashbuckle.AspNetCore
    • Swashbuckle.AspNetCore.Swagger
    • Swashbuckle.AspNetCore.SwaggerGen
    • Swashbuckle.AspNetCore.SwaggerUI

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2020-10-23
      • 2015-12-03
      • 1970-01-01
      相关资源
      最近更新 更多