简体   繁体   English

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

[英]ASP.NET Core - Swashbuckle not creating swagger.json file

I am having trouble getting the Swashbuckle.AspNetCore (1.0.0) package to generate any output. I read the swagger.json file should be written to '~/swagger/docs/v1'.我无法让 Swashbuckle.AspNetCore (1.0.0) package 生成任何 output。我读到 swagger.json 文件应该写入“~/swagger/docs/v1”。 However, I am not getting any output.但是,我没有收到任何 output。

I started with a brand new ASP.NET Core API project.我从一个全新的 ASP.NET Core API 项目开始。 I should mention this is ASP.NET Core 2. The API works, and I am able to retrieve values from the values controller just fine.我应该提到这是 ASP.NET Core 2。API 有效,我能够从值 controller 中检索值就好了。

My startup class has the configuration exactly as described in this article ( Swashbuckle.AspNetCore on GitHub ).我的启动 class 的配置与本文中描述的完全相同( GitHub 上的 Swashbuckle.AspNetCore )。

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

You can see the NuGet references...您可以看到 NuGet 参考...

在此处输入图像描述

Again, this is all the default template, but I include the ValuesController for reference...同样,这都是默认模板,但我包含了 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)
    {
    }
}

I had the same problem.我有同样的问题。 Check http://localhost:XXXX/swagger/v1/swagger.json .检查http://localhost:XXXX/swagger/v1/swagger.json If you get any a errors, fix them.如果您遇到任何错误,请修复它们。

For example, I had an ambiguous route in a base controller class and I got the error: "Ambiguous HTTP method for action. Actions require an explicit HttpMethod binding for Swagger 2.0.".例如,我在基本控制器类中有一条不明确的路由,并收到错误消息:“用于操作的不明确的 HTTP 方法。操作需要 Swagger 2.0 的显式 HttpMethod 绑定。”。 If you use base controllers make sure your public methods use the HttpGet/HttpPost/HttpPut/HttpDelete OR Route attributes to avoid ambiguous routes.如果您使用基本控制器,请确保您的公共方法使用 HttpGet/HttpPost/HttpPut/HttpDelete 或 Route 属性以避免模糊路由。

Then, also, I had defined both HttpGet("route") AND Route("route") attributes in the same method, which was the last issue for swagger.然后,我也在同一个方法中定义了 HttpGet("route") 和 Route("route") 属性,这是招摇的最后一个问题。

I believe you missed these two lines on your Configure:我相信您在配置中错过了这两行:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();

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

To access Swagger UI, the URL should be: http://localhost:XXXX/swagger/要访问 Swagger UI,URL 应该是: http://localhost:XXXX/swagger/

The json can be found at the top of Swagger UI: json 可以在 Swagger UI 的顶部找到:

在此处输入图像描述

如果您的应用程序托管在 IIS/IIS Express 上,请尝试以下操作:

c.SwaggerEndpoint("../swagger/v1/swagger.json", "MyAPI V1");
#if DEBUG
   // For Debug in Kestrel
   c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
#else
   // To deploy on IIS
   c.SwaggerEndpoint("/webapi/swagger/v1/swagger.json", "Web API V1");
#endif

When deployed to IIS webapi(base URL) is the Application Alias.当部署到 IIS webapi(base URL) 是应用程序别名。 You need to keep Application Alias(base URL) same for all IIS deployments because swagger looks for swagger.json at "/swagger/v1/swagger.json" location but wont prefix application Alias(base URL) that is the reason it wont work.您需要为所有 IIS 部署保持应用程序别名(基本 URL)相同,因为 swagger 在“/swagger/v1/swagger.json”位置查找 swagger.json,但不会为应用程序别名(基本 URL)添加前缀,这就是它不起作用的原因.

For Example:例如:

localhost/swagger/v1/swagger.json - Couldn't find swagger.json localhost/swagger/v1/swagger.json - 找不到 swagger.json

I was running into a similar, but not exactly the same issue with swagger.我在招摇时遇到了类似但不完全相同的问题。 Hopefully this helps someone else.希望这对其他人有帮助。

I was using a custom document title and was not changing the folder path in the SwaggerEndPoint to match the document title.我使用的是自定义文档标题,并且没有更改 SwaggerEndPoint 中的文件夹路径以匹配文档标题。 If you leave the endpoint pointing to swagger/v1/swagger.json it won't find the json file in the swagger UI.如果您让端点指向 swagger/v1/swagger.json,它将不会在 swagger UI 中找到 json 文件。

Example:例子:

services.AddSwaggerGen(swagger =>
{
    swagger.SwaggerDoc("AppAdministration", new Info { Title = "App Administration API", Version = "v1.0" });
            
});


app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/AppAdministration/swagger.json", "App Administration");
});

You must conform to 2 rules:您必须遵守 2 条规则:

  1. Decorate all actions with explicit Http Verbs like [HttpGet("xxx")] , [HttpPost("xxx")] , ... instead of [Route("xxx")] .用明确的 Http 动词装饰所有动作,例如[HttpGet("xxx")][HttpPost("xxx")] ,...而不是[Route("xxx")]
  2. Decorate public methods in controllers with [NonAction] Attribute.使用[NonAction]属性装饰控制器中的公共方法。

Note that http://localhost:XXXX/swagger/ page requests for http://localhost:XXXX/swagger/v1/swagger.json file, but an Exception will occur from Swagger if you wouldn't conform above rules.请注意, http://localhost:XXXX/swagger/页面请求http://localhost:XXXX/swagger/v1/swagger.json文件,但如果您不遵守上述规则,Swagger 会出现异常。

After watching the answers and checking the recommendations, I end up having no clue what was going wrong.在查看答案并检查建议后,我最终不知道出了什么问题。

I literally tried everything.我真的尝试了一切。 So if you end up in the same situation, understand that the issue might be something else, completely irrelevant from swagger.因此,如果您最终处于相同的情况,请了解问题可能是其他问题,与招摇完全无关。

In my case was a OData exception.在我的情况下是一个OData异常。

Here's the procedure:这是程序:

1) Navigate to the localhost:xxxx/swagger 1)导航到localhost:xxxx/swagger
2) Open Developer tools 2)打开开发者工具
3) Click on the error shown in the console and you will see the inner exception that is causing the issue. 3)单击控制台中显示的错误,您将看到导致问题的内部异常。

I am moving my comment to an answer since it appears to be helpful.我将我的评论移至答案,因为它似乎很有帮助。


To avoid issues with IIS aliases, remove /swagger/ from the URL path.为避免 IIS 别名出现问题,请从 URL 路径中删除 /swagger/。 It should look like this:它应该如下所示:

app.UseSwaggerUI(c => { c.SwaggerEndpoint("v1/swagger.json", "API name"); });

If you have any issues in your controller to map to an unique URL you get this error.如果您的控制器中有任何问题无法映射到唯一的 URL,您会收到此错误。

The best way to find the cause of issue is exclude all controllers from project.找出问题原因的最佳方法是从项目中排除所有控制器。 Then try running the app by enabling one controller or one or more methods in a controller at a time to find the controllers/ controller method(S) which have an issue.然后尝试通过一次启用一个控制器或控制器中的一个或多个方法来运行应用程序,以查找有问题的控制器/控制器方法。 Or you could get smart and do a binary search logic to find the disable enable multiple controller/methods to find the faulty ones.或者您可以变得聪明并执行二进制搜索逻辑来查找禁用启用多个控制器/方法以找到有故障的控制器/方法。

Some of the causes is一些原因是

  1. Having public methods in controller without HTTP method attributes在没有 HTTP 方法属性的控制器中有公共方法

  2. Having multiple methods with same Http attributes which could map to same api call if you are not using "[action]" based mapping如果您不使用基于“[action]”的映射,则具有多个具有相同 Http 属性的方法可以映射到相同的 api 调用

  3. If you are using versioning make sure you have the method in all the controller versions (if using inheritance even though you use from base)如果您正在使用版本控制,请确保您在所有控制器版本中都有该方法(如果使用继承,即使您从基础使用)

I don't know if this is useful for someone, but in my case the problem was that the name had different casing.我不知道这对某人是否有用,但就我而言,问题在于名称的大小写不同。

V1 in the service configuration - V capital letter服务配置中的V1 —— V大写字母
v1 in Settings -- v lower case设置中的v1 -- v小写

The only thing I did was to use the same casing and it worked.我做的唯一一件事就是使用相同的外壳并且它起作用了。

带有大写 V 的版本名称

You actually just need to fix the swagger url by removing the starting backslash just like this :实际上,您只需要通过删除起始反斜杠来修复 swagger url,就像这样:

c.SwaggerEndpoint("swagger/v1/swagger.json", "MyAPI V1");

instead of :代替 :

c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI V1");

A common error that we make when use Swagger is to give the same name to(NET ASP) two or more routes.我们在使用 Swagger 时犯的一个常见错误是为(NET ASP)两个或多个路由提供相同的名称。 this cause that swagger cannot generate the JSON file.这导致 swagger 无法生成 JSON 文件。 for example, this is a wrong way例如,这是错误的方式

[HttpPost, Route("Start")]
public async Task<TransactionResult> WipStart(BodyWipStartDTO data)
{
    return await _wipServices.WipStart(data);
}

Other action with the same route name but different action name其他具有相同路由名称但不同动作名称的动作

[HttpPost, Route("Start")]
public async Task<TransactionResult> WipAbort(BodyWipStartDTO data)
{
    return await _wipServices.WipAbort(data);
}

This a correct way这是正确的方法

[HttpPost, Route("Start")]
public async Task<TransactionResult> WipStart(BodyWipStartDTO data)
{
    return await _wipServices.WipStart(data);
}

[HttpPost, Route("Abort")]
public async Task<TransactionResult> WipAbort(BodyWipStartDTO data)
{
    return await _wipServices.WipAbort(data);
}

Adding a relative path worked for me:添加相对路径对我有用:

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("../swagger/v1/swagger.json", "My App");
});

Personally I had the same issue and when I tried again today after a while I found in the new version (2.5.0) that going in the json I could see an explanation of the error that was in here .就我个人而言,我遇到了同样的问题,当我今天再次尝试时,我发现在新版本(2.5.0)中进入json我可以看到对此处错误的解释。

Also another thing that helped to fix it to me was removing the hosting information connected to the website that is hold inside "..vs\config\applicationhost.config" at the root of the solution folder帮助我解决它的另一件事是删除连接到解决方案文件夹根目录“..vs\config\applicationhost.config”中的网站的托管信息

I removed the element that was configuring the website.我删除了配置网站的元素。

           <site name="**" id="9">
              <application path="/" applicationPool=""></application>
              <bindings></bindings>
           </site>

Try to follow these steps, easy and clean.尝试遵循这些步骤,简单而干净。

  1. Check your console are you getting any error like " Ambiguous HTTP method for action. Actions require an explicit HttpMethod binding for Swagger 2.0. "检查您的控制台是否收到任何错误,例如“ Ambiguous HTTP method for action. Actions require an explicit HttpMethod binding for Swagger 2.0.
  2. If YES : Reason for this error: Swagger expects如果:此错误的原因:Swagger 预计

each endpoint should have the method (get/post/put/delete)每个端点都应该有方法(get/post/put/delete)

. . Solution :解决方案

Revisit your each and every controller and make sure you have added expected method.重新访问您的每个控制器,并确保您添加了预期的方法。

(or you can just see in console error which controller causing ambiguity) (或者您可以在控制台错误中看到哪个控制器导致歧义)

  1. If NO .如果 Please let us know your issue and solution if you have found any.如果您发现任何问题,请告诉我们您的问题和解决方案。

I had this problem when I used a inner class in Post parameters当我在 Post 参数中使用内部类时遇到了这个问题

[HttpPost]
public async Task Post([FromBody] Foo value)
{
}

Where Foo is福在哪里

public class Foo
{
    public IEnumerable<Bar> Bars {get;set;}

    public class Bar
    {
    }
}

Same problem - easy fix for me.同样的问题-对我来说很容易解决。

To find the underlying problem I navigated to the actual swagger.json file which gave me the real error为了找到根本问题,我导航到了实际的 swagger.json 文件,这给了我真正的错误

/swagger/v1/swagger.json

The actual error displayed from this Url was此网址显示的实际错误是

NotSupportedException: Ambiguous HTTP method for action  ... Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0

The point being重点是

Actions require an explicit HttpMethod

I then decorated my controller methods with [HttpGet]然后我用 [HttpGet] 装饰了我的控制器方法

[Route("GetFlatRows")]
 [HttpGet]
 public IActionResult GetFlatRows()
 {

Problem solved问题解决了

Make sure you have all the required dependencies, go to the url xxx/swagger/v1/swagger.json you might find that you're missing one or more dependencies.确保您拥有所有必需的依赖项,转到 url xxx/swagger/v1/swagger.json 您可能会发现缺少一个或多个依赖项。

在此处输入图像描述

I was getting this Swagger error when I created Version 2 of my api using version headers instead of url versioning.当我使用版本标头而不是 url 版本控制创建我的 api 的版本 2 时,我收到了这个 Swagger 错误。 The workaround was to add [Obsolete] attributes to the Version 1 methods then use SwaggerGeneratorOptions to ignore the obsolete api methods in Startup -> ConfigureServices method.解决方法是将[Obsolete]属性添加到版本 1 方法,然后使用SwaggerGeneratorOptions忽略Startup -> ConfigureServices方法中过时的 api 方法。

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

I had the same problem.我有同样的问题。 I was using swagger like below mentioned pattern ie "../swagger/v1/swagger.json" because I am using IIS Express.Later than I change it to "/swagger/v1/swagger.json"and clean,rebuild the solution worked for me.我正在使用像下面提到的模式那样的招摇,即“../swagger/v1/swagger.json”,因为我使用的是 IIS Express。后来我将其更改为“/swagger/v1/swagger.json”并清理,重建解决方案为我工作。

Swagger 启用 UI

You might forgetting to include.. StartUp.cs/Configure()您可能忘记包含.. StartUp.cs/Configure()

app.UseSwagger();

Check if you forgot to include, you error must be remove.检查您是否忘记包含,必须删除您的错误。

I'd a similar issue, my Swagger documentation broke after I was adding async version of APIs to existing ones.我有一个类似的问题,在我将异步版本的 API 添加到现有的 API 之后,我的 Swagger 文档中断了。 I played around the Swagger DLL's by installing / Reinstalling, finally commenting newly added APIs, and it worked.我通过安装/重新安装来玩 Swagger DLL,最后评论新添加的 API,它工作正常。 Then I added different signature in attributes, and bingo!, It worked.然后我在属性中添加了不同的签名,然后宾果游戏!,它起作用了。

In your case, you are having two API with matching signatures在您的情况下,您有两个具有匹配签名的 API

[HttpGet]
public IEnumerable<string> Get()
{
  return new string[] { "value1", "value2" };
}

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

Try providing different names in attributes like
[HttpGet("List")]
public IEnumerable<string> Get()
{
 return new string[] { "value1", "value2" };
}

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

This should solve the issue.这应该可以解决问题。

Be aware that in Visual Studio 2022 and .NetCore 6 if you create a new ASP.NET Core Web App , Program.cs has the oposite check for Development environment.请注意,在 Visual Studio 2022 和 .NetCore 6 中,如果您创建新的ASP.NET Core Web 应用程序,Program.cs 会检查开发环境。

instead of代替

if (app.Environment.IsDevelopment())
{
   app.UseSwagger();
   app.UseSwaggerUI();
}

you will find你会找到

if (!app.Environment.IsDevelopment())
{
     app.UseExceptionHandler("/Home/Error");
}
// You shoukd add swagger calls here 
app.UseSwagger();
app.UseSwaggerUI();

If you create a new project by selecting the template ASP.NET Core Web API and check "Enable OpenAPI support" you will have different Program.cs with preinstalled swagger package and related code.如果您通过选择模板ASP.NET Core Web API 创建一个新项目并选中“启用 OpenAPI 支持” ,您将拥有不同的 Program.cs,其中预装了 swagger 包和相关代码。

This took some time for me to find, hope to help someone.这花了我一些时间来寻找,希望对某人有所帮助。

I have came across the same issue, and noticed that my API has not hosted in the root folder and in an virtual directory.我遇到了同样的问题,并注意到我的 API 没有托管在根文件夹和虚拟目录中。 I moved my API to the root folder in IIS and worked.我将我的 API 移动到 IIS 的根文件夹并开始工作。

More info in this answer 此答案中的更多信息

Take a look on Chrome developer tools, sometimes, swagger.json request throws http 500, witch means that there is some inconsistency on your controllers.看看 Chrome 开发者工具,有时,swagger.json 请求会抛出 http 500,这意味着你的控制器上存在一些不一致。 For example: In my case, there is an "Ambiguous HTTP method for action":例如:在我的例子中,有一个“Ambiguous HTTP method for action”:

在此处输入图像描述

Also I had an issue because I was versioning the application in IIS level like below:我也遇到了一个问题,因为我在 IIS 级别对应用程序进行版本控制,如下所示:

在此处输入图像描述

If doing this then the configuration at the Configure method should append the version number like below:如果这样做,那么 Configure 方法中的配置应附加版本号,如下所示:

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/1.0/swagger/V1/swagger.json", "Static Data Service");
});

I was able to fix and understand my issue when I tried to go to the swagger.json URL location:当我尝试转到 swagger.json URL 位置时,我能够解决并理解我的问题:

https://localhost:XXXXX/swagger/v1/swagger.json

The page will show the error and reason why it is not found.该页面将显示错误和未找到的原因。

In my case, I saw that there was a misconfigured XML definition of one of my methods based on the error it returned:就我而言,我看到我的一种方法的 XML 定义配置错误,这是基于它返回的错误:

NotSupportedException: HTTP method "GET" & path "api/Values/{id}" overloaded by actions - ...
...
...

In my case problem was in method type, should be HttpPOST but there was HttpGET Once I changed that, everything starts work.在我的情况下,问题在于方法类型,应该是 HttpPOST 但有 HttpGET 一旦我改变了它,一切都开始工作了。

https://c2n.me/44p7lRd.png https://c2n.me/44p7lRd.png

You should install the following packages into your project.您应该将以下软件包安装到您的项目中。

5.0.0-rc4 version of Swashbuckle is the minimum. 5.0.0-rc4版本的 Swashbuckle 是最低要求。 Otherwise, it won't work.否则,它将无法正常工作。

As of now, directly installing it from Nuget, installs the old versions which won't work for Core 3.截至目前,直接从 Nuget 安装它会安装不适用于 Core 3 的旧版本。

I inserted the following lines into .csproj project file like that:我将以下行插入 .csproj 项目文件,如下所示:

<PackageReference Include="Microsoft.OpenApi" Version="1.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.0.0-rc4" />

After that, Rebuild installs the newer versions.之后,Rebuild 会安装较新的版本。 If not, you can use restore too.如果没有,您也可以使用还原。

In the Startup.cs, you should configure Swashbuckle like that:在 Startup.cs 中,您应该像这样配置 Swashbuckle:

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

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            });

        services.AddMvc();
    }

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

        app.UseHttpsRedirection();

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            //c.RoutePrefix = String.Empty;
        });

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

Just go to the " https://localhost:5001/swagger/index.html " and you'll see the Swagger UI.只需转到“ https://localhost:5001/swagger/index.html ”,您就会看到 Swagger UI。 (5001 is my local port, you should change it with yours) (5001是我的本地端口,你应该用你的改变它)

It took a little time for me to figure it out.我花了一点时间才弄清楚。

I hope it will help others :)我希望它会帮助别人:)

Answer:回答:

If using directories or application  with IIS or a reverse proxy,<br/> set the Swagger endpoint to a relative path using the ./ prefix. For example,<br/> ./swagger/v1/swagger.json. Using /swagger/v1/swagger.json instructs the app to<br/>look for the JSON file at the true root of the URL (plus the route prefix, if used). For example, use http://localhost:<br/><br/><port>/<route_prefix>/swagger/v1/swagger.json instead of http://localhost:<br/><port>/<virtual_directory>/<route_prefix>/swagger/v1/swagger.json.<br/>
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");
//Add dot in front of swagger path so that it takes relative path in server
c.SwaggerEndpoint("./swagger/v1/swagger.json", "MyAPI V1");
    });
}

[Detail description of the swagger integration to web api core 3.0][1]


  [1]: https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio

I had a silly issue, I had a capital v in the AddSwaggerGen method and a lowercase v in c.SwaggerEndpoint.我有一个愚蠢的问题,我在 AddSwaggerGen 方法中有一个大写的 v,在 c.SwaggerEndpoint 中有一个小写的 v。

It appears to be case sensitive.它似乎区分大小写。

According to Microsoft: To serve the Swagger UI at the app's root ( http://localhost:/ ), set the RoutePrefix property to an empty string:根据微软的说法:要在应用程序的根目录 ( http://localhost:/ ) 提供 Swagger UI,请将 RoutePrefix 属性设置为空字符串:

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});

i had the same Error , but due to different issue ,i was using a 3rd party library which cause this issue, and i did not need to have this library in my exposure ,so i used the below solution that is posted here .我有同样的错误,但由于不同的问题,我使用了导致这个问题的第 3 方库,我不需要在我的曝光中拥有这个库,所以我使用了下面发布解决方案。

Create somewhere class ApiExplorerIgnores with following content使用以下内容创建某处类 ApiExplorerIgnores

public class ApiExplorerIgnores : IActionModelConvention
{
    public void Apply(ActionModel action)
    {
        if (action.Controller.ControllerName.Equals("ImportExport"))
            action.ApiExplorer.IsVisible = false;
    }
}

Add following code to method ConfigureServices in Startup.cs将以下代码添加到 Startup.cs 中的方法 ConfigureServices

services.AddMvc(c => c.Conventions.Add(new ApiExplorerIgnores()))

this will get read of all methods in that controller ,you can also use a specific level ,like method name or so ,also you can remove one Method only and so one ,这将读取该控制器中的所有方法,您也可以使用特定级别,例如方法名称左右,也可以仅删除一个方法等等,

就我而言,我忘记为方法设置public访问修饰符!

// Enable middleware to serve generated Swagger as a JSON endpoint.

app.UseSwagger(c =>
{
    c.SerializeAsV2 = true;
});

// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Name");
});

Please use the following convention for simple API docs.请对简单的 API 文档使用以下约定。

In ConfigureServices methodConfigureServices方法中

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My Test API", Version = "1.0.0.0" });
            c.OperationFilter<SwaggerAuthorizationOperationFilter>();
            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
            {
                Description = "Enter the request header in the following box to add Jwt To grant authorization Token: Bearer Token",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.ApiKey,
                BearerFormat = "JWT",
                Scheme = "Bearer"
            });
            //c.SchemaFilter<SwaggerExcludeSchemaFilter>();
            c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference {
                            Type = ReferenceType.SecurityScheme,
                            Id = "Bearer"
                        }
                    },
                    new string[] { }
                }
                });
        });

In Configure methodConfigure方法中

        app.UseSwagger();
        app.UseSwaggerUI(c =>
            {
                c.DefaultModelExpandDepth(2);
                c.DefaultModelsExpandDepth(-1);
                c.DisplayOperationId();
                c.DisplayRequestDuration();
                c.EnableDeepLinking();
                c.EnableFilter();
                c.ShowExtensions();
                c.EnableValidator();
            });

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test API");
            c.RoutePrefix = "";
            c.DocumentTitle = "My Test API Docs";                
        });

Please note following 2 lines form those 2 methods sequentially.请注意以下 2 行依次构成这两种方法。

c.SwaggerDoc("v1", new OpenApiInfo { Title = "My Test API", Version = "1.0.0.0" });

c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test API");

So whatever you put instead of v1 that should be matched.所以无论你放什么而不是v1都应该匹配。

In my case, multiple endpoints inside my controller have the same HttpPost attribute name.在我的例子中,我的 controller 中的多个端点具有相同的HttpPost属性名称。 You can either change one of them or remove it entirely.您可以更改其中之一或将其完全删除。

[HttpPost("Post")]
[Route("someroute")]
public async Task<IActionResult> Method1([FromBody] Request request)
{
 // Some body
 return Ok(JsonConvert.SerializeObject(citations));
}

// Rename this "Post" or remove it
[HttpPost("Post")]
[Route("otherroute")]
public async Task<IActionResult> Method2([FromBody] Request request)
{
 // Some body
 return Ok(JsonConvert.SerializeObject(citations));
}

为了我:

c.SwaggerEndpoint("../swagger/v1/swagger.json", "MyAPI V1");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swashbuckle swagger.json 大于 4 mb 净核心 - Swashbuckle swagger.json larger than 4 mb net core ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json - ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json 使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型 - Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义” - Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition" .net core/swagger - 生成 swagger.json 文件时如何绕过控制器文件? - .net core/swagger - How can I around a controller file when generating the swagger.json file? 使用在 Swashbuckle 的后期构建操作中生成的 swagger.json 文件,而不是在运行时生成的文件 - Use swagger.json file generated in post build actions in Swashbuckle instead of the file generated at runtime 如何修复 .net core 2.2 应用程序中未找到的 swagger.json - How to fix swagger.json not found in .net core 2.2 app Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter? - Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection? 如何在 ASP.NET Core Swagger (Swashbuckle.AspNetCore) 中定义控制器描述? - How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)? 使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json - Add `host`, `basePath` and `schemes` to swagger.json using Swashbuckle Aspnetcore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM