swagger
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[ASP.NET Core Web]]
&color(red){※This article is based on .NET Core2.2};
#contents
* 配置 [#o6289a11]
默认的Swagger配置没有用户验证功能,需要配置一下,Swagger...
打开Program.cs,把builder.Services.AddSwaggerGen();这一句...
#codeprettify{{
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurity...
{
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Description = "Bearer Token",
Name = "Authorization",
BearerFormat = "JWT",
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequireme...
{
new OpenApiSecurityScheme()
{
Reference=new OpenApiReference()
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},new string[]{ }
}
});
});
}}
右上角会出现“Authorize”按钮
&color(red){※注意:填写的字符串的前面需要加上“Bearer ”};
* Tips [#bd7d825b]
** 上传文件 [#gc0b1a95]
&color(red){ .net5.0 、swagger 5.6.3 环境下swagger上传文...
#codeprettify{{
using Microsoft.AspNetCore.Http;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SProj.Filters
{
public class SwaggerFileUploadFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, Ope...
{
if (!context.ApiDescription.HttpMethod.Equals...
!context.ApiDescription.HttpMethod.Equals...
{
return;
}
var fileParameters = context.ApiDescription.A...
if (fileParameters.Count < 0)
{
return;
}
operation.RequestBody = new OpenApiRequestBody
{
Description = "upload file ",
Content = new Dictionary<String, OpenApiM...
{
{
"multipart/form-data", new OpenAp...
{
Schema = new OpenApiSchema
{
Type = "object",
Required = new HashSet<St...
Properties = new Dictiona...
{
{
"file", new OpenA...
{
Type = "strin...
Format = "bin...
}
}
}
}
}
}
}
};
}
}
}
}}
* Troubleshooting [#m028deb9]
** 不断提示:401 Undocumented Error [#a57276b9]
需要添加app.UseAuthentication();
并且一定要在app.UseAuthorization();前面,顺序反了都不行
#codeprettify{{
app.UseAuthentication();
app.UseAuthorization();
}}
** Failed to load API definition [#cd55dbe0]
报出下面的错误
#codeprettify{{
Failed to load API definition
}}
为了查看具体的错误信息
https://localhost:端口号/swagger/v1/swagger.json
#codeprettify{{
An unhandled exception occurred while processing the requ...
SwaggerGeneratorException: Ambiguous HTTP method for acti...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Stack Query Cookies Headers Routing
SwaggerGeneratorException: Ambiguous HTTP method for acti...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwa...
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(H...
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMi...
}}
看报错得知 action HomeController.Index 未明确标记 HttpMet...
解决方法① 看到这个报错直接在报错对应的action上增加HttpMet...
#codeprettify{{
[ApiController]
[Route("/")]
public class HomeController : Controller
{
[HttpGet]
public ResponseVm Index()
{
return ResponseVm.Success();
}
}
}}
解决方法②
#codeprettify{{
builder.Services.AddSwaggerGen(options =>
{
options.DocInclusionPredicate((name, api) => api.Http...
});
}}
#hr();
コメント:
#comment_kcaptcha
終了行:
[[ASP.NET Core Web]]
&color(red){※This article is based on .NET Core2.2};
#contents
* 配置 [#o6289a11]
默认的Swagger配置没有用户验证功能,需要配置一下,Swagger...
打开Program.cs,把builder.Services.AddSwaggerGen();这一句...
#codeprettify{{
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurity...
{
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Description = "Bearer Token",
Name = "Authorization",
BearerFormat = "JWT",
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequireme...
{
new OpenApiSecurityScheme()
{
Reference=new OpenApiReference()
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},new string[]{ }
}
});
});
}}
右上角会出现“Authorize”按钮
&color(red){※注意:填写的字符串的前面需要加上“Bearer ”};
* Tips [#bd7d825b]
** 上传文件 [#gc0b1a95]
&color(red){ .net5.0 、swagger 5.6.3 环境下swagger上传文...
#codeprettify{{
using Microsoft.AspNetCore.Http;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SProj.Filters
{
public class SwaggerFileUploadFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, Ope...
{
if (!context.ApiDescription.HttpMethod.Equals...
!context.ApiDescription.HttpMethod.Equals...
{
return;
}
var fileParameters = context.ApiDescription.A...
if (fileParameters.Count < 0)
{
return;
}
operation.RequestBody = new OpenApiRequestBody
{
Description = "upload file ",
Content = new Dictionary<String, OpenApiM...
{
{
"multipart/form-data", new OpenAp...
{
Schema = new OpenApiSchema
{
Type = "object",
Required = new HashSet<St...
Properties = new Dictiona...
{
{
"file", new OpenA...
{
Type = "strin...
Format = "bin...
}
}
}
}
}
}
}
};
}
}
}
}}
* Troubleshooting [#m028deb9]
** 不断提示:401 Undocumented Error [#a57276b9]
需要添加app.UseAuthentication();
并且一定要在app.UseAuthorization();前面,顺序反了都不行
#codeprettify{{
app.UseAuthentication();
app.UseAuthorization();
}}
** Failed to load API definition [#cd55dbe0]
报出下面的错误
#codeprettify{{
Failed to load API definition
}}
为了查看具体的错误信息
https://localhost:端口号/swagger/v1/swagger.json
#codeprettify{{
An unhandled exception occurred while processing the requ...
SwaggerGeneratorException: Ambiguous HTTP method for acti...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Stack Query Cookies Headers Routing
SwaggerGeneratorException: Ambiguous HTTP method for acti...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.Genera...
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwa...
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(H...
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMi...
}}
看报错得知 action HomeController.Index 未明确标记 HttpMet...
解决方法① 看到这个报错直接在报错对应的action上增加HttpMet...
#codeprettify{{
[ApiController]
[Route("/")]
public class HomeController : Controller
{
[HttpGet]
public ResponseVm Index()
{
return ResponseVm.Success();
}
}
}}
解决方法②
#codeprettify{{
builder.Services.AddSwaggerGen(options =>
{
options.DocInclusionPredicate((name, api) => api.Http...
});
}}
#hr();
コメント:
#comment_kcaptcha
ページ名: