【问题标题】:Swagger not producing json file for the second methodSwagger 不为第二种方法生成 json 文件
【发布时间】:2020-05-26 07:09:42
【问题描述】:

好的,我有一个控制器和两个函数,但是当我尝试在 url 中大摇大摆时,我无法加载 json 文件,但我认为它与多个相同的操作有关

产生错误

获取错误内部服务器错误 /swagger/v1/swagger.json

我试过另一个问题建议的这条线

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

但是随后从 swagger 文档视图中隐藏了该方法,因此人们将如何测试它们。我在控制器中做错了什么。

public class BmiInformationsController : ControllerBase
{
    private readonly AppManagerDBContext _context;

    public BmiInformationsController(AppManagerDBContext context)
    {
        _context = context;
    }
    [HttpGet]
    public async Task<ActionResult<IEnumerable<BmiInformation>>> GetBmiInformation() {
        return await _context.BmiInformation.ToListAsync();
    }

    // GET: api/BmiInformations
    [HttpGet]
    public async Task<ActionResult<IEnumerable<BmiInformation>>> GetBmiInformationByUserId(string id)
    {
        return await _context.BmiInformation.Where(w => w.TennentId == new Guid(id)).ToListAsync();
    }        
}

这就是我配置我的招摇的方式

 services.AddSwaggerGen(c => {
   c.SwaggerDoc("v1", new OpenApiInfo { Title = "App Manager - Running Buddies", Version = "v1" });
   c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

   c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
   Description = "JWT Authorization header using the Bearer scheme.",
   Name = "Authorization",
   In = ParameterLocation.Header,
   Type = SecuritySchemeType.Http,
   Scheme = "bearer",
   BearerFormat = "JWT"

 });

我的启动文件中的路线。

app.UseEndpoints(endpoints => {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
});

【问题讨论】:

    标签: c# asp.net-core swagger asp.net-web-api2


    【解决方案1】:

    在控制器上方添加带有[Route]-attribute 的显式路由和如下操作方法:

    [ApiController]
    [Route("api")]
    public class BmiInformationsController : ControllerBase
    {
        private readonly AppManagerDBContext _context;
    
        public BmiInformationsController(AppManagerDBContext context)
        {
            _context = context;
        }
    
        [HttpGet]
        [Route("bmi/all")]
        public async Task<ActionResult<IEnumerable<BmiInformation>>> GetBmiInformation() {
            return await _context.BmiInformation.ToListAsync();
        }
    
        // GET: api/BmiInformations
        [HttpGet]
        [Route("bmi/{id}")]
        public async Task<ActionResult<IEnumerable<BmiInformation>>> GetBmiInformationByUserId(string id)
        {
            return await _context.BmiInformation.Where(w => w.TennentId == new Guid(id)).ToListAsync();
        }        
    }
    

    设置显式路由时,不再有冲突的动作,swagger 可以同时显示两个动作。

    您也可以删除c.ResolveConflictingActions(apiDescriptions =&gt; apiDescriptions.First());(这就是为什么swagger.json中没有包含第二种方法,您只在路由冲突的情况下采用第一种)。

    【讨论】:

    • 实际上,即使添加了与上面相同的两个错误,也没有工作相同的错误。
    • 更新了我的答案,试试这个!
    • 这招致谢了,因为他们不能拥有相同的路径。
    • 很高兴听到@rogue39nin!
    • 我总是忘记路线,但从那以后它确实很重要。
    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2016-03-24
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多