【发布时间】: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