cia*_*ius 1 swagger swagger-ui asp.net-web-api2 swashbuckle
我正在尝试使用几个简单的控制器创建一个WebApi项目.如果我使用fiddler调用方法一切都很好,但我更喜欢使用swashbuckle,因为它有点漂亮.
但是,使用默认配置安装swashbuckle时它无法正常工作.
当我导航到 http:// localhost/api/mycontroller/swagger时
它重定向到 http:// localhost/api/mycontroller/swagger/ui/index
但是它只显示以下错误:
<错误>
<消息>
未找到与请求URI" http:// localhost/api/Management/swagger/ui/index " 匹配的HTTP资源.
</ Message>
<MessageDetail>
未找到与名为'swagger'的控制器匹配的类型.</ MessageDetail>
</ Error>
我的路由如下:config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
cia*_*ius 11
我们找到了一个非常简单的解决方案.
当swagger被添加到项目中时,它会自动更新App_Start中的Bootstrapper.cs,它看起来像这样:
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
SwaggerConfig.Register(config);
Run Code Online (Sandbox Code Playgroud)
只需将swagger移动到列表顶部,就可以在web api路由之前正确注册它的路由:
SwaggerConfig.Register(config); // Swagger must be the first thing in this sequence or it just does not work...
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
Run Code Online (Sandbox Code Playgroud)
只是张贴这个,希望它能帮助别人.
| 归档时间: |
|
| 查看次数: |
2098 次 |
| 最近记录: |