加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – ASP.NET Core – Swashbuckle没有创建swagger.j

发布时间:2020-12-16 04:38:13 所属栏目:asp.Net 来源:网络整理
导读:我无法获取Swashbuckle.AspNetCore(1.0.0)包来生成任何输出.我读过swagger.json文件应该写成’?/ swagger / docs / v1′.但是,我没有得到任何输出. 我从一个全新的ASP.NET Core API项目开始.我应该提到这是ASP.NET Core 2. API工作,我能够很好地从值控制器中
我无法获取Swashbuckle.AspNetCore(1.0.0)包来生成任何输出.我读过swagger.json文件应该写成’?/ swagger / docs / v1′.但是,我没有得到任何输出.

我从一个全新的ASP.NET Core API项目开始.我应该提到这是ASP.NET Core 2. API工作,我能够很好地从值控制器中检索值.

我的启动类具有与本文(Swashbuckle.AspNetCore on GitHub)中描述的完全相同的配置.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1",new Info { Title = "My API",Version = "v1" });
        });
    }

    // 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();

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json","MyAPI V1");
            });
        }
        else
        {
            app.UseExceptionHandler();
        }

        app.UseStatusCodePages();
        app.UseMvc();

        //throw new Exception();
    }
}

你可以看到NuGet参考…

同样,这是所有默认模板,但我包含ValuesController以供参考…

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1","value2" };
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    [HttpPost]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put(int id,[FromBody]string value)
    {
    }

    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

解决方法

我相信你在配置上错过了这两行:
if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();

    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json","MyAPI V1");
    });
}

要访问Swagger UI,URL应为:http://localhost:XXXX/swagger/

json可以在Swagger UI的顶部找到:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读