2017-10-17 59 views
1

的API(C#编写的,ASP.NET核心)接收3个字段:标题,内容和类别的阵列(整数):发送阵列到API

[HttpGet] 
[AllowAnonymous] 
public IActionResult CreatePublication(string title, string content, IEnumerable<int> categoryIds) 
{ 
    return Ok(); 
} 

问题是API总是接收categoryIds数组中的零个元素。

在Angular4客户端:

let ccategoryIds = new Array<number>() 
ccategoryIds.push(2) 
ccategoryIds.push(5) 
ccategoryIds.push(7) 

let requestOptions = { 
    params: new HttpParams() 
    .set('title', 'The title') 
    .append('content', 'The content') 
    .append('categoryIds', ccategoryIds.toString()), 
    withCredentials: true 
} 

this.http 
    .get('http://localhost:53203/api/publications/createPublication', requestOptions) 
    .subscribe(data => { 
    }, 
    (err: HttpErrorResponse) => { 
    alert('Error') 
    }) 

更新

如果使用JSON.stringify(ccategoryIds)的代替ccategoryIds.toString(),收到在零个元素API也是。

enter image description here

这不是后端的问题。在邮差它的工作原理: enter image description here enter image description here

回答

1

很明显,我不能看你怎么写API处理器,但我瘦你正在使用.toString(),你应该使用JSON.stringify()

let ccategoryIds = new Array<number>() 
ccategoryIds.push(2) 
ccategoryIds.push(5) 
ccategoryIds.push(7) 

let requestOptions = { 
    params: new HttpParams() 
    .set('title', 'The title') 
    .append('content', 'The content') 
    .append('categoryIds', JSON.stringify(ccategoryId)), 
    withCredentials: true 
} 

this.http 
    .get('http://localhost:53203/api/publications/createPublication', requestOptions) 
    .subscribe(data => { 
    }, 
    (err: HttpErrorResponse) => { 
    alert('Error') 
    }) 
+0

@JohnDoe这看起来像是C#端的一个问题。我们能在那里看到你的代码吗? –