Startup.cs代码调整成如下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using Simple_Asp.Net_Core.Data; using Simple_Asp.Net_Core.ServiceProvider; using System; 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.AddCORS(); services.AddMvc(); services.AddSwagger(); services.AddScoped<ICommanderRepo, MockCommanderRepo>(); services.AddControllers().AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); } // 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.UseCors("CorsTest"); app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } 在编写Restful API之前还差最后一步,AutoMapper的使用前面已经创建了Command实体与CommandReadDto Dto,现在我们要让这Commond实体能够自动转换成CommandReadDto Dto
AutoMapper引入Nuget包 再一次配置Startup.cs代码如下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using Simple_Asp.Net_Core.Data; using Simple_Asp.Net_Core.ServiceProvider; using System; 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.AddCORS(); services.AddMvc(); services.AddSwagger(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddScoped<ICommanderRepo, MockCommanderRepo>(); services.AddControllers().AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); } // 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.UseCors("CorsTest"); app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } 新建Profiles文件夹,新建CommandsProfile.cs AutoMpper映射配置类代码如下:
using AutoMapper; using Simple_Asp.Net_Core.Dtos; using Simple_Asp.Net_Core.Models; namespace Simple_Asp.Net_Core.Profiles { public class CommandsProfile : Profile { public CommandsProfile() { //Source -> Target CreateMap<Command, CommandReadDto>(); } } } 在Controllers文件夹下新建控制器CommandsController.cs 将接口注入至 CommandsController 构造函数中 private readonly ICommanderRepo _repository; private readonly IMapper _mapper; public CommandsController(ICommanderRepo repository, IMapper mapper) { _repository = repository; _mapper = mapper; } 这时候我们就可以实现 Commands 中 api/commands 请求 //GET api/commands [HttpGet] public ActionResult<IEnumerable<CommandReadDto>> GetAllCommmands() { var commandItems = _repository.GetAllCommands(); return Ok(_mapper.Map<IEnumerable<CommandReadDto>>(commandItems)); } 调试项目使用swagger调用api/commands接口,后端能够成功返回数据! 接下来就完成剩下的几种请求 在Dtos文件夹下新建CommandUpdateDto.cs 与 CommandCreateDto.cs