Put Requist not working

osyris 236 Reputation points
2021-10-15T20:41:08.667+00:00

Im trying to update a Product with a new Image and new descriptions

C#:

[HttpPut("editProduct")]
 public async Task<IActionResult> EditProduct([FromForm]ProductDto dto)
 {
  var find = await _dbContext.Products.FirstOrDefaultAsync(x => x.Id == dto.Id);

 if (find == null)
 return BadRequest("Could not find product");

//..... other code

ProductDto:

 public class ProductDto
 {
 public int Id { get; set; }
 public string Name { get; set; }
 public string Description { get; set; }
 public int Price { get; set; }
 public IFormFile File { get; set; }
 public string Category { get; set; }
 }

front-end, React Js

const formdata = new FormData();
    if(file !== null){
    formdata.append("file", file, file.name);
}
    formdata.append("name", name);
    formdata.append('description',description);
    formdata.append('price',price);
    formdata.append('category', category)

    const resfile = await axios.put("https://localhost:44332/api/AdminAuth/editProduct", formdata)
    if(resfile.status === 200){
    console.log("succesfull uploaded image")

It keeps giving a Error 400 even when i put a breakpoint at the first line:

 var find = await _dbContext.Products.FirstOrDefaultAsync(x => x.Id == dto.Id);
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
{count} votes

1 answer

Sort by: Most helpful
  1. osyris 236 Reputation points
    2021-10-16T03:12:53.04+00:00

    the rest of the code:

    [Route("api/[controller]")]
        [ApiController]
        public class AdminAuthController : ControllerBase
    
    [HttpPut("editProduct")]
            public async Task<IActionResult> EditProduct([FromForm]ProductDto dto)
            {
             var find = await _dbContext.Products.FirstOrDefaultAsync(x => x.Id == dto.Id);
    
                if (find == null)
                    return BadRequest("Could not find product");
    
                if (dto.File != null)
                {
                    if (dto.File.Length > 0)
                    {
    
                        string root = _env.WebRootPath + "/client/src/images/ProductImages/";
                        string oldFileName = find.ImagePath;
    
                        if (System.IO.File.Exists(Path.Combine(root, oldFileName)))
                            System.IO.File.Delete(Path.Combine(root, oldFileName));
    
    
                        string filename = Guid.NewGuid().ToString() + "_" + dto.File.FileName;
                        string filepath = Path.Combine(root, filename);
    
                        using (var stream = System.IO.File.Create(filepath))
                        {
                            await dto.File.CopyToAsync(stream);
                        }
    
                        find.ImagePath = filename;
                    }
                }
                if (!string.IsNullOrWhiteSpace(dto.Name))
                    find.Name = dto.Name;
    
                if (!string.IsNullOrWhiteSpace(dto.Description))
                    find.Description = dto.Description;
    
                if (dto.Price > 0)
                    find.Price = dto.Price;
    
                if (!string.IsNullOrWhiteSpace(dto.Category))
                    find.Category = dto.Category;
    
    
                await _dbContext.SaveChangesAsync();
    
                return Ok();
            }
    

    the rest of the front end is just a button that starts the "edit" function
    and there are some inputs that have a onChange handler function

    0 comments No comments