Was trying to set up Swagger for my .net core 7.0 project but it gives "Fetch error NetworkError when attempting to fetch resource" on opening

huangapple go评论91阅读模式
英文:

Was trying to set up Swagger for my .net core 7.0 project but it gives "Fetch error NetworkError when attempting to fetch resource" on opening

问题

大部分的控制器都有自定义的路由,当我通过Postman获取它们时,API可以正常工作。

此外,我在我的 Startup.cs 中添加了以下内容,用于Swagger:

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Version = "v1",
        Title = "ToDo API",
        Description = "An ASP.NET Core Web API for managing ToDo items",
        TermsOfService = new Uri("https://example.com/terms"),
        Contact = new OpenApiContact
        {
            Name = "Example Contact",
            Url = new Uri("https://example.com/contact")
        },
        License = new OpenApiLicense
        {
            Name = "Example License",
            Url = new Uri("https://example.com/license")
        }
    });
});

还有以下部分:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
        options.RoutePrefix = string.Empty;
    });
}

网页加载正常,但API不工作。

在检查URL之后:

Was trying to set up Swagger for my .net core 7.0 project but it gives "Fetch error NetworkError when attempting to fetch resource" on opening

日志:

2023-05-17 12:44:14.5197 Error Error parsing layout aspnet-request-statuscode will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-request-statuscode'. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name, Nullable`1 throwConfigExceptions)

在我的 nlog.config 中已经添加了以下内容:

<extensions>
    <add assembly="NLog.Web.AspNetCore"/>
</extensions>

希望这对你有所帮助。

英文:

Most of the controllers have custom routing and APIs work when I fetch them through postman

Also have added the following for Swagger in my Startup.cs

services.AddSwaggerGen(options =&gt;
        {
            options.SwaggerDoc(&quot;v1&quot;, new OpenApiInfo
            {
                Version = &quot;v1&quot;,
                Title = &quot;ToDo API&quot;,
                Description = &quot;An ASP.NET Core Web API for managing ToDo items&quot;,
                TermsOfService = new Uri(&quot;https://example.com/terms&quot;),
                Contact = new OpenApiContact
                {
                    Name = &quot;Example Contact&quot;,
                    Url = new Uri(&quot;https://example.com/contact&quot;)
                },
                License = new OpenApiLicense
                {
                    Name = &quot;Example License&quot;,
                    Url = new Uri(&quot;https://example.com/license&quot;)
                }
            });
        });

Also

if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(options =&gt;
            {
                options.SwaggerEndpoint(&quot;/swagger/v1/swagger.json&quot;, &quot;v1&quot;);
                options.RoutePrefix = string.Empty;
            });

        }

The web page loads but the APIs do not
Was trying to set up Swagger for my .net core 7.0 project but it gives "Fetch error NetworkError when attempting to fetch resource" on opening

After Inspecting the URL
Was trying to set up Swagger for my .net core 7.0 project but it gives "Fetch error NetworkError when attempting to fetch resource" on opening

The Logs

2023-05-17 12:44:14.5197 Error Error parsing layout aspnet-request-statuscode will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: &#39;aspnet-request-statuscode&#39;. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name, Nullable`1 throwConfigExceptions)

Already have the following in my nlog.config

  &lt;extensions&gt;
&lt;add assembly=&quot;NLog.Web.AspNetCore&quot;/&gt;&lt;/extensions&gt;

答案1

得分: 0

已修复,显然是因为我在一些控制器中有一些非操作方法,导致了这个问题,通过在非操作方法上方添加 [ApiExplorerSettings(IgnoreApi = true)] 来修复了这个问题。

英文:

Fixed it, apparently it was because I had a few non-action methods inside the some of the controllers which caused this, fixed this by adding [ApiExplorerSettings(IgnoreApi = true)] on top of the non action methods.

huangapple
  • 本文由 发表于 2023年5月17日 23:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定