您好我们目前正在使用c#MVC Core 2和Angular 5,我们正在努力理解为什么我们的端点没有在动作处理程序中绑定信息。
这是我们的拦截器,它设置了有关请求的所有必要信息。
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this._cookieService.check('Abp.AuthToken')) {
// We have our auth token..
let bearerToken = this._cookieService.get('Abp.AuthToken');
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', 'bearer ' + bearerToken).set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')});
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
} else {
// @todo This should sign the user out of the application...
console.log('here');
}
}
请注意我们还尝试过&application / json&#39;也没有成功。
以下是我们使用角度HttpClient
:
this.http.post('http://localhost:22742/api/services/app/UserGridSetting/SaveUserGridSettings', {'jsonData': JSON.stringify(gridSettingsObject)}).subscribe((d: any) => {
console.log(d);
});
这是我们的C#对应端点:
public async Task<int> SaveUserGridSettings(string jsonData)
{
return await _repository.InsertOrUpdateAndGetIdAsync(setting);
}
正确地调用和运行Endpoint但是我们的jsonData从未绑定到方法中,我们尝试使用Angular客户端进行了大量不同的操作,但信息从未被绑定。
答案 0 :(得分:0)
我会尝试做三件事:
使用内容类型&#34; application / json&#34;不是&#34; JSON /应用&#34;
直接发布字符串
this.http.post('http://localhost:22742/api/services/app/UserGridSetting/SaveUserGridSettings', JSON.stringify(gridSettingsObject)) //...
public async Task<int> SaveUserGridSettings([FromBody]string jsonData)
答案 1 :(得分:0)
我们在几个小时后找到了答案,预期的字符串类型无法绑定jsonData
中的HttpClient
标识符,因为它正在检查基元上的data
属性输入字符串。
只需添加一个类并将签名更改为:
public async Task<int> SaveUserGridSettings(ColumnState jsonData)
ColumnState
为:
public class ColumnState
{
public string data { get; set; }
}
并使用角度像:
this.http.post('http://localhost:22742/api/services/app/UserGridSetting/SaveUserGridSettings', {data: JSON.stringify(gridSettingsObject)}).subscribe((d: any) => {
console.log(d);
});