Can I pass more than one variables to an [HttpGet] ApiController action? For example:
[HttpGet]
public IPagedList<Something> GetAll(int? page, string sortOrder="")
{
...
}
I tested using
curl http://localhost:[port]/api/[controller]/GetAll?page=3&sortOrder=Name
But the action was never called. The returning message is: No HTTP resource was found that matches the request URI. What went wrong?
—Update—
If I check the querystring in HttpActionContext.Request.RequestUri.Query, the AbsoluteUri truncated at the ampersand and gives me only page=3.
—End of Update
I also tried one variable:
[HttpGet]
public IPagedList<Something> GetAll(int? page)
{
...
}
The break point inside can be reached without problem in the test
curl http://localhost:%5Bport%5D/api/%5Bcontroller%5D/GetAll?page=3
I also created a view model:
public class ListRequestModel
{
public string SortOrder { get; set; }
public string CurrentFilter { get; set; }
public string SearchString { get; set; }
public int PageNumber { get; set; }
}
Action is like:
[HttpGet]
public IPagedList<Something> GetAll(ListRequestModel model)
{
...
}
Then test
curl http://localhost:%5Bport%5D/api/%5Bcontroller%5D/GetAll?PageNumber=3&SortOrder=Name
This time, the break point inside can be reached, which is a great thing. But the model is null, not populated with the data in the query string.
So, what is the correct way to pass multiple variables into an action method? Please help. Thank you.
———-Update———–
As requested, pasted below is the Routing Table in the RouteConfig.cs file:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In Global.asax.cs, AreaRegistration.RegisterAllAreas(); goes before RouteConfig.RegisterRoutes(RouteTable.Routes); But no Area routes start with api. All my other tests on this ApiController can get into the action without problem.
Your routing and setup is fine. And of course that you can pass multiple arguments to a controller.
You just need to improve your Linux foo because
&has a special meaning in Linux shells…From Wikipedia:
So you need escape the ampersand
&with a backlash\in your your commandOr use quotation marks
""around the url: