【发布时间】:2016-06-14 21:56:17
【问题描述】:
我正在尝试使用 Angular 2 中的 HTTP 服务的 http post 请求。但参数不会在 web api 操作中进行。以下是我的组件和服务:
我的服务被注入的组件(LogService)
import {Component, Inject} from '@angular/core';
import {Http, HTTP_BINDINGS} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import {LogService} from '../../services/canvas/logService'
import {PagingModel} from '../../PagingModel';
@Component({
selector: 'about',
templateUrl: 'app/components/about/about.html',
styleUrls: ['app/components/about/about.css'],
bindings: [LogService, HTTP_BINDINGS],
providers: [],
directives: [],
pipes: []
})
export class About {
private abc = "";
constructor( @Inject(LogService) public _logService: LogService) {
this._logService = _logService;
}
ngOnInit() {
this.getAllLogs();
}
getAllLogs() {
var body = { CurrentPageIndex: 1, PageSize: 10,SearchText:"JHFGHY"};
let mod = new PagingModel();
mod.CurrentPageIndex = 1;
mod.PageSize = 10;
mod.SearchText = "sdfs";
this._logService.getAllLogs(mod).subscribe(function (data) {
var abc = data.json();
});
}
}
服务(LogService)从哪里将数据发布到 web api post
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {About} from '../../components/about/about';
require('rxjs/add/operator/toPromise');
@Injectable()
export class LogService {
public headers: Headers;
constructor(private http: Http) {
this.http = http;
}
getAllLogs(model): Observable<Response> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post("http://localhost:64224/api/Logs/Paging",JSON.stringify(model), options);
}
}
我的 web api 控制器操作是:
[HttpPost]
[Route("Paging")]
public ResponseMessage Paging([FromBody] PagingModel model)
{
ResponseMessage responseMessage = new ResponseMessage();
List<ActivityLogModel> activityLogModelList = new List<ActivityLogModel>();
activityLogModelList = logService.Paging(model);
responseMessage.totalRecords = activityLogModelList.Count();
activityLogModelList = activityLogModelList.OrderBy(x => x.UserId).Skip((model.CurrentPageIndex - 1) * model.PageSize).Take(model.PageSize).ToList();
responseMessage.data = activityLogModelList;
return responseMessage;
}
#endregion
我无法在此 Web Api 控制器中获取从 LogService http post 方法发送的参数。请帮帮我。提前谢谢你
【问题讨论】:
-
您能否详细介绍一下发送的请求的内容(来自开发工具)?谢谢!
标签: c# asp.net-web-api angular