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 ?

5
  • Have you tried passing parameters as http://localhost/testApplication/api/questionnaire/GetMore/dd[]=1 ? Commented Sep 8, 2015 at 7:28
  • 2
    Your question was answered before on SO. Commented Sep 8, 2015 at 7:28
  • localhost/testApplication/api/questionnaire/… <= use this Commented Sep 8, 2015 at 7: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 Commented Sep 8, 2015 at 7:38
  • I try the solution mentioned at 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 Commented Sep 8, 2015 at 8:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.