代码之家  ›  专栏  ›  技术社区  ›  Chabba Mohamed Nadir

使用mediatR映射从.net中的Angular接收的数据

  •  0
  • Chabba Mohamed Nadir  · 技术社区  · 3 年前

    我正在FormData中从我的angular应用程序发送数据 这是发送的数据的打印 键:tePartNumber,值:test 关键字:customerPN,值:test 关键字:projectNumber,值:test 密钥:oem,值:994f433e-f36b-1410-9006-000a33fe38d1 键:harnessMaker,值:e14f433e-f36b-1410-9006-00a33fe38d1 键:numberOfConnectors,值:2 密钥:cdPath,值:[对象文件] 键:pdPath,值:[对象文件] 键:excelFilePath,值:null 键:imagePath,值:[对象文件] 键:componentsData,值:{“componentId”:“e94f433e-f36b-1410-9006-00a33fe338d1”,“side”:“left”} 键:componentsData,值:{“componentId”:“e94f433e-f36b-1410-9006-00a33fe338d1”,“side”:“right”} 键:电缆数据,值:{“componentId”:“dd4f433-e-36b-1410-9006-00a33fe338d1”} 在我的.net部分中,我使用MediatR接收数据并进行映射

            [HttpPost]
            public async Task<ActionResult<RequestResponseMessage>> SaveDrawingData([FromForm] SaveDrawingDataCommand command)
            {
                try
                {
                    Console.WriteLine("Compoentdata :"+ command.componentsData.Count);
                    var response = await Mediator.Send(command);
                    return Ok(response);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An error occurred while saving the drawing data.");
                    return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
                }
            }
    

    该调用应将接收到的数据映射到SaveDrawingDataCommand的实例

         public class SaveDrawingDataCommand : IRequest<SaveDrawingDataResponse>
        {
            public Guid Id { get; set;}
            public string? tePartNumber { get; set; }
            public string? customerPN { get; set; }
            public string? projectNumber { get; set; }
            public string? oem { get; set; }
            public string? harnessMaker { get; set; }
            public int numberOfConnectors { get; set; }
            public List<ComponentsDataDto>? componentsData { get; set; }
            public List<CablesDataDto>? cablesData { get; set; }
            public IFormFile? cdPath { get; set; }
            public IFormFile? pdPath { get; set; }
            public IFormFile? excelFilePath { get; set; }
            public IFormFile? imagePath { get; set; }
    
        }
        
    

    和SaveDrawingDataCommand实现IRequest 这会自动调用SaveDrawingDataCommandHandler

     public async Task<SaveDrawingDataResponse> Handle(SaveDrawingDataCommand request, CancellationToken cancellationToken)
        {
            var command = _mapper.Map<SaveDrawingDataCommand>(request);
    
            var drawingData = new Backend.Domain.Entities.DrawingData
            {
    
                tePartNumber = request.tePartNumber,
                customerPN = request.customerPN,
                projectNumber = request.projectNumber,
                oem =Guid.Parse( request.oem),
                harnessMaker = Guid.Parse(request.harnessMaker),
                numberOfConnectors = request.numberOfConnectors, 
    
            };
    
            //  Add the files paths to DrawingData
            drawingData.cdPath = await SaveFile(request.cdPath, drawingData.tePartNumber + "CD.pdf", baseUrl);
            drawingData.pdPath = await SaveFile(request.pdPath, drawingData.tePartNumber + "PD.pdf", baseUrl);
            drawingData.excelFilePath = await SaveFile(request.excelFilePath, drawingData.tePartNumber + "SmartAssembly.xlsm", baseUrl);
            drawingData.imagePath = await SaveFile(request.imagePath, drawingData.tePartNumber +".jpeg", baseUrl);
            
            var drawings = await _drawingDataRepository.Create(drawingData, cancellationToken);
    
    
           foreach (ComponentsData componentDto in request.componentsData)
            {
                 Console.WriteLine($"ComponentId: {componentDto.componentId}, Side: {componentDto.side}");
                var componentsData = new ComponentsData
                {
                    componentId = componentDto.componentId,
                    side = componentDto.side,   
    
                };
               
                 await _componentWithSideRepository.Create(componentsData, cancellationToken);
                drawingData.componentsData.Add(componentsData);
            }
    
            // Ajouter les câbles
            foreach (CablesData cableDto in request.cablesData)
            { Console.WriteLine($"CablesId: {cableDto.cablesId}, Length: {cableDto.length}");
                var cablesData = new CablesData
                {
                    cablesId = cableDto.cablesId,
                    length = cableDto.length,
    
                };
    
                await _cablesDataRepository.Create(cablesData, cancellationToken);
                drawingData.cablesData.Add(cablesData);
            }
        
            var response = new SaveDrawingDataResponse
            {
                Message = "Drawing data saved successfully",
                Succeed = true,
                // You can set any other properties in the response as needed
            };
    
            return response;
        }
    
    

    问题是,我可以访问所有接收到的数据,除了组件数据和电缆数据。当我试图控制台记录它们时,我总是有空的项目,当我试图在调用句柄的控制器中打印组件数据的值时,我也得到了空的iteam

     Console.WriteLine("Compoentdata :"+ command.componentsData.Count);
    

    Compoentdata :0

    我已经花了10多天的时间试图解决这个问题,但我输了。。。有人能帮帮我吗!!

    0 回复  |  直到 3 年前
        1
  •  0
  •   Raymonde Akiki    3 年前

    这个问题可能与您从angular发送数据的方式有关。 componentsData和cablesData没有作为输入中的列表正确处理,因此映射不适用于这两个属性。下面是应该发送到API的Json示例:

         {
      "Id": "c719b1fd",
      "tePartNumber": "ABC123",
      "customerPN": "XYZ456",
      "projectNumber": "P123",
      "oem": "Example OEM",
      "harnessMaker": "Harness Maker Inc.",
      "numberOfConnectors": 5,
      "componentsData": [
        {
          "componentId": "1",
          "side": "left"
        },
        {
          "componentId": "2",
          "side": "right"
        }
      ],
      "cablesData": [
        {
          "componentId": "3",
          "side": "right"
        },
        {
          "componentId": "4",
          "side": "right"
        }
      ],
      "cdPath": null,
      "pdPath": null,
      "excelFilePath": null,
      "imagePath": null
    }