2016-06-14 63 views
1

我想在角度2中使用HTTP服务的http post请求。但参数不会在web api动作上进行。下面是我的组件和服务:
我的部件,其中服务注入(LogService)从Angular2到WebApi的HTTP POST

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


    } 
} 

服务从那里数据被发布到网页API后

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

    } 


} 

(LogService)和我的网页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方法获取参数。请帮我在这里。谢谢你在前进

+0

您能告诉更多的关于发送的请求(从开发工具)的内容?谢谢! –

回答

1

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(data => { // <<<=== use arrow function instead of `function` 
    this.abc = data.json(); // <<<=== this.abc 
    }); 
} 
+0

他正在谈论他的控制器在C#Webservice部分。准确地说是 – rinukkusu

+0

。请帮助我将参数发送到web api操作。非常感谢GünterZöchbauer –

+0

对不起,不能帮助这里。如果您修正了我的建议,我可以删除我的答案。 –