我在ASP.Net Web API Controller中有一个方法如下 -
public string addNewTask([FromBody]TaskListModels _newTask)
{
DataTable _newTaskDataTable = new DataTable();
try
{
SQLHelper sqlHelper = new SQLHelper(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString());
sqlHelper.Parameters.AddWithValue("@taskId", _newTask.taskId);
//sqlHelper.Parameters.AddWithValue("@userId", userId);
sqlHelper.Parameters.AddWithValue("@taskDetails", _newTask.taskDetails);
sqlHelper.Parameters.AddWithValue("@companyId", _newTask.companyId);
sqlHelper.Parameters.AddWithValue("@dueDate", _newTask.dueDate);
_newTaskDataTable = sqlHelper.ReturnDataTableFromStoredProcedure("[dbo].[MYSP]");
}
catch (SqlException ex)
{
Console.WriteLine("SQL ERROR: " + ex.Message);
}
catch (Exception e)
{
Console.WriteLine("ERROR: " + e.Message);
}
return "String";
}
其中TaskListModels是一个具有如下属性的类。
public class TaskListModels
{
public int taskId { get; set; }
public string userId { get; set; }
public string taskDetails { get; set; }
public int companyId { get; set; }
public string companyName { get; set; }
public string dueDate { get; set; }
}
我试图从Angular服务发布一些数据,如下所示 -
public addTask(_newTask: any) {
console.log('Service - TaskListService : Method - addTask : Params - ' + JSON.stringify(_newTask))
this._requestOptions = new RequestOptions({
method: RequestMethod.Post,
url: this.addNewtaskApiUrl,
headers: this.headers,
body: JSON.stringify(_newTask)
});
console.log('ADD New - POST Request Query : ' + JSON.stringify(this._requestOptions));
return this._http.request(new Request(this._requestOptions)).map(this._httpExtractDataService.extractData).catch(this._httpErrorHandlerService.handleError);
}
数据显示在浏览器控制台中,如下所示,但它没有达到我的控制器方法 -
POST Request Query : {"method":1,"headers":{"Content-Type":["application/json"]},"body":"{\"taskId\":4,\"taskDetails\":\"Working with Avi for accounting platform project setup\",\"companyId\":4,\"dueDate\":\"2017-05-16\"}","url":"http://localhost:56250/api/Dashboard/addNewTask","withCredentials":null,"responseType":null}
任何帮助都将不胜感激。
由于
答案 0 :(得分:1)
默认情况下,Observable是冷的。您需要subscribe
向观察者实际发出请求:
this._taskListService.addTask(this.newTask).subscribe((res)=>{
console.log(res);
});