《Asp.Net Core3 + Vue3入坑教程》 此教程仅适合新手入门或者先后端分离尝试者。能够根据图文一步一步进操做编码也能够选择直接查看源码。每一篇文章都有对应的源码html
教程后期会将 .Net Core 3升级成 .Net Core 5前端
Asp.Net Core后端项目vue
Vue3 前端项目git
暂未发表敬请期待...github
本文为《Asp.Net Core3 + Vue3入坑教程》系列教程的后端开篇,主要介绍 Asp.Net Core Web后端项目的搭建流程与Swagger配置。sql
代码以下:json
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Simple_Asp.Net_Core.ServiceProvider; namespace Simple_Asp.Net_Core { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSwagger(); } // 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.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1"); }); } app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } }
目的是让项目的注释可以展现在swagger页面上 。XML 文档文件的路径须要与下一步Swagger扩展类的文件路径一致后端
var xmlPath = Path.Combine(basePath, "Simple_Asp.Net_Core.xml");
当前Swagger扩展类,包含了不少内容,后续会陆续使用上api
代码以下:跨域
using System; using System.IO; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; namespace Simple_Asp.Net_Core.ServiceProvider { public static class Swagger { public static void AddSwagger(this IServiceCollection services) { services.AddSwaggerGen(option => { option.SwaggerDoc("v1", new OpenApiInfo { Version = "0.0.1", Title = "Simple API", Description = "框架说明文档", TermsOfService = null, Contact = new OpenApiContact { Name = "Simple", Email = string.Empty, Url = null } }); // 读取xml信息 var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "Simple_Asp.Net_Core.xml"); option.IncludeXmlComments(xmlPath, true); // Add security definitions option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, }); option.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference() { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, Array.Empty<string>() } }); }); } } }
目的是让项目启动页为Swagger页面
代码以下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace Simple_Asp.Net_Core.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET: api/<ValuesController1> [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/<ValuesController1>/5 [HttpGet("{id}")] public string Get(int id) { return "value"; } // POST api/<ValuesController1> [HttpPost] public void Post([FromBody] string value) { } // PUT api/<ValuesController1>/5 [HttpPut("{id}")] public void Put(int id, [FromBody] string value) { } // DELETE api/<ValuesController1>/5 [HttpDelete("{id}")] public void Delete(int id) { } } }
因为引入了Swagger致使VS多了CS1591警告,也能够不取消此警告
Swagger做为先后端分离开发必备工具,不只能够做为先后端同事交流的文档也有助于咱们更直观的管理API文档。在开发过程当中针对Controller的职能与用途,须要作好必要注释、良好的注释为先后端交流和后期维护都有很重要的做用。
注意:源码调试过程当中若是出现xml文件路径错误,须要参照Swagger配置“配置XML 文档文件”步骤,取消勾选而后再选中 ,将XML路径设置成与你的电脑路径匹配!
https://github.com/Impartsoft/Simple_Asp.Net_Core/tree/master/Simple_Asp.Net_Core 1.Swagger
微软官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/?view=aspnetcore-5.0
Swagger官网 https://swagger.io/