c# - ASP.NET Core - Swashbuckle 未创建 swagger.json 文件

标签 c# asp.net-core swagger swashbuckle

我在获取 Swashbuckle.AspNetCore (1.0.0) 包生成任何输出时遇到问题。我读到 swagger.json 文件应该写入“~/swagger/docs/v1”。但是,我没有得到任何输出。

我从一个全新的 ASP.NET Core API 项目开始。我应该提到这是 ASP.NET Core 2。API 可以工作,并且我能够很好地从值 Controller 检索值。

我的启动类的配置与本文中描述的完全相同 ( 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 引用...

enter image description here

同样,这都是默认模板,但我包含了 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)
    {
    }
}

最佳答案

我也遇到了同样的问题。检查http://localhost:XXXX/swagger/v1/swagger.json 。如果出现任何错误,请修复它们。

例如,我在基本 Controller 类中的路由不明确,并收到错误:“操作的 HTTP 方法不明确。操作需要 Swagger 2.0 的显式 HttpMethod 绑定(bind)。”。 如果您使用基本 Controller ,请确保您的公共(public)方法使用 HttpGet/HttpPost/HttpPut/HttpDelete 或 Route 属性以避免路由不明确。

然后,我还在同一个方法中定义了 HttpGet("route") 和 Route("route") 属性,这是 swagger 的最后一个问题。

关于c# - ASP.NET Core - Swashbuckle 未创建 swagger.json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450262/

相关文章:

c# - WatiN doubleclick 在 IE9 中不起作用

C# 和反射不能连续工作两次

C#获取电脑MAC地址 "OFFLINE"

signalr - 如何在 ASP.NET 5 中使用 SignalR

c# - 删除和选项请求得到 401 未经授权的响应

c# - Swashbuckle:具有相同状态代码但不同描述/模型的多个 [SwaggerResponse]

java - Swagger API 控制台无法在 WSO2 中工作

clojure - compojure-api/schema/swagger 中的非必需参数?

c# - 当我执行沮丧时实际上发生了什么?

azure - 消息未到达 Azure SignalR 服务