如何在Web API URL中动态指定查询字符串

时间:2018-09-13 11:56:33

标签: c# asp.net-web-api routes

我是C#和Web API的新手。我有一个带有Get方法的WebAPI控制器,如下所示:

 public class peopleController : ApiController
 {
     [HttpGet]
     public IHttpActionResult getAllPeople(string Name, string Age)
     {
        //Return something
     }
 }

我的WebApiConfig是这样的:

config.Routes.MapHttpRoute(
            name: "getAllPeopleApi",
            routeTemplate: "people",
            defaults: new { controller = "people", action = "getAllPeople" }

        );

如果我这样调用我的网址:http://localhost:xxx/people?Name=&Age=。运行正常。

但是当我像所有这些调用时: http://localhost:xxx/peoplehttp://localhost:xxx/people?Name=http://localhost:xxx/people?Age=

我收到此错误消息:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:xxxx/......'.","MessageDetail":"No action was found on the controller 'people' that matches the request."}

我尝试设置routeTemplate: "people/{Name}/{Age}"。现在,当我运行此网络API Error 404.0 Not Found

1 个答案:

答案 0 :(得分:0)

routeTemplate: "people/{Name}/{Age}"

这部分不是QueryString,这是动态路径。 这意味着您的路径变为Domain/people/SomeName/SomeAge?QueryString=Whatever