英文:
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之后:
日志:
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 =>
{
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")
}
});
});
Also
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}
The web page loads but the APIs do not
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: '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)
Already have the following in my nlog.config
<extensions>
<add assembly="NLog.Web.AspNetCore"/></extensions>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论