使用.net Core 2 API实现Swashbuckle/Swagger我现在在访问swagger.json时收到500错误:
NotSupportedException:用于操作的不明确的HTTP方法 - EBisAPI.Controllers._class.HandleError(EBisAPI).动作需要为Swagger提供明确的HttpMethod绑定
我已经浏览了所有控制器,并在每个控制器的所有公共方法上看到显式路由.有没有办法确定哪种方法抛出模糊的路由错误?
I have the asp.net core MVC project and separate WebApi project in one solution. I'm adding the swagger following the documentation on github. Here is my Startup.cs of mvc project:
public void ConfigureServices(IServiceCollection services)
{
//...
// Adding controllers from WebApi:
var controllerAssembly = Assembly.Load(new AssemblyName("WebApi"));
services.AddMvc(o =>
{
o.Filters.Add<GlobalExceptionFilter>();
o.Filters.Add<GlobalLoggingFilter>();
})
.AddApplicationPart(controllerAssembly)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSwaggerGen(c =>
{
//The generated Swagger JSON file will have these properties.
c.SwaggerDoc("v1", new Info
{
Title = "Swagger XML …Run Code Online (Sandbox Code Playgroud) Swagger UI端点与登台时的dev不同(不包括域名)
IIS配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
app.UseSwagger(c=>
{
//Change the path of the end point , should also update UI middle ware for this change
c.RouteTemplate = "api-docs/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
//Include virtual directory if site is configured so
c.SwaggerEndpoint(Configuration["Appsettings:VirtualDirectory"]+"api-docs/v1/swagger.json", "Api v1");
});
services.AddSwaggerGen(c =>
{
var xmlDocPath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Api.xml");
c.IncludeXmlComments(xmlDocPath);
c.DescribeAllEnumsAsStrings();
Run Code Online (Sandbox Code Playgroud)
具有上述配置
发展
"AppSettings": {
"VirtualDirectory": "/"
Run Code Online (Sandbox Code Playgroud)
}
分期
"AppSettings": {
"VirtualDirectory": "/Api/"
Run Code Online (Sandbox Code Playgroud)
}
启动计算机上UI的结束点,暂存时间为ON
http://localhost:5001/api-docs/v1/swagger.json
Run Code Online (Sandbox Code Playgroud)
但在登台服务器上是相同的
http://xxxx:5002/swagger/Api/api-docs/v1/swagger.json
Run Code Online (Sandbox Code Playgroud)
而不是(应该是什么)
http://xxxx:5002/Api/api-docs/v1/swagger.json
Run Code Online (Sandbox Code Playgroud) 我有一个在 .NetCore 3.1 中创建的 API,并使用 Swashbuckle 启用了 Swagger(OAS3)。默认情况下,当我的应用程序启动时,如果使用此 URL 显示 Swagger 页面:
http://{port}/swagger.index.html
Run Code Online (Sandbox Code Playgroud)
我想自定义 Swagger URL,以便它包含正在运行的应用程序的名称。我这样做的原因是我在 AWS Fargate 中运行 Nginx 时使用基于路径的路由。
我将在 Fargate 任务中运行多个 API 容器,Nginx 将接收来自应用程序负载均衡器和路径(例如 /api/app1)的 REST 请求,它将请求路由到目标应用程序的正确容器端口.
例如,我有三个应用程序:端口 5000 上的 App1、端口 5001 上的 App2 和端口 5003 上的 App3。
如果用户向https://api/app1发出请求,Nginx 将检测路径并将请求转发到端口 5000,这是 App1 的容器端口。
但是,为了确保出现正确的 Swagger 页面,我需要在 Swagger 的 URL 中添加“api/App1”,以便 Nginx 将请求转发到正确的容器。在本例中,它是 App1。
换句话说,我希望我的 Swagger URL 看起来像这样:
https://api/app1/swagger/index.html
Run Code Online (Sandbox Code Playgroud)
我试过的
在我的 Startup.cs 文件中,我添加了以下内容:
// Define prefix for application
private readonly string baseApplicationRoute = "api/app1";
// Enable …Run Code Online (Sandbox Code Playgroud)