FromBody参数始终为null

时间:2018-02-09 16:09:36

标签: angular typescript asp.net-core asp.net-core-2.0 frombodyattribute

在这个论坛上已经有很多关于此的问题。我已经阅读过但我无法解决我的问题,所以我会在这里再试一次。

我正在构建一个Angular + ASP Core应用程序。我已经在其他请求上成功使用了FromBody,但由于某种原因,这个特定的请求无效。

这是我的模特课:

export interface CreateEventModel {

    name: string;
    description: string;
    categoryId: number;
    typeId: number;
    price: number;
    startDate: Date;
    endDate: Date;

    imageContent: Blob;
    imageMimeType: string;
}

以下是我发送到服务器的方式:

 plan() {

   // ...

    this.net.post('Event/Create', this.model).subscribe((res: Response | any) => {

        if (res.status == 200 || res.isOk == true) {
            this.toasterService.pop('success', 'Sucess', "IS NOT NULL!");
        }
    }, err => {  // Show error  });

}

请求我的'.net.post'服务请求,以防你想知道:

post<T>(url: string, data: any): Observable<T> {
    return this.basePut<T>(url, data);
}

// Does header, with JWT
private basePut<T>(url: string, data: any): Observable<T> {
    let options = this.doHeaders();
    return this.doSub(this.http.post(this.doUrl(url), data, options));
}


private doSub<T>(obs: Observable<Response>) {
    var ob1: Observable<T> = obs.map(this.extractData)
        .catch(this.handleError);

    return ob1;
}

继承我的JSON输出:

{
    "imageContent": {},
    "imageMimeType": "image/jpeg",
    "name": "MyTitle",
    "typeId": 3,
    "categoryId": 3,
    "description": "My Description",
    "price": 0,
    "startDate": "2018-08-10T15:55:00.000Z",
    "endDate": "2018-09-26T15:55:00.000Z"
}

这是我的控制器方法:

[HttpPost, Authorize(Roles = "Admin,Planner")]
public IActionResult Create([FromBody]CreateEventModel model)
{
    // Do stuff, but 'model' is always null
}

最后,我的C#模型

 public class CreateEventModel: ICreateEventModel
{

    public string Name { get; set; }
    public string Description { get; set; }
    public int CategoryId { get; set; }
    public int TypeId { get; set; }
    public decimal? Price { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    public byte[] ImageContent { get; set; }
    public string ImageMimeType { get; set; }
}

我不确定会出现什么问题。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题