简体   繁体   中英

Asp.Net Web Api - Passing String[] values from swagger to Asp.Net Web API

I am calling an external API from my Asp.Net Web Api.. I am testing it using swagger and from swagger i am passing array of DepartmentCodes as ["IT","HR"] , when i am testing it using swagger the API is failing because of the DepartmenCodes array.

I am posting only partial code here as the issue is only with the string[] values being passed from swagger.

public async Task<IHttpActionResult> GetUsers([FromUri] GetUserRequest getUserRequest)
{
   UserRequest userRequest = new UserRequest();
   userRequest.DepartmentCodes = getUserRequest.DepartmentCodes;
   using (var Client = new HttpClient())
   {
     //code to call external api
   }
}

Public Class UserRequest
{
  Public string[] DepartmentCodes {get;set;}
}

Public Class GetUserRequest
{
  Public string[] DepartmentCodes {get;set;}
}

When i am hardcoding the DepartmentCodes array in the code, the API is working as expected and i am getting the result.

public async Task<IHttpActionResult> GetUsers([FromUri] GetUserRequest getUserRequest)
{
   UserRequest userRequest = new UserRequest();
   userRequest.DepartmentCodes = new string[] {"IT", "HR"};
   //userRequest.DepartmentCodes = getUserRequest.DepartmentCodes;
   using (var Client = new HttpClient())
   {
     //code to call external api
   }
}

In order for the API to work with swagger, do we have to make any change in the code or do i need to change the way i am passing string array from swagger.. currently from swagger i am passing the string array as ["IT, HR"]

I figured it out, It worked when i tried posting the string array values from swagger in the below way

IT
HR

There shouldn't be any double quotes, commas or any other special characters...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM