【问题标题】:FromBody is nullFromBody 为空
【发布时间】:2017-02-16 04:16:23
【问题描述】:

我正在向 api 发布帖子,无论出于何种原因,我一直在使用 Angular2 在 [FromBody] 中调用 api 服务,但不知道为什么。我有以下

组件

fileInfo: IFile;//this is an interface
this.fileInfo = {
    FileID:1000,
    LinkTabID: 1,
    FileName: 'Test',
    FileDisplayName: 'test',
    IsActive: true,
    CreatedBy: 'mike',
    UpdatedBy:'mike'
}

this._tabService.AddFile(this.fileInfo)
    .subscribe(
        data => {
            this.tabControls = data;
        },
        error => this.errorMessage = <any>error);

服务

AddFile(fileInfo: IFile){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this._fileUploadAPI + 'InsertFile' , { fileInfo }, options)
        .map((response: Response) => response.json())
        .catch(this.handleError);
}

在我的提琴手中,我看到了原始数据

WebAPI

public HttpResponseMessage InsertFile([FromBody] FileModel value)
{
    try
    {
      //  sp_GetFileResult fileEntry = _modelFactory.Parse(value);

        if (fileEntry == null)
            return Request.CreateResponse(HttpStatusCode.BadRequest, "Could not read file entry in the body");
        else
        {
            some logic
        }
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
}

***********也尝试过************************************ ******

AddFile(fileInfo: IFile){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this._fileUploadAPI + 'InsertFile', { value: fileInfo }, options)
        .map((response: Response) => response.json())
        .catch(this.handleError);
}

杰森

遇到同样的错误

文件模型

public class FileModel
    {

     public int FileID { get; set; }
     public int  LinkTabID { get; set; }
     public string  FileName { get; set; }
     public string FileDisplayName { get; set; }
     public bool  IsActive { get; set; }
     public  string CreatedBy { get; set; }
     public  string UpdatedBy { get; set; }

    }

【问题讨论】:

  • FileModel的结构是什么?
  • 我怀疑“value”和“fileInfo”的嵌套内部对象突出显示它只是一个单一的接口,尝试删除那些内部对象并直接发布属性。
  • 删除了内部对象,仍然得到相同的结果
  • 添加了我的文件模型

标签: angular asp.net-web-api asp.net-web-api2


【解决方案1】:

知道了,只需要在我的 jason 中添加“结果”

AddFile(fileInfo: IFile){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this._fileUploadAPI + 'InsertFile', { result: fileInfo }, options)
        .map((response: Response) => response.json())
        .catch(this.handleError);
}

而不是

AddFile(fileInfo: IFile){
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this._fileUploadAPI + 'InsertFile', { value: fileInfo }, options)
        .map((response: Response) => response.json())
        .catch(this.handleError);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2015-11-05
    相关资源
    最近更新 更多