.net core swagger安装

网友投稿 759 2022-11-26

- core swagger安装

- core swagger安装

第一步:

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

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

第二步:输入命令

Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4

安装完毕后 ,提示如下

第三步:配置

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

输入如下代码

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

找到Configure()方法

输入如下代码

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

注:1.控制器要取消[ApiController]特性 2.Action方法上面要加 请求类型特性3.增加API控制器的时候,可以 右键》添加》控制器》API控制器

第四步:运行

浏览器输入

​​ 找到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": " "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": " "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": " "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控制器

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:开发常用工具软件GitLab使用教程(详细):GitLab使用教程(详细)_奔跑的哈士奇的博客-CSDN博客
下一篇:c# .net无限递归获取分类,传统for无限递归和 linq无限递归获取分类
相关文章

 发表评论

暂时没有评论,来抢沙发吧~