【问题标题】:Async Method returns 404异步方法返回 404
【发布时间】:2021-12-12 16:17:39
【问题描述】:

我遇到了以下问题。

在我的控制器中,此方法应该从数据库中返回带有宠物的 JSON。但正在被调用,服务器返回 404。

public async Task<JsonResult> GetPetsAsync(int ownerId )
        {
            var pets = await _dataContext.Pets
                .Where(p => p.Owner.Id == ownerId)
                .OrderBy(p => p.Name)
                .ToListAsync();
            return Json(pets);
        }

================================================ ======

这是为该特定所有者查找宠物的方法提供 OwnerId 的表单。

<div class="row m-auto align-content-center">
    <div class="col-md-4">
        <form asp-action="Assign" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Id" />

            <div class="form-group">
                <label asp-for="OwnerId" class="form-label fw-bold"></label>
                <select asp-for="OwnerId" asp-items="Model.Owners" class="form-control"></select>
                <span asp-validation-for="OwnerId" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="PetId" class="form-label fw-bold"></label>
                <select asp-for="PetId" asp-items="Model.Pets" class="form-control"></select>
                <span asp-validation-for="PetId" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Remarks" class="form-label fw-bold "></label>
                <textarea asp-for="Remarks" class="form-control"></textarea>
                <span asp-validation-for="Remarks" class="text-danger"></span>
            </div>

            <div class="form-group mt-3">
                <button value="Assign" type="submit" class="btn btn-primary btn-sm"><i class="bi bi-calendar-plus-fill"></i></button>
                <a asp-action="Index" class="btn btn-success btn-sm"><i class="bi bi-arrow-return-left"></i> Go Back</a>
                <span asp-validation-for="Remarks" class="text-danger"></span>
            </div>


        </form>
    </div>
</div>

在我看来——对 GetPetsAsync 的 AJAX 请求

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script type="text/javascript">
        $(document).ready(function () {
            $("#OwnerId").change(function () {
                $("#PetId").empty();
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("GetPetsAsync", "Agenda")',
                    //Take the OwnerId and send it to the JsonResult Method to retrieve the pets
                    data: { ownerId: $("#OwnerId").val() },
                    dataType: 'Json',
                    success: function (pets) {
                        $("#PetId").append('<option value="0">(Select a pet...)</option>');
                        $.each(pets, function (i, pet) {
                            $("#PetId").append('<option value="'
                                + pet.id + '">'
                                + pet.name + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve pets. ' + ex.statusText);
                    }
                });
                return false;
            })
        });
    </script>

【问题讨论】:

  • 表示没有找到url,它必须在不同的位置。
  • @Grumpy,我验证了它指向正确的位置,POST localhost:44313/Agenda/GetPetsAsync
  • 你能分享你的Agenda控制器吗?你改变了控制器的路由和动作吗?

标签: ajax asp.net-core asp.net-core-mvc


【解决方案1】:

此功能更改似乎正在影响您。

Async suffix for controller action names will be trimmed by default #14716

您可以从视图端代码中删除“Async”后缀。这就是为什么重命名您的方法“解决”了您的问题。

您可以使用SuppressAsyncSuffixInActionName 控制应用启动时的这种行为

builder.Services.AddMvc(options =>
{
    options.SuppressAsyncSuffixInActionNames = false;
});

【讨论】:

    【解决方案2】:

    确保使用了“httppost”属性。如果继续报错,请检查 ajax 部分中的数据部分。

    【讨论】:

    • 我试过了,但没有修复 404。但是我更改了控制器名称,它起作用了,?
    【解决方案3】:

    我找到了解决方案。令人难以置信的是,只需将方法名称更改为 GetPetAsyncronous,问题就解决了。我不知道为什么以前的方法行不通。但它奏效了。

    【讨论】:

    猜你喜欢
    • 2013-05-03
    • 2012-12-20
    • 2014-10-22
    • 2018-06-10
    • 1970-01-01
    • 2016-08-27
    • 2016-05-27
    • 2020-04-28
    • 2019-03-28
    相关资源
    最近更新 更多