• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

help with asp.net automapper : SOLVED

Status
Not open for further replies.
Level 21
Joined
Mar 29, 2020
Messages
1,237
hey guys, after fighting with this thing for a few hours, reading all the documentation and still not figuring this out, I figured why not throw this out here to my favorite forum, and who knows maybe someone can help me...

I am trying to map an object to a dto that contains only some of it's fields. It's an inherited type, so I even created the same hierarchy for the Dto. I'm not sure where I'm going wrong.

I downloaded the nuget packages.

I added this mapper profile:

C#:
namespace API.helpers
{
    public class AutomapperProfiles : Profile
    {
     
     public AutomapperProfiles()
     {
        CreateMap<User,BaseUserDto>()
        .Include<Customer,CustomerDto>();

        CreateMap<Customer,CustomerDto>();

     
     }  
    }
}

referenced it in startup:

C#:
services.AddAutoMapper(typeof(AutomapperProfiles).Assembly);

injected it and used it in the controller:

C#:
[HttpGet("Customer/{username}")]
        public async Task<ActionResult<CustomerDto>> GetCustomer(string username)
        {
            var cust =  await _uow.customers.GetCustomerAsync(username);
           
            var custDto = _mapper.Map<CustomerDto>(cust);

            return custDto;
        }


and when I call that endpoint I get this mess:

Code:
{"statusCode":500,"errorMessage":"Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nActionResult\u00601 -\u003E CustomerDto\r\nMicrosoft.AspNetCore.Mvc.ActionResult\u00601[[API.Data.Model.Customer, API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -\u003E API.Data.DTOs.CustomerDto","details":"   at lambda_method104(Closure , Object , CustomerDto , ResolutionContext )\r\n   at PetShop.PetShopBackend.API.Controllers.AccountController.GetCustomer(String username) in C:\\Users\\johnDoe\\OneDrive\\Desktop\\MyAngularThing\\PetShop\\PetShopBackend\\API\\Controllers\\AccountController.cs:line 110\r\n   at lambda_method19(Closure , Object )\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\u003CInvokeActionMethodAsync\u003Eg__Awaited|12_0(ControllerActionInvoker invoker, ValueTask\u00601 actionResultValueTask)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.\u003CInvokeNextActionFilterAsync\u003Eg__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State\u0026 next, Scope\u0026 scope, Object\u0026 state, Boolean\u0026 isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\u003CInvokeFilterPipelineAsync\u003Eg__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.\u003CInvokeAsync\u003Eg__Logged|17_1(ResourceInvoker invoker)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.\u003CInvoke\u003Eg__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\r\n   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\r\n   at API.Middleware.ExceptionMiddleware.InvokeAsync(HttpContext context) in C:\\Users\\johnDoe\\OneDrive\\Desktop\\MyAngularThing\\PetShop\\PetShopBackend\\API\\Middleware\\ExceptionMiddleware.cs:line 42"}


here are my classes:

C#:
    public abstract class User
    {
        public int Id {get;set;}

        public string FirstName {get;set;}
        public string LastName {get;set;}

        public string UserName { get; set; }

        public byte[] hash { get; set; }

        public byte[] salt { get; set; }

        public string Email {get;set;}

        public Photo Avatar { get; set;}
        public string UserType { get; internal set; }
    }
}


    public class Customer : User
    {

        public string CreditInfo { get; set; }
     
        public DeliveryAdress Adress {get;set;}

         public ICollection<Order> Orders {get;set;}

     
    }
and my Dtos:

C#:
 public class BaseUserDto
    {
       
         public int Id {get;set;}

        public string FirstName {get;set;}
        public string LastName {get;set;}

        public string UserName { get; set; }

     
        public string Email {get;set;}

        public Photo Avatar { get; set;}
    }

    public class CustomerDto : BaseUserDto
    {

        public string CreditInfo { get; set; }
     
        public DeliveryAdress Adress {get;set;}

         public ICollection<Order> Orders {get;set;}
    }

if anyone reads this wall of code and can help me I will be forever grateful. (or at least for a nice while... lets not get carried away....)



edit:

so, I figured it out... just had to unwrap the actionResult before trying to use the automapper on it...

don't know if there is any reason not to delete this thread....
 
Last edited:
Status
Not open for further replies.
Top