Asp.net core & Vue Swagger

因为前后端分离的开发模式,前后端之间少一些对接烦恼,出现了Swagger文档。

1. 引入Swagger文档包

Swashbuckle.AspNetCore

 

2.添加Swagger中间件

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v0.0.1",    //版本
                    Title = "Core API",  //标题
                    Description = "说明文档",  //描述文字
                    TermsOfService = "None",  //服务项
                    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "Core", Email = "[email protected]", Url = "https://www.xxxx.com" }    //联系人信息
                });
            });


        }

 

3. 使用Swagger中间件


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    //与ConfigureServices中要一致
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");
                });
            }

            

            app.UseMvc();
}

最好只是开发期间使用啦

 

4. 为接口添加注释

 

正常注释用///即可呼出,弄到文档中,需要配置一下:

在项目属性的Build中:

Asp.net core & Vue Swagger_第1张图片

配置一下Startup,把生成的xml文档让swagger加载一下:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v0.0.1",
                    Title = "Core API",
                    Description = "说明文档",
                    TermsOfService = "None",
                    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "Core", Email = "[email protected]", Url = "https://www.xxxxx.com" }
                });

               var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
               var xmlPath = Path.Combine(basePath, "Blog.Core.xml");//这个就是刚刚配置的xml文件名
               c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改
            });

        }

Asp.net core & Vue Swagger_第2张图片

Model的说明也是一样的方法,不再赘述。

 

 

你可能感兴趣的:(Asp.net,Core,&,Vue)