[英]model binding fails on POST
我有一個用作網絡API的ASP.Net Core 2.0項目。 客戶端是使用Angular 4完成的。當我將窗體數據從我的angular服務發布到我的API時,服務器端的模型屬性始終為null。 我不確定我在做什么錯。 我嘗試了使用application/json
和application/x-www-form-urlencoded
各種組合。 我還嘗試了[FromBody]
和[FromForm]
屬性,在兩種情況下,我模型的屬性均為null。
在發貼期間,我在角度上做錯了嗎?
我已驗證發布的json字符串(如屏幕截圖所示)包含我期望的表單數據。 它在服務器上到達null。
這是我的角度服務
import { Injectable, OnInit } from '@angular/core';
import { Http, URLSearchParams, Headers, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/mergeMap';
import { CryptoData } from './cryptodata.interface';
import { Result } from './result.model';
@Injectable()
export class EncryptService implements OnInit {
private options: RequestOptions;
constructor(private http: Http) {
this.options = new RequestOptions({
headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' })
})
}
ngOnInit() {
}
encrypt(data: CryptoData): Promise<Result> {
let json: any = JSON.stringify(data);
return this.http
.post('/api/encrypt', json, this.options)
.toPromise()
.then(response => response.json());
}
decrypt(data: CryptoData) {
}
}
這是我的控制器:
[Produces("application/json")]
[AllowAnonymous]
public class CryptoController : Controller
{
[HttpPost("~/api/Encrypt")]
public IActionResult Encrypt([FromForm] CryptoRequest request)
{
// Testing
return base.Ok(new Result(data: request.Content));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.