欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NetCore?配置Swagger的詳細(xì)代碼

 更新時(shí)間:2022年09月22日 10:10:51   作者:Robot-Blog  
這篇文章主要介紹了NetCore?配置Swagger的方法,通過添加Nuget和添加靜態(tài)類擴(kuò)展方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.添加Nuget

install-package Swashbuckle.AspNetCore -project XXX -version 6.4.0

2.添加靜態(tài)類擴(kuò)展方法

2.1.生成項(xiàng)目xml:選中項(xiàng)目 / 右鍵 / 屬性 / 生成 / 輸出 / 選中xml文檔文件

2.2.system_v1:必須唯一不重復(fù),且【options.SwaggerDoc("system_v1"】必須與【options.SwaggerEndpoint("/swagger/system_v1/】一致,不然會異常【Failed to load API definition; Fetch error: response status is 404 /swagger/system_v1/swagger.json】

/// <summary>
    /// Swagger 靜態(tài)類
    /// </summary>
    public static class SwaggerExtend
    {
        /// <summary>
        /// 添加服務(wù): swagger
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static void AddCustSwagger(this IServiceCollection services)
        {
            var version = "V1.0";
            var apiName = "XXX系統(tǒng)";
            services.AddSwaggerGen(options =>
            {
                options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

                options.SwaggerDoc("system_v1", new OpenApiInfo
                {
                    Version = version,
                    Title = $"{apiName} API",
                    Description = $"{apiName} {version} 接口服務(wù)"
                });

                //  獲取應(yīng)用程序所在目錄
                var basePath = Path.GetDirectoryName(typeof(SwaggerExtend).Assembly.Location);
                var xmlPath = Path.Combine(basePath, "ProjectName.xml");
                //  swagger界面默認(rèn)只顯示 方法&字段 注釋,不顯示 控制器注釋
                //  第二個(gè)參數(shù)為true, 則是controller的注釋
                //options.IncludeXmlComments(xmlPath);
                options.IncludeXmlComments(xmlPath, true);
            });
        }

        /// <summary>
        /// 添加中間件: swagger
        /// </summary>
        /// <param name="app"></param>
        public static void UseCustSwagger(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/system_v1/swagger.json", "系統(tǒng)API");
                //  默認(rèn)路徑為:/swagger/index.html
                //  路由前綴 - 設(shè)置為空,可直接跳轉(zhuǎn)到swagger頁面:/index.html
                //  api測試可設(shè)置為空,部署時(shí)容易與前端路由沖突
                options.RoutePrefix = string.Empty;
            });
        }
    }

3.StartUp注冊服務(wù),添加中間件

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCustSwagger();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCustSwagger();
        }

到此這篇關(guān)于NetCore 配置Swagger的的文章就介紹到這了,更多相關(guān)NetCore 配置Swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論