FromBody为空

时间:2017-02-16 04:16:23

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

我正在为api做一个帖子,无论出于什么原因我一直得到一个空值我在Angular2中调用api服务,在[FromBody]中并不确定原因。我有以下

成分<​​/ P>

JSONObject request = new JSONObject();
request.put("a","1");
request.put("v","33");

JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST, "<URL>", request,
        new Response.Listener<JSONObject>() {

             @Override

             public void onResponse(JSONObject response) {
             },
        new Response.ErrorListener() {

             @Override
             public void onErrorResponse(VolleyError error) {
             }
        });

服务

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);

在我的小提琴手中,我看到了原始数据 enter image description here

的WebAPI

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);
}

***********还被审讯********************************** ******

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);
    }
}

杰森 enter image description here

得到了同样的错误

FileModel

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);
}

1 个答案:

答案 0 :(得分:0)

知道了,只需要将“结果”添加到我的杰森

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);
}