1

I have this controller action:

[ActionName("GetMany")] [HttpGet]
public IHttpActionResult GetMany([FromUri] IEnumerable<int> ids)
{
    //Do something...
}

I'm also using a custom model binder as described in this SO question. My question is different from this linked question because, even though I followed the answers suggested there, it is still not working when using a custom ModelBinder as it's throwing an exception mentioned below.

I've got this routing configured:

config.Routes.MapHttpRoute(
    name: "ApiByAction",
    routeTemplate: "Api/{controller}/{action}",
    defaults: new { action = "Get" },
    constraints: null
);

And on the client side, I do:

public IEnumerable<MyEntity> LoadMany(IEnumerable<int> ids)
{
    var url = "Api/MyEntity/GetMany?ids={0}";
    var s = HttpUtility.UrlEncode(string.Format(url, string.Join(",", ids)));

    response = client.GetAsync(HttpUtility.UrlEncode(s)).Result;

    return null; //for now 
}

However, response is throwing a Not Found exception.

What am I missing here?

EDIT

I removed the registering of the custom model binder, and I changed the action to:

[ActionName("GetMany")] [HttpGet]
public IHttpActionResult GetMany([ModelBinder(typeof(ArrayModelBinder))] IEnumerable<int> ids)
{
    //Do something...
}

And now I get the following exception:

HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (?)

So if ? is not allowed in the Url, and without removing validation, how is it possible to pass the array to the action? Would changing the Get to a Post make sense (in order to use request.body)?

Community
  • 1
  • 1
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • Instead of `"Api/MyEntity/GetMany?ids={0}"` have you tried full url? (`"http:/www.example.com/Api/MyEntity/GetMany?ids={0}"`) – adricadar May 13 '15 at 10:15

1 Answers1

3

If you have the controller action as

public IHttpActionResult GetMany([FromUri] int[] ids)

and pass the URL as:

?ids=1&ids=2&ids=3...

It should just work without a custom binder

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60