【问题标题】:HTTP POST from Angular2 to WebApi从 Angular2 到 WebApi 的 HTTP POST
【发布时间】: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


【解决方案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
  });
}

【讨论】:

  • 他在 C# Webservice 部分谈论他的控制器。
  • 完全正确。请帮助我将参数发送到 web api 操作。无论如何,谢谢 Günter Zöchbauer
  • 抱歉,这里帮不上忙。如果你修正了我的建议,我可以删除我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 2017-10-23
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
相关资源
最近更新 更多