Bonjour, j’utilise un DTO pour une valeur unique (Id) et j’essaie de poster sur Db avec ApiController, mais lorsque je clique sur un bouton, je continue à avoir l’erreur 400 qui me renvoie à l’erreur xhr.send. (im using asp.net core 2.1) Code: @section Scripts {
$(document) .ready(function() { $(".js-toggle-HandShake") .click(function(e) { var button = $(e.target); console.log(button.attr("data-QuettaOfferId")); //Value=24 >> OK $.post("/Api/HandShake/", { QuettaOfferId: button.attr("data-QuettaOfferId") }) // Error in > POST https://localhost:44339/Api/HandShake/ 400 () & //in jquery>> xhr.send( options.hasContent && options.data || null ); .done(function() { button .text("Chousen"); }) .fail(function() { alert("Something failed"); }); }); });
} & le code ApiController
[Microsoft.AspNetCore.Mvc.Route("api/[controller]")] [ApiController] [Microsoft.AspNetCore.Authorization.Authorize] public class HandShakeController : ControllerBase { private readonly ApplicationDbContext _context; private readonly UserManager _userManager; // private readonly IHostingEnvironment hostingEnvironment; public HandShakeController(ApplicationDbContext context ,UserManager userManager/*, IHostingEnvironment environment*/) { _context = context; _userManager = userManager; //hostingEnvironment = environment; } [Microsoft.AspNetCore.Mvc.HttpPost] // public IHttpActionResult HandShakes(HandShakeDto dto) public IActionResult HandShakes(HandShakeDto dto) { var userId = _userManager.GetUserId(User); var check = _context.Quetta.Where(u => u.SiteUserId == userId); if ( _context.handShakes.Any(f => f.QuettaOfferId == dto.QuettaOfferId)) return BadRequest("Some error Msg"); if (check.Any()) { var hand = new HandShake { QuettaOfferId = dto.QuettaOfferId }; try { _context.handShakes.Add(hand); _context.SaveChangesAsync(); return Ok(); } catch (Exception e) { Console.WriteLine(e); return BadRequest("Some error Msg"); } } else{ return BadRequest("");} // Check if the user id that publish the ed = login user. //if so add the offer to selected table, } }
im using asp.net core 2.1 & suspectons fortement que le problème est dans le ApiController mais im pas sûr.
Le DTO
public class HandShakeDto { public int QuettaOfferId { get; set; } }
Essayez de remplacer $.post("/Api/Hand/", { QuettaOfferId: button.attr("data-QuettaOfferId") })
par $.post("/Api/HandShake/", { QuettaOfferId: button.attr("data-QuettaOfferId") })
comme votre nom de contrôleur d’api est HandShakeController