【问题标题】:WCF Post Json from angularWCF从角度发布Json
【发布时间】:2018-10-05 09:57:57
【问题描述】:

我正在尝试使用 post 方法将 json 数据从 angular 发送到 wcf。 Angular 以 json 格式发送数据,但 wcf 将其作为空对象接收。可以使用get方法发送json。在按钮单击事件上调用 GetData 方法。

Angular 组件.ts

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {


  constructor(private httpClient: HttpClient){

  }


  getData(){
    this.httpClient.post('http://localhost:3292/examservice.svc/jsontest',{
      RollNumber: '41',
      Name: 'Test'
    })
    .subscribe(
      (data:any) => {
        console.log(data);
      }
    )

  }
}

WCF 服务.svc.cs

namespace Service
{

    public class Service : IService
    {
        public Student jsont(Student s)
        {

            return s;
        }
   }
}

WCF IService.cs

namespace Service
{

    [ServiceContract]
    public interface IService
    {

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "jsontest", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        Student jsont(Student s);
    }


    [DataContract]
    public class Student
    {
        [DataMember]
        public string RollNumber { get; set; }
        [DataMember]
        public string Name { get; set; }
    }
}

【问题讨论】:

    标签: c# json angular wcf


    【解决方案1】:

    删除 BodyStyle = WebMessageBodyStyle.WrappedRequest 然后你就可以走了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 2022-09-28
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多