我有一个 .NET Web API 项目。我可以通过安装 Swashbuckle NuGet 包来添加 Swagger。Swagger 现在制作了一个漂亮的网页,通过访问以下网址提供文档和测试能力:
http://localhost:20475/product-catalog-api/swagger/ui/index
这部分效果很好。然而,当我添加 FluentValidation 并在 WebApiConfig.cs 中设置它时,对我的网站的所有请求都被劫持,我只得到一个带有空响应的 JSON,而不是看到 Swagger html 页面。
我使用 Matthew Jones 的这篇文章来设置 FluentValidation: http://www.exceptionnotfound.net/custom-validation-in-asp-net-web-api-with-fluentvalidation/
我的 Web api 配置如下:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Filters.Add(new ValidateModelStateFilter());
config.MessageHandlers.Add(new ResponseWrappingHandler());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: Version + "/{action}",
defaults: new {controller = "Main"}
);
FluentValidationModelValidatorProvider.Configure(config);
}
Run Code Online (Sandbox Code Playgroud)
我可以在每个 Web 服务方法中手动触发 FluentValidation,但我希望有一个通用的解决方案,就像 Matthew Jones 对过滤器和消息处理程序所做的那样。基本上我想做的是如果请求是针对 swagger-ui 那么不要通过 FluentValidation 进行路由。
在我们运行RESTful Web服务的QA和Prod环境中,端口80未打开.所以目前当我尝试在QA中使用Swagger UI时,我收到此消息并且它只是挂起:
fetching resource list: http://qa-server:80/product-catalog-api/swagger/docs/v1; Please wait.
我正在使用Swashbuckle来配置Swagger.我也在配置中更改了这一行,但它仍然无效.
// If schemes are not explicitly provided in a Swagger 2.0 document, then the scheme used to access
// the docs is taken as the default. If your API supports multiple schemes and you want to be explicit
// about them, you can use the "Schemes" option as shown below.
//
c.Schemes(new[] { "https" });
Run Code Online (Sandbox Code Playgroud)
SSL端口443是打开的,所以我想在Swagger UI上运行它.我可以手动更改http://qa-server:80/product-catalog-api/swagger/docs/v1
到https://qa-server/product-catalog-api/swagger/docs/v1
再扬鞭会列出我的网络的方法,但是当我点击它挂起Try it out!
这是从控制台输出:SCRIPT5: Access is denied. File: …