Mapper未在asp.net web api中初始化

时间:2018-02-15 07:12:12

标签: c# asp.net-web-api

此代码适用于Employee Controller类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Xebia.EmployeeDB.DAL;
using AutoMapper;

namespace Xebia.EmployeeDB.API.Controllers
{
    [RoutePrefix("api/Employee")]
    public class EmployeeController : ApiController
    {

        [Route("")]
        public IEnumerable<Employee> Get()
        {
            using (EmployeeDataEntities entity = new EmployeeDataEntities())
            {
                return Mapper.Map<List<Employee>>(entity);
                //var mapper = cfg.CreateMapper();
            }
        }
}

这是AutomapperConfig类文件。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Xebia.EmployeeDB.DTO;

namespace Xebia.EmployeeDB.DAL
{
    public class AutomapperConfig
    {
        //public AutomapperConfig()
        //{
        public static void Initialze()
        {
            //Mapper.Initialize(cfg => 
//Employee is the class under Data access layer class library and EmployeeDto is the class inside DTO class library.
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Employee, EmployeeDto>()
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.SerialNumber))
                .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
                .ForMember(dest => dest.MiddleName, opt => opt.MapFrom(src => src.MiddleName))
                .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
                .ForMember(dest => dest.EmailID, opt => opt.MapFrom(src => src.EmailID))
                .ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => src.JoiningDate))
                .ForMember(dest => dest.AnnualCTC, opt => opt.MapFrom(src => src.AnnualCTC))
                .ForMember(dest => dest.DesignationID, opt => opt.MapFrom(src => src.DesignationID))
                .ForMember(dest => dest.CurrencyID, opt => opt.MapFrom(src => src.CurrencyID))
                .ForMember(dest => dest.LocationID, opt => opt.MapFrom(src => src.LocationID))
                .ForMember(dest => dest.ProjectID, opt => opt.MapFrom(src => src.ProjectID))
    ;
            });

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<EmployeeDto, Employee>()
                .ForMember(dest => dest.SerialNumber, opt => opt.MapFrom(src => src.SerialNumber))
                .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
                .ForMember(dest => dest.MiddleName, opt => opt.MapFrom(src => src.MiddleName))
                .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
                .ForMember(dest => dest.EmailID, opt => opt.MapFrom(src => src.EmailID))
                .ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => src.JoiningDate))
                .ForMember(dest => dest.AnnualCTC, opt => opt.MapFrom(src => src.AnnualCTC))
                .ForMember(dest => dest.DesignationID, opt => opt.MapFrom(src => src.DesignationID))
                .ForMember(dest => dest.CurrencyID, opt => opt.MapFrom(src => src.CurrencyID))
                .ForMember(dest => dest.LocationID, opt => opt.MapFrom(src => src.LocationID))
                .ForMember(dest => dest.ProjectID, opt => opt.MapFrom(src => src.ProjectID))
    ;
            });
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Xebia.EmployeeDB.DAL;

namespace Xebia.EmployeeDB.API
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            AutomapperConfig.Initialze();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

请建议需要做什么。当执行到达Employee Controller类中的Get方法内的return语句时,如何初始化映射器作为异常。 在解决方案中,我做了三个项目:1。对于API 2.对于DAL 3.For DTO 我的DAL包括automapperconfig类文件,Dto包括EmployeeDTo等类。 我的API包括Employeecontroller和get方法,当执行到达Get方法中的return语句时,它显示错误异常。

我在这里添加了Global.asax文件,我在其中调用了Automapperconfig.Initialize方法,但它显示了执行时的错误。 错误:已经被调用。

0 个答案:

没有答案