0

I have an ASP.NET Core 6 Web API project and I am using AutoMapper. This is how I am configuring it in my Startup class:

services.AddAutoMapper(Assembly.GetExecutingAssembly());

Is there a way to configure AutoMapper to throw a custom exception when 2 types cannot be mapped? I have more than 10 mapping profiles in my project and I would like a certain error to be thrown when the types cannot be mapped.

I am using .NET 6.0

2
  • 1
    Automapper doesn't let an exception escape directly from the mapping process,but you could try and wrap it in its own Exception follow this issue :stackoverflow.com/questions/22409895/… Commented Sep 27, 2023 at 2:47
  • I mentioned that I have more mapping profiles in the project and I am mapping in many places, so I would prefer a solution where I configure this exception in one place Commented Sep 27, 2023 at 6:17

1 Answer 1

0

AutoMapper throws an AutoMapperMappingException on missed mapping errors.

You can a use middleware to handle the exceptions of type AutoMapperMappingException and do any on it and return specific messages when this error rises.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.