Handle Automapper exception?

ⅰ亾dé卋堺 提交于 2019-12-25 02:46:26

问题


Using mvc5 with automapper I have following:

In Controller :

  [HttpPost]
        public ActionResult LunchMenu_Create_Index(VmSysMenuCreate menu)
        {
            try
            {
                var domainMenu = Mapper.Map<VmSysMenuCreate, Menu>(menu);
            }

            catch (Exception ex)
            {
                return Content("Error msg");
            }

            return Content("Succes");
        }

Mapping:

  Mapper.CreateMap<VmSysMenuCreate, Menu>()
                .ForMember(c => c.Id, op => op.MapFrom(v => v.Id))
                .ForMember(c => c.MenuDate, op => op.ResolveUsing(data =>
                {
                    try
                    {
                        DateTime convertedDate = Convert.ToDateTime(data);
                        return convertedDate;
                    }
                    catch (Exception ex)
                    {

                        throw new Exception("Date not in correct format", ex);
                    }

                }));

Doing this I was excpected to catch automapper error when tryng to map my objects , but this is not working like I was expect.

How can I catch error in controller that is thrown from automapper? If you have any question please ask me.. Thanks for you attention!


回答1:


Not sure, but probably you have the option CLR Exceptions Thrown ON.

Select "Exceptions..." from "Debug" menu. (or press CTRL + ALT + E)

If the highlighted Thrown option is ON, you'll see the debugger stopping in the innermost exception.



来源:https://stackoverflow.com/questions/22350603/handle-automapper-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!