0

Web API code

[HttpGet]
public string GetMore([FromUri] Int32[] dd)
{
   return "";
}

I need to be able to call this web api passing in an array of integers

The route at the WebApiconfig is as follows

config.EnableCors();

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
config.Routes.MapHttpRoute(
                name: "ActionApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );

I try to access the method with the URL

  1. http://localhost/testApplication/api/questionnaire/GetMore/dd=1 Calls the method but the dd array list is empty that is no data is present in the dd array.
  2. http://localhost/testApplication/api/questionnaire/GetMore/?dd=1&dd=2&dd=3 i get an error

{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'WebAPI.Services.Controllers.Questionnaire Get(Int32)' in 'WebAPI.Services.Controllers.QuestionnaireController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}

None of them seem to work . I am sure im doing something wrong. Just not sure what that is ?

user581157
  • 1,327
  • 4
  • 26
  • 64
  • Have you tried passing parameters as `http://localhost/testApplication/api/questionnaire/GetMore/dd[]=1 `? – kamil-mrzyglod Sep 08 '15 at 07:28
  • 2
    Your question [was answered before on SO](http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-a-asp-net-web-api-rest-service). – Michael Sep 08 '15 at 07:28
  • http://localhost/testApplication/api/questionnaire/GetMore?dd=1&dd=2&dd=3 <= use this – Arghya C Sep 08 '15 at 07:34
  • I have test these code without the code before `config.Routes.MapHttpRoute`,and it works fine with the url end with `GetMore/?dd=1&dd=2&dd=3`,so there must be something happened in prev code – Sky Fang Sep 08 '15 at 07:38
  • I try the solution mentioned at http://stackoverflow.com/a/11100414/581157 i get the error {"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'WebAPI.Services.Controllers.Questionnaire Get(Int32)' in 'WebAPI.Services.Controllers.QuestionnaireController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."} Also the post does not contain an Answer Accepted – user581157 Sep 08 '15 at 08:17

0 Answers0