【问题标题】:I am using NSwag with an ASP.Net Core API and the Swagger UI client is displaying我正在将 NSwag 与 ASP.Net Core API 一起使用,并且 Swagger UI 客户端正在显示
【发布时间】:2019-07-25 03:40:18
【问题描述】:

我将 NSwag 与 ASP.Net Core API 一起使用,当我执行 Web API 并导航到 Swagger UI 时,它会显示以下错误:

正在获取资源列表:未定义。请稍等。它给出了 404 并告诉我无法读取未定义的属性“子字符串”,当我尝试跟踪错误时,它指向了 self.url.substring 中的 Swagger 客户端。虽然swagger.json中显示的json是完全正确的。

这是我的 Startup.cs 类,右侧的 Explorer 解决方案显示了我的 nuget 依赖项:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();

        // Enable the Swagger UI middleware and the Swagger generator
        app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
        {
            settings.SwaggerUiRoute = "/swagger";
            settings.PostProcess = document =>
            {
                document.Info.Version = "v1";
                document.Info.Title = "Analisis API";
                document.Info.Description = "A simple ASP.NET Core web API";
                document.Info.TermsOfService = "None";
                document.Info.Contact = new NSwag.SwaggerContact
                {
                    Name = "Example",
                    Email = "example@gmail.com",
                    Url = "http://google.es"
                };
                document.Info.License = new NSwag.SwaggerLicense
                {
                    Name = "Use under LICX",
                    Url = "https://example.com/license"
                };
            };

            app.UseMvc();
        });
}

这是我的控制器:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : Controller
{
    public IDatosAnalisis datosManager = new DatosAnalisis();
    public IResultado resultadoManager = new Resultado();

    [HttpGet]
    public ActionResult<String> GetDefault()
    {
        return "Bienvenido a AnalisisApi";
    }

    [HttpGet("getResultado/{patologiaId}")]
    [ProducesResponseType(typeof(ResultadoDTO), 200)]
    public ActionResult<ResultadoDTO> GetResultadoByPatologiaId(int patologiaId)
    {
        ResultadoDTO result = resultadoManager.getResultadoByPatologia(patologiaId);

        return result;
    }

    /// <summary>
    /// Receives the analisis data and evaluates them.
    /// </summary>
    [HttpPost]
    public ActionResult<List<ShortResultDTO>> TestValoresAnalisis(DatosSujetoDTO datosSujeto)
    {
        List<ShortResultDTO> results = datosManager.postDatosAnalisisAndGetPatologias(datosSujeto);

        return results;
    }
}

提前感谢您提供的任何帮助!

【问题讨论】:

    标签: asp.net-core swagger


    【解决方案1】:

    同样的问题,我的解决方法:使用自定义 url 访问 Swagger

    https://localhost:44336/swagger/index.html?url=/swagger/v1/swagger.json

    【讨论】:

    • 感谢吉平的回复,我也可以在屏幕上看到 swagger.json 但我仍然无法显示 Swagger 界面。最后我得到了信息,但我想知道为什么界面不起作用。
    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多