• 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界面及各地址,并可测试。

  • 相关阅读:
    url 百分号解密
    16.UA池和代理池
    15.scrapy框架之日志等级、请求传参、提高scrapy框架的爬取效率
    14. scrip框架之5大核心组件和post请求
    13.scrapy 框架之递归解析(手动发送请求),
    12. scrapy 框架持续化存储
    11.scrapy框架简介和基础应用
    10. 移动端数据爬取
    09.python之网络爬虫之selenium、phantomJs和谷歌无头浏览器的自动化操作
    08 python之网络爬虫之乱码问题
  • 原文地址:https://www.cnblogs.com/youyuan1980/p/13644436.html
Copyright © 2020-2023  润新知