Setup Automapper in ASP.NET Core
2018-01-13 18:41
447 查看
1、添加Nuget Package
2、修改Startup.cs
3、从Profile派生一个自定义的映射配置类
4、在代码中调用映射对象
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
2、修改Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAutoMapper(); }
3、从Profile派生一个自定义的映射配置类
public class MappingProfile : Profile { public MappingProfile() { CreateMap<User, UserDTO>(); } }
4、在代码中调用映射对象
public class HomeController : Controller { private readonly IMapper _mapper; public HomeController(IMapper mapper) { _mapper = mapper; } public IActionResult Index() { var user = new User { Id = 1, Name = "bidianqing" }; var userDTO = _mapper.Map<User, UserDTO>(user); return View(); } }
相关文章推荐
- Setup Entity Framework Core in ASP.NET Core
- [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?
- Bower In ASP.NET Core
- 体验 ASP.NET Core 集成测试三剑客:xUnit.net、TestServer、EF Core InMemory
- Static Resources In ASP.NET Core 1.0
- [转]How to use an Area in ASP.NET Core
- Adding Cache-Control headers to Static Files in ASP.NET Core
- Asp.net mvc 2 in action 笔记-3 Areas AJAX AutoMapper
- User Secrets – Storing sensitive data in ASP.NET Core projects
- Routing in ASP.NET Core
- Formatting AutoGenerateColumns in an ASP.NET Grid
- Sharing Configuration in ASP.NET Core SPA Scenarios
- Middleware In ASP.NET Core
- WebSocket In ASP.NET Core(二)
- Allow Only Ajax Requests For An Action In ASP.NET Core
- Upload Files In ASP.NET Core 1.0 (Form POST And JQuery Ajax)
- ASP.NET Core Logging in Elasticsearch with Kibana
- asp.net core in centos
- [转]How to override HandleUnauthorizedRequest in ASP.NET Core
- [转]File uploads in ASP.NET Core