Razor 中的错误请求

Bad Request in Razor

我有一个 REST API 服务 + UI。如果我通过 Swagger/Postman 等进行 api 调用,我会在数据库中更新实体,但是当我从 UI 进行调用时(在表单中)api 表示 HTTP 400 Bad要求。 我厌倦了很多解决方案,例如 CORS/WebDAV/etc(不成功)这是我的代码:

UI 表格:

<form asp-action="EditRule" asp-controller="RuleList" method="post" asp-antiforgery=false  asp-route-id =@Model.Id>
                    <div asp-validation-summary=None class="text-danger"></div>
                    <div class="form-group">
                        <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" class="btn btn-primary" />
                    </div>
                     </form>
                </div> 
            </div>
        <div>

控制器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);

控制器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);
            }
        }

规则API服务:

 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;
            }

        }

在数据中,我收到了一个包含验证错误的错误请求。 我做错了什么?

public IActionResult Post([FromQuery] Rule value)更改为public IActionResult Post([FromBody] Rule value)

我创建了一个 MVC 项目和一个 API 项目。这是我发送请求和接收请求的方式:

[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();
    }
}