当前位置:  开发笔记 > 编程语言 > 正文

Swashbuckle招摇是行不通的

如何解决《Swashbuckle招摇是行不通的》经验,为你挑选了1个好方法。

我有一个web api 2项目.它被配置为使用owin自托管.它没有任何global.asax文件.我需要有关于web api的帮助页面,并且已经使用了swaschbuckle.但是rooturl/swagger/docs没有提供任何输出.我按照这里的说明' https://github.com/domaindrivendev/Swashbuckle/issues/196 ',但它仍然无法正常工作.以下是我的配置代码

public void Configuration(IAppBuilder app)
{
    // Configure DI
    container = BuildDI();

    // Create the configuration. OWIN Should create a new httpconfiguration.
    // GlobalConfiguration can't be used.
    HttpConfiguration = new HttpConfiguration();

    HttpConfiguration.Formatters.XmlFormatter.UseXmlSerializer = true;
    HttpConfiguration.MapHttpAttributeRoutes();
    HttpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    // Set ServicePointManager properties.
    ServicePointManager.ServerCertificateValidationCallback = ((sender, cert, chain, sslPolicyErrors) => true);

    // 10 concurrent connections can be made on the service point.
    ServicePointManager.DefaultConnectionLimit = 10;

    // After the idle time expires, the ServicePoint object is eligible for 
    // garbage collection and cannot be used by the ServicePointManager object.
    ServicePointManager.MaxServicePointIdleTime = 30000; // 30 Seconds.

    app.UseSerilogRequestContext("RequestId");

    // Middleware is injected form DI.
    app.UseAutofacMiddleware(container);

    app.UseAutofacWebApi(HttpConfiguration);

    //Enable swashbuckle
    SwaggerConfig.Register(HttpConfiguration);

    // Webapi middleware. Do it at the end.
    app.UseWebApi(HttpConfiguration);

    // Register callback to dispose container.
    RegisterShutdownCallback(app, container);
}

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.EnableSwagger(c =>
        {
            c.RootUrl(rurl => ConfigurationManager.AppSettings["hostUrl"].ToString());
            c.IncludeXmlComments(GetXmlCommentsFileLocation());
            c.SingleApiVersion("v1", "Isone");
        })
        .EnableSwaggerUi(c =>
        {
        });
    }

    private static string GetXmlCommentsFileLocation()
    {
        var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\bin";
        var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML";
        var commentsFileLocation = Path.Combine(baseDirectory, commentsFileName);

        return commentsFileLocation;
    }
}

请在代码中指出错误.



1> Alistair Sut..:

步骤应如下:

1)添加“Swashbuckle.Core”作为的NuGet依赖性(仅需要在芯包带OWIN)

package.json

     
    
    ...

2)在您的启动类内部注册大摇大摆

启动文件

public partial class Startup
{
    /// 
    ///     Used to create an instance of the Web application
    /// 
    /// Parameter supplied by OWIN implementation which our configuration is connected to
    public void Configuration(IAppBuilder app)
    {
        // Wire-in the authentication middleware
        ConfigureAuth(app);

        // In OWIN you create your own HttpConfiguration rather than re-using the GlobalConfiguration.
        HttpConfiguration config = new HttpConfiguration();

        // Handle registration of Swagger API docs
        SwaggerConfig.Register(config);

        // Handles registration of the Web API's routes
        WebApiConfig.Register(config);

        // Register web api
        app.UseWebApi(config);
    }

SwaggerConfig.cs

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    {
        config
            .EnableSwagger(c =>
            {
                c.IncludeXmlComments(GetXmlCommentsPath());
            })
            .EnableSwaggerUi(c =>
            {
                c.EnableApiKeySupport("Authorization", "header");
            });
    }

    private static string GetXmlCommentsPath()
    {
        return $@"{AppDomain.CurrentDomain.BaseDirectory}\bin\Example.XML";
    }
}

3)在构建属性中启用XML文档输出

4) Swagger UI将通过swagger / ui / index提供


对于使用Visual Studio Code(或其他编辑器)的程序员来说,要补充此答案,以在项目构建中启用XML文档生成,只需将csproj的属性组添加到其中的以下行即可:```<GenerateDocumentationFile> true < / GenerateDocumentationFile>```
推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有