.net core swagger安装

 第一步:


 工具》NuGet包管理器》程序包管理控制台

因为用“程序包管理控制台”可以获取到最新的swagger

 第二步:输入命令

Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4

.net core swagger安装_第1张图片

安装完毕后 ,提示如下

.net core swagger安装_第2张图片

 第三步:配置

打开项目根目录Startup.cs,找到ConfigureServices()方法

输入如下代码

//Swagger
services.AddSwaggerGen(m=>{
   m.SwaggerDoc("v1", new OpenApiInfo { Title="swaggerTest",Version="v1" });
});

找到Configure()方法

输入如下代码

//Swagger
app.UseSwagger();
app.UseSwaggerUI(s=> {
 //下面路径里的v1必须和SwaggerDoc()第一个参数一致
 s.SwaggerEndpoint("/swagger/v1/swagger.json", "swaggerTast");
});

注:

1.控制器要取消[ApiController]特性

2.Action方法上面要加 请求类型特性

3.增加API控制器的时候,可以 右键》添加》控制器》API控制器

 第四步:运行

浏览器输入

http://localhost:49979/swagger/index.html

第五步:修改默认页

Properties>launchSettings.json>
 找到profiles节点,修改launchUrl属性的值,默认是weatherforecast
 我们修改为:swagger/index.html

 

完整代码

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using swaggertest.Utility;

namespace swaggertest
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            SqlHelper.ConStr = Configuration["ConStr"].Trim();

            //跨域服务注册
            services.AddCors(m=>m.AddPolicy("any",a=>a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));

            //Swagger
            services.AddSwaggerGen(m=>{
                m.SwaggerDoc("v1", new OpenApiInfo { Title="swaggerTest",Version="v1" });
            });
            services.AddControllers();
        }

        // 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.UseRouting();

            app.UseAuthorization();

            //Swagger
            app.UseSwagger();
            app.UseSwaggerUI(s=> {
                //下面路径里的v1必须和SwaggerDoc()第一个参数一致
                s.SwaggerEndpoint("/swagger/v1/swagger.json", "swaggerTast");
            });

            //跨域
            app.UseCors();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Properties》launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49979",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "swaggertest": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

控制器

Controllers》ProductsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using swaggertest.Models;

namespace swaggertest.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]/[Action]")]
    //[ApiController]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetProduct()
        {
          List products= Products.ListAll();
            return new JsonResult(products);
        }
    }
}

注:

1.控制器要取消[ApiController]特性

2.Action方法上面要加 请求类型特性

3.增加API控制器的时候,可以 右键》添加》控制器》API控制器

 


源码下载

.net core的swagger使用,ADO使用,跨域

https://download.csdn.net/download/cplvfx/12611473

 

你可能感兴趣的:(.net,core)