使用参数列表调用WebApi操作

时间:2015-09-08 07:20:39

标签: c# asp.net-mvc-4 asp.net-web-api

Web API代码

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

我需要能够调用此web api传递一个整数数组

WebApiconfig的路线如下

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 }
                );

我尝试使用URL

访问该方法
  1. http://localhost/testApplication/api/questionnaire/GetMore/dd=1 调用方法,但dd数组列表为空,dd数组中没有数据。
  2. http://localhost/testApplication/api/questionnaire/GetMore/?dd=1&dd=2&dd=3我收到错误
  3.   

    {“消息”:“请求无效。”,“MessageDetail”:“参数   字典包含非可空参数'id'的空条目   输入'System.Int32'作为方法   'WebAPI.Services.Controllers.Questionnaire获取(Int32)'   'WebAPI.Services.Controllers.QuestionnaireController'。可选的   参数必须是引用类型,可空类型或声明为   一个可选参数。“}

    它们似乎都不起作用。我确信我做错了什么。只是不确定那是什么?

0 个答案:

没有答案
相关问题