zoukankan      html  css  js  c++  java
  • Swagger在.Net Core中的应用

    1、新建.net core项目。

    2、NuGet安装Swagger插件“Swashbuckle.AspNetCore”。

    3、Startup.cs中添加代码

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
                services.AddSwaggerGen(m => {
                    m.SwaggerDoc("v1", new OpenApiInfo { Title = "CoreWebApi", Version = "v1" });
                });
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseSwagger();
    
                // 配置SwaggerUI
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "CoreWebApi");
                    c.RoutePrefix = string.Empty;
    
                });
    
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
    

    4、修改launchSetings.json

    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:50770",
          "sslPort": 44326
        }
      },
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "http://localhost:50770",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "use64Bit": true
        },
        "WebApplication11": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "index.html",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "https://localhost:5001;http://localhost:5000"
        }
      }
    }
    

    5、启动项目时不选择IIS,选择项目名称那个,启动后可看到Swaggerl界面及各地址,并可测试。

  • 相关阅读:
    实验四Web服务器2
    发际线与我作队团队作业(五):冲刺总结1
    读书笔记
    socket3
    使用CAS来实现个单例模式
    基于无锁的C#并发队列实现
    C#3.0中的“多重继承”
    对并发/并行编程的总结
    谈谈多线程编程(二) 不变对象
    谈谈多线程编程(一) 安全性策略
  • 原文地址:https://www.cnblogs.com/youyuan1980/p/13644436.html
Copyright © 2011-2022 走看看