ASP.NET Core Swagger错误:“获取错误内部服务器错误”

ryhaxcpt  于 2023-10-18  发布在  .NET
关注(0)|答案(2)|浏览(146)

我正在使用所有最新的Nuget Packages for ASP.NET Core Rest Server with Swagger
我无法找到如何修复底部描述的错误(也有代码)。我试过:

  • 正在添加Route属性
  • HttpGet属性添加路径
  • 删除参数名称

我总是得到下面描述的错误:
内部服务器错误...
在“输出”窗口中:
“Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:操作的Ambient HTTP方法-CommandDER.WebService.Controllers.GensetController.GetGensetsByMicrogridId(CommandDER.WebService)。操作需要Swagger/OpenAPI 3.0的显式HttpMethod绑定”

问题

  • 为什么RouteHttpGet似乎提供相同的东西?=>在StackOverflow找到答案,我应该使用HttpGet进行GET操作。
  • 为什么这两种方式似乎都不能消除动作的歧义?

错误代码:

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:Ambient HTTP方法用于操作-
CommandDER.WebService.Controllers.GensetController.GetGensetsByMicrogridId(CommandDER.WebService).操作需要Swagger/OpenAPI 3.0的显式HttpMethod绑定
在Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEEE801 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable 1 apiDescriptions,SchemaRepository schemaRepository)
在Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerDocumentWithoutFilters(String documentName,String host,String basePath)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(String documentName,String host,String basePath)
在Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext上下文)
在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext上下文)
代码:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CommanDER.Data;
using CommanDER.Domain.ResourceDef;

namespace CommanDER.WebService.Controllers
{
    /// <summary>
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class GensetController : ControllerBase
    {
        /// <summary>
        /// Get all resources of a Microgrid by the microgridId
        /// </summary>
        /// <param name="microgridId"></param>
        /// <returns></returns>
        [Route("GetGensetsByMicrogridId/{microgridId}")]
        [HttpGet("GetGensetsByMicrogridId/{microgridId}")]
        public IEnumerable<Genset> GetGensetsByMicrogridId(int microgridId)
        {
            return _context.Gensets.Where(g => g.MicrogridId == microgridId).ToList();
        }

        /// <summary>
        /// Get Genset by it's ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("GetGensetById/{id}")]
        [HttpGet("GetGensetById/{id}")]
        public async Task<ActionResult<Genset>> GetGensetById(int id)
        {
            var genset = await _context.Gensets.FindAsync(id);

            if (genset == null)
            {
                return NotFound();
            }

            return genset;
        }

        private readonly MicroReseauDbContext _context;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public GensetController(MicroReseauDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// Get all gensets in the Database. Could be for more than one Microgrid (if applied).
        /// </summary>
        /// <returns></returns>
        [Route("All")]
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Genset>>> GetGensets()
        {
            return await _context.Gensets.ToListAsync();
        }
    }
}
rvpgvaaj

rvpgvaaj1#

如果你删除[Route(“xxx”)]行,你应该没问题。不需要使用“HttpGet”和“Route”

wrrgggsh

wrrgggsh2#

我发现了我的问题:
我删除了“路线”属性,它解决了我的问题。
这两个(HttpGet和Route)似乎不应该一起使用。太糟了,没有信息告诉你吗??嗯!我知道如果我知道我在做什么就不会发生了。但我仍然认为这将是一个很好的验证,至少在2010年。

相关问题