zoukankan      html  css  js  c++  java
  • .net core web api 使用swagger

    .net 的就不用说了,看这位老哥的  http://www.cnblogs.com/alunchen/p/6888002.html

    现在来讲.net core

    Nuget Packages安装,使用程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre

     Startup 配置

       public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc()
                    .AddJsonOptions(options=>options.SerializerSettings.ContractResolver
                    =new Newtonsoft.Json.Serialization.DefaultContractResolver());//json首字母小写
    
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                    {
                        Version = "v1",
                        Title = "TestSwagger API"
                    });
    
                    //Determine base path for the application.  
                    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                    //Set the comments path for the swagger json and ui.  
                    var xmlPath = Path.Combine(basePath, "TestSwagger.xml");
                    options.IncludeXmlComments(xmlPath);
                });
            }
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseMvc();
    
    
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestSwagger API V1");
                });
            }

    另外上图中,禁止显示警告中,添加1591 代码,可以过滤掉一些类名没有写注释的报警信息。

     然后,就可以运行啦

    每次启动进入api/values很烦,把webapi项目的启动路径设置成 swagger。这样每次调试运行项目都会自动跳转到swagger帮助页面

    其余的请看这个大神的 https://www.cnblogs.com/suxinlcq/p/6757556.html

  • 相关阅读:
    uva111 History Grading
    UVA 10100Longest Match
    UVA 147Dollars
    归并排序模板
    找礼物(find)
    水流(water)dfs
    细菌(disease) 位运算
    单词接龙
    关于jquery的each遍历,return只终止当前循环,不好使的解决办法
    jquery中ajax回调函数使用this
  • 原文地址:https://www.cnblogs.com/Cein/p/9104317.html
Copyright © 2011-2022 走看看