无法在我的 vb.net webapi 项目中设置 Nswag
Cannot setup Nswag in my vb.net webapi project
我必须设置一个用 VB.NET 编写的 WebApi 项目(在 .NET Framework 4.6.1 中),我想设置 Nswag 和 Swagger 以进一步实施。
我正在寻找该指南:enter link description here 并且我所有的 C# webapi 项目都运行良好
我正在尝试在 VB.NET 中设置,将代码转换成这样:
Public Module WebApiConfig
Public Sub Register(ByVal config As HttpConfiguration)
' Servizi e configurazione dell'API Web
' Route dell'API Web
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{action}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
config.MessageHandlers.Add(New TokenValidationHandler())
Dim appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(Function(t) t.MediaType = "application/xml")
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType)
RouteTable.Routes.MapOwinPath("swagger", Function(app)
app.UseSwaggerUi3(GetType(WebApiApplication).Assembly, Function(settings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"
settings.GeneratorSettings.OperationProcessors.Add(New OperationSecurityScopeProcessor("JWT Token"))
settings.GeneratorSettings.DocumentProcessors.Add(New SecurityDefinitionAppender("JWT Token",
New NSwag.OpenApiSecurityScheme With
{
.Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
.Name = "Authorization",
.Description = "Copy 'Bearer ' + valid JWT token into field",
.[In] = NSwag.OpenApiSecurityApiKeyLocation.Header
}))
End Function)
End Function)
End Sub
模块结束
但是当我运行项目弹出那个错误:
No conversion available between VB$AnonymousDelegate_0`2[System.Object,System.Object] and System.Func`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Threading.Tasks.Task]. Parameter name: signature
在我的代码的第 28 行设置错误 RouteTable.Routes.MapOwinPath("swagger", Function(app)
有什么建议吗?
我发现 VB.NET lambda 函数必须指定它们 return 的类型
像这样:
RouteTable.Routes.MapOwinPath("swagger", Function(app) As Owin.IAppBuilder
Return app.UseSwaggerUi3(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUi3Settings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"
settings.GeneratorSettings.OperationProcessors.Add(New OperationSecurityScopeProcessor("JWT Token"))
settings.GeneratorSettings.DocumentProcessors.Add(New SecurityDefinitionAppender("JWT Token",
New NSwag.OpenApiSecurityScheme With
{
.Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
.Name = "Authorization",
.Description = "Copy 'Bearer ' + valid JWT token into field",
.[In] = NSwag.OpenApiSecurityApiKeyLocation.Header
}))
Return settings
End Function)
End Function)
注意我添加了 As Owin.IAppBuilder 和 As Swagger Ui 3 Settings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
我必须设置一个用 VB.NET 编写的 WebApi 项目(在 .NET Framework 4.6.1 中),我想设置 Nswag 和 Swagger 以进一步实施。 我正在寻找该指南:enter link description here 并且我所有的 C# webapi 项目都运行良好
我正在尝试在 VB.NET 中设置,将代码转换成这样:
Public Module WebApiConfig
Public Sub Register(ByVal config As HttpConfiguration)
' Servizi e configurazione dell'API Web
' Route dell'API Web
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{action}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
config.MessageHandlers.Add(New TokenValidationHandler())
Dim appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(Function(t) t.MediaType = "application/xml")
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType)
RouteTable.Routes.MapOwinPath("swagger", Function(app)
app.UseSwaggerUi3(GetType(WebApiApplication).Assembly, Function(settings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"
settings.GeneratorSettings.OperationProcessors.Add(New OperationSecurityScopeProcessor("JWT Token"))
settings.GeneratorSettings.DocumentProcessors.Add(New SecurityDefinitionAppender("JWT Token",
New NSwag.OpenApiSecurityScheme With
{
.Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
.Name = "Authorization",
.Description = "Copy 'Bearer ' + valid JWT token into field",
.[In] = NSwag.OpenApiSecurityApiKeyLocation.Header
}))
End Function)
End Function)
End Sub
模块结束
但是当我运行项目弹出那个错误:
No conversion available between VB$AnonymousDelegate_0`2[System.Object,System.Object] and System.Func`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Threading.Tasks.Task]. Parameter name: signature
在我的代码的第 28 行设置错误 RouteTable.Routes.MapOwinPath("swagger", Function(app)
有什么建议吗?
我发现 VB.NET lambda 函数必须指定它们 return 的类型 像这样:
RouteTable.Routes.MapOwinPath("swagger", Function(app) As Owin.IAppBuilder
Return app.UseSwaggerUi3(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUi3Settings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"
settings.GeneratorSettings.OperationProcessors.Add(New OperationSecurityScopeProcessor("JWT Token"))
settings.GeneratorSettings.DocumentProcessors.Add(New SecurityDefinitionAppender("JWT Token",
New NSwag.OpenApiSecurityScheme With
{
.Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
.Name = "Authorization",
.Description = "Copy 'Bearer ' + valid JWT token into field",
.[In] = NSwag.OpenApiSecurityApiKeyLocation.Header
}))
Return settings
End Function)
End Function)
注意我添加了 As Owin.IAppBuilder 和 As Swagger Ui 3 Settings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)