使用招摇着一个命名空间,versionned的WebAPI [英] Using Swagger with a namespaced-versionned WebApi

查看:163
本文介绍了使用招摇着一个命名空间,versionned的WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了我的WebAPI如何版本基于使用的命名空间的this类

I have found out how to version my WebAPI based on namespaces using this class.

我使用Swashbuckle到扬鞭文档添加到我的API,使用Swashbuckle的NuGet包。

I am using Swashbuckle to add Swagger doc to my API, using the Swashbuckle Nuget package.

如果我把一切都完好无损,当我浏览到/招摇/,我得到一个空白页。

If I keep everything intact, when I navigate to /swagger/, I get an empty page.

在我App_Start:

In my App_Start:

public class SwaggerConfig
{
    public static void Register()
    {
        Bootstrapper.Init(GlobalConfiguration.Configuration);
        SwaggerSpecConfig.Customize(c =>
            {
                c.IncludeXmlComments(GetXmlCommentsPath());
            });
    }

    private static string GetXmlCommentsPath()
    {
        return string.Format(@"{0}\App_Data\XmlDocumentation.xml", AppDomain.CurrentDomain.BaseDirectory);
    }
}

和我的Web API路线:

And my web API routes:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{namespace}/{controller}/{id}",
            defaults: new
                {
                    id = RouteParameter.Optional
                });
    }
}

如果我删除 {命名空间} 它的工作原理(显示在API命令),但我想保持这个命名空间的信息在我的路线。

If I remove the {namespace} it works (the API commands are displayed), but I want to keep this namespace information in my route.

我如何自定义扬鞭/ Swashbuckle,使这项工作?

How do I customize Swagger/Swashbuckle to make this work ?

谢谢!

推荐答案

从Swashbuckle Github上回购:

From the Swashbuckle Github repo:

有在上述实施命名空间路由的一个缺陷,因为它打破了的WebAPI元数据层 - ApiExplorer因此Swashbuckle

There is a flaw in the above implementation of "namespace routing" as it breaks the WebApi metadata layer - ApiExplorer and hence Swashbuckle.

有一个解决方法,虽然不能直接解决您的问题,就是用属性代替版本,这与Swashbuckle正常工作:

A workaround, although doesn't directly solve your problem, is to use attribute versioning instead, which works fine with Swashbuckle:

即:

[RoutePrefix("api/v1/Features")]
public class FeaturesV1Controller : ApiController
{
    [Route("Products/{product}")]
     public IList<Metadata.FeatureListItemModel> Get(long product){}

请参阅以下两个问题Github上获取更多信息。
https://github.com/domaindrivendev/Swashbuckle/issues/317
https://github.com/domaindrivendev/Swashbuckle/issues/303

Please see the two Github issues below for more info. https://github.com/domaindrivendev/Swashbuckle/issues/317 https://github.com/domaindrivendev/Swashbuckle/issues/303

我相信,随着属性的路由,你的控制器必须为每个版本不同的名称。即类应该被命名为FeaturesV1Controller和FeaturesV2Controller为V2,但对于路由你仍然可以使用/ API / V1 /特点和/ API / V2 /功能

I believe that with attribute routing, your controller must have a different name for each version. I.e. the class should be named FeaturesV1Controller and FeaturesV2Controller for v2, but for the routes you can still use /api/v1/Features and /api/v2/Features

这篇关于使用招摇着一个命名空间,versionned的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆