I've a REST API Service UI. If I do api call via Swagger/Postman etc, I'd an updated entity in DB, but when I doing it from UI (in form) api says HTTP 400 Bad Request. I tired a lot of solutions like CORS/WebDAV/etc (unsuccessful) There's my code:
UI Form:
<form asp-action="EditRule" asp-controller="RuleList" method="post" asp-antiforgery=false asp-route-id [email protected]>
<div asp-validation-summary=None ></div>
<div >
<input type="text" asp-for="Name" />
@*<span asp-validation-for="Name"/>*@
<input type="text" asp-for="Description" />
@* <span asp-validation-for="Description"/>*@
<input type="checkbox" value="IsActive" asp-for="IsActive" />
<input type="text" asp-for="AddedByUser" />
<input type="text" asp-for="QueryText" />
<input type="text" asp-for="RunAt" />
<input type="submit" value="Save" />
</div>
</form>
</div>
</div>
<div>
Controller API:
[Route("api/[controller]")]
[ApiController]
public class RuleController : ControllerBase
...
[HttpPost]
public IActionResult Post([FromQuery] Rule value)
{
var ent = _rulesDb.Get(value.Id);
if (ent == null)
{
return NotFound(ent);
}
ent.AddedByUser = value.AddedByUser;
ent.Description = value.Description;
ent.QueryText = value.QueryText;
ent.RunAt = value.RunAt;
_rulesDb.Save();
return Ok(ent);
Controller UI (RuleListController):
[HttpGet("Edit/{id}")]
public async Task<ActionResult> EditRule(int id)
{
var data = await _client.RuleAPIService.GetRule(id);
return View(new RuleViewModel(data));
}
[HttpPost("Edit/{id}")]
public async Task<ActionResult> EditRule([FromRoute] int id, string name, string description, string addedByUser, string queryText, int runAt)
{
try
{
var rule = new Rule()
{
AddedByUser = addedByUser,
Name = name,
Description = description,
QueryText = queryText,
RunAt = runAt
};
await _client.RuleAPIService.UpdateRule(id, rule);
return RedirectToAction("Index");
}
catch
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
RuleAPIService:
public async Task UpdateRule(int id, Rule rule)
{
var client = _clientFactory.CreateClient("windowsAuthClient");
client.BaseAddress = new Uri(_configuration["API_URL"]);
rule.Id = id;
try
{
var data = await client.PostAsJsonAsync<Rule>($"api/Rule", rule);
}
catch (Exception)
{
throw;
}
}
in data I'd a Bad Request with validation errors. What I doing wrong?
CodePudding user response:
Change public IActionResult Post([FromQuery] Rule value)
to public IActionResult Post([FromBody] Rule value)
I created an MVC project and an API project. This is how I send the request and how I receive the request:
[HttpPost("Edit/{id}")]
public async Task<ActionResult> EditRule([FromRoute] int id, string name, string description, string addedByUser, bool isActive)
{
try
{
var rule = new Rule()
{
AddedByUser = addedByUser,
Name = name,
Description = description,
IsActive = isActive
};
var content = new StringContent(JsonConvert.SerializeObject(rule), Encoding.UTF8, "application/json");
var httpClient = _httpClientFactory.CreateClient();
// the comment line can work as well
//var httpResponseMessage = await httpClient.PostAsync("https://localhost:7006/api/Rule", content);
var httpResponseMessage = await httpClient.PostAsJsonAsync("https://localhost:7006/api/Rule", rule);
return RedirectToAction("Index");
}
catch
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
[Route("api/[controller]")]
[ApiController]
public class RuleController : ControllerBase
{
[HttpPost]
public IActionResult Post([FromBody] Rule value)
{
//var ent = _rulesDb.Get(value.Id);
//var id = value.Id;
return Ok();
}
}