ASP.Net Core 3.1 Razor Pages 事件处理程序无法在生产环境中运行

ASP.Net Core 3.1 Razor Pages event handler not working in production

我正在使用 ASP.NET 3.1 Core Razor Pages 并尝试在 onchange 事件时为我的下拉列表添加实时交互。

我有两个下拉列表,第一个包含省,第二个将使用 onchange 事件填充所选省的城市。

我在开发过程中一切顺利,但如果我发布我的项目,城市下拉列表不会发生实时交互,我会收到此错误

GET http://localhost/SectorPlan/Update?handler=GetCitiesList&GovernorateId=15 404 (Not Found)

这是我的代码。

Update.cshtml.cs

public JsonResult OnGetGetCitiesList(int GovernorateId)
    {
        var cities = manageCities.GetCities().Where(c => c.GovId == GovernorateId).ToList();

        return new JsonResult(cities);
    }

Update.cshtml

        <div class="form-group">
            <label asp-for="Governarate.Id">Governorate Name</label>
            <select class="form-control governarateDropdown" asp-for="Governarate.Id" asp-items="@(new SelectList(Model.Governarates,"Id", "Name"))">
                <option selected disabled>Select Governorate</option>
            </select>
            <span asp-validation-for="Governarate.Id" class="alert-danger"></span>
        </div>

        <div class="form-group">
             <label asp-for="City.Id">City Name</label>
             <select class="form-control cityDropdown" asp-for="City.Id">
                 <option selected disabled>Select City</option>
              </select>
              <span asp-validation-for="City.Id" class="alert-danger"></span>
         </div>

JS代码

//Bind City dropdownlist
$(".governarateDropdown").change(function () {
    var govId = $(this).val();
    console.log(govId);

    $.ajax({
        type: "GET",
        url: '/SectorPlan/Update?handler=GetCitiesList',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: $.param({ GovernorateId: govId }),
        success: function (result) {
            //alert(JSON.stringify(result));
            var item = "";
            $(".cityDropdown").find('option').not(':first').remove();
            item += '<option selected disabled value="">Select City</option>'
            $.each(result, function (i, city) {
                console.log(city);
                item += '<option value="' + city.id + '">' + city.name + '</option>'
            });
            $(".cityDropdown").html(item);
        }, //End of AJAX Success function  

        failure: function (result) {
            //alert(result.responseText);
            console.log(result.responseText);
        }, //End of AJAX failure function  
        error: function (result) {
            //alert(result.responseText);
            console.log(result.responseText);
        } //End of AJAX error function  

    });
});

任何帮助将不胜感激。

你试过像这样使用Url.PageLink方法吗?

type: "GET",
url: "@Url.PageLink(pageHandler:"GetCitiesList", pageName:"/SectorPlan/Update",values: route values if you have any)",