GET api 无法获取其参数

GET api cannot get its arguments

我有简单的 aspnet 核心 api 控制器,当一个 get 动作时,但是这个动作无法从查询路径中获取它的参数。是不是我在使用 webapi 路由有什么问题?

操作:

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

路线:

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

日志:

> 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

如果异常中显示的URI(http://localhost/api/Ad/Ad/Test/asdfasdf)允许您进入该方法。这两种解决方案之一应该有效:

1: 从路由中删除 /{id} 并更改您的请求 URI: http://localhost/api/Ad/Ad/Test?args=asdfasdf

2: 也可以在路线中重命名 /{id} /{args} 或将您的方法的参数重命名为 ID。