Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNetCore 3.0 (upgraded from 2.2) routes seem to break when Controller Method has Async suffix

I upgraded my project to Core 3.0 from 2.2 and everything seems to work well, except one get JSON request. I have the following Js code doing an Ajax request to the Home controller:

    var isLastPage = false;
    var incidentModel = {
        incidents: ko.observableArray([]),
        getIncidents: function(a) {
            var self = this;
            //var $incdiv = $('#incidentsList');
            $.getJSON('@Url.Action("AjaxPageAsync", "Home")', { page: page++, user: user, type: type }, function(data) {
                //console.log(data);
                self.incidents(self.incidents().concat(data));
                if (data[0].IsLastPage) {
                    isLastPage = true;
                }
                a();
            });
        }
    }

The Home controller is like this:

public async Task<ActionResult> AjaxPageAsync([FromQuery] string type, [FromQuery] string user, [FromQuery] int? page)
{
       //Get some json data
       return Json(Incident);
}

As you may tell, I'm using knockout (version 3.5.0 & jquery 3.3.0) which both are working fine on other pages. However, the getJSON request in the js code above is returning a 404:

GET https://localhost:44366/Home/AjaxPageAsync?page=1&user=&type=&_=1575386778917 404

The debug output looks similar:

Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/2.0 GET https://localhost:44366/Home/AjaxPageAsync?page=1&user=&type=&_=1575386536876

The 2.2 version is identical and works fine. I thought it might be the number appended onto the end of the url, but that exists in 2.2 as well.

At this point, I'm thinking it's a problem with the syntax of my AjaxPageAsync task in the controller, but not sure what it should be.

Anyone know where I'm going wrong here? Thank you.

like image 724
Sum None Avatar asked Dec 13 '25 04:12

Sum None


1 Answers

According to https://github.com/aspnet/AspNetCore/issues/8998, in .NET Core 3.0 Async is trimmed from Action name. Your endpoint is available at /Home/AjaxPage. You can change this behaviour by replacing

services.AddControllers();

with

services.AddControllers(options => options.SuppressAsyncSuffixInActionNames = false);

in ConfigureServices method, or just use the new routes

like image 101
Piotr L Avatar answered Dec 15 '25 16:12

Piotr L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!