Ale*_*nzi 5 versioning asp.net-web-api swagger swagger-ui
我正在尝试使用 SwaggerUI,但遇到了一些问题。
当我打电话给http://mysite.com/api/swagger 时,我得到了这个:
{
"apiVersion":"4.0.0.0",
"swaggerVersion":"2.0",
"basePath":"http://mysite.com",
"resourcePath":null,
"apis":
[
{
"path":"/api/docs/V1.Foo",
"description":"Foo V1.",
"operations":[]
},
{
"path":"/api/docs/V2.Foo",
"description":"Foo V2.",
"operations":[]
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,当我调用http://mysite.com/api/docs/V1.Foo或http://mysite.com/api/docs/V2.Foo 时,我得到了这个:
<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>
Run Code Online (Sandbox Code Playgroud)
看起来我正在调用我的 API,但我正在尝试获取 API 文档。
我所有的控制器都实现了 System.Web.Http.ApiController。
这是我的 WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
config.Routes.MapHttpRoute(
name: "SwaggerApi",
routeTemplate: "api/docs/{controller}",
defaults: new { swagger = true }
);
config.Routes.MapHttpRoute(
name: "Swagger",
routeTemplate: "api/swagger",
defaults: new { controller = "swagger" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, version = "v1" }
);
config.Filters.Add(new SwaggerActionFilter());
try
{
config.Services.Replace(typeof(IDocumentationProvider),
new XmlCommentDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/XmlDocument.XML")));
}
catch (FileNotFoundException)
{ }
//My version selector
config.Services.Replace(typeof(IHttpControllerSelector), new VersionControllerSelector(config));
config.Filters.Add(new VersionNoHeaderAttribute());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 IHttpControllerSelector 实现(VersionControllerSelector):
...
public HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var routeData = request.GetRouteData();
if (routeData == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
string version;
if (GetRouteVariable<bool>(routeData, "swagger"))
{
version = ""; // Here I have the version and controller name.
}
else if (request.RequestUri.ToString().ToLower().EndsWith("swagger"))
{
version = "net."; // Net.Swagger
}
else
{
version = GetRouteVariable<string>(routeData, VersaoKey);
if (version == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
version = string.Format(CultureInfo.InvariantCulture, "{0}.", version); // V1.MyControler
}
var controllerName = GetRouteVariable<string>(routeData, ControllerKey);
if (controllerName == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
var key = version + controllerName;
HttpControllerDescriptor controllerDescriptor;
if (_controllers.Value.TryGetValue(key, out controllerDescriptor))
{
return controllerDescriptor;
}
throw new HttpResponseException(HttpStatusCode.NotFound);
}
...
Run Code Online (Sandbox Code Playgroud)
当我调用http://mysite.com/api/docs/“some-controller ”时 SwaggerActionFilter 没有收到我的请求。
如果我在另一个没有版本控制的解决方案中使用 SwaggerUI,我可以获得所有文档。
我知道我的版本选择器可能是错误的,但我不知道出了什么问题。
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |