GET api cannot get its arguments

733 Views Asked by At

I have simple aspnet core api controller, when a get action, but this action cannot get its arguments from query path. Is there anything I'm using the webapi routing wrong?

action:

    [HttpGet]
    [AllowAnonymous]
    public string Test(string arg) // arg is always null no matter what path I use
    {
        return arg;
    }

routes:

routes.MapAreaRoute(
                name: "ad",
                areaName: "Ad",
                template: "api/{area:exists}/{controller}/{action}/{id}",
                defaults: new { controller = "Ad" }
            );
routes.MapRoute(
                name: "default",
                template: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );

logs:

> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://localhost/api/Ad/Ad/Test/asdfasdf dbug: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[9] AuthenticationScheme: Bearer was not authenticated. dbug: Microsoft.AspNetCore.Routing.RouteConstraintMatcher[1] Route value 'Ad' with key 'area' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.CompositeRouteConstraint'. dbug: Microsoft.AspNetCore.Routing.RouteConstraintMatcher[1] Route value 'Ad' with key 'area' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.CompositeRouteConstraint'. dbug: Microsoft.AspNetCore.Routing.RouteBase[1] Request successfully matched the route with name 'ad' and template 'api/{area:exists}/{controller}/{action}/{id}'. dbug: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] Executing action Api.Areas.Ad.Controllers.AdController.Test (Api) info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] Executing action method Api.Areas.Ad.Controllers.AdController.Test (Api) with arguments () - ModelState is Valid dbug: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] Executed action method Api.Areas.Ad.Controllers.AdController.Test (Api), returned result Microsoft.AspNetCore.Mvc.ObjectResult. dbug: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[4] No information found on request to perform content negotiation. dbug: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[2] Selected output formatter 'Microsoft.AspNetCore.Mvc.Formatters.HttpNoContentOutputFormatter' and content type '' to write the response. info: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[1] Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext. info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] Executed action Api.Areas.Ad.Controllers.AdController.Test (Api) in 193.6691ms dbug: Microsoft.AspNetCore.Server.Kestrel[9] Connection id "0HL9N6GCMPEP7" completed keep alive response. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 218.2123ms 204

1

There are 1 best solutions below

2
On BEST ANSWER

If the URI as shown in the exception(http://localhost/api/Ad/Ad/Test/asdfasdf) allows you to enter the method. One of these 2 solutions should work:

1: Remove /{id} from the route and change your request Uri too: http://localhost/api/Ad/Ad/Test?args=asdfasdf

2: Either rename /{id} in the route too /{args} or rename the parameter of your method to ID.