首页 > 解决方案 > 角度4后方法

问题描述

我是 angular 4 web api 的新手。我想使用 angular 4 web api 将一些数据保存到数据库中。当我从角度调用 GET 方法时,它工作正常,但不适用于 POST 方法。我收到 415 不支持的媒体类型。一直以来,但是当我使用 postman 时,post 方法工作正常,因为我将内容类型更改为 application/json。但是在角度上它不起作用......我看到很多相同问题的问题,但它们对我不起作用。在这里,我添加了一些硬编码数据。

这是我的组件 ts 文件:

   fulldetail: Idetails[] =[];
detail: Idetails;
  onClick(value:any) {


       \\hardcoded data
        this.id = 1;
       this.name= abcd;
        this.address= "abcdefgh";
        this.about= "samplestring";
        this.number= 18888;
        this.count = 12.0;

  this._Service.CatchDetail(this.id, this.name, this.address, 
this.about, this.number, this.count)
           .subscribe(detail=> {
                detail.forEach(value => {
                    this.fulldetails.push(value)
           });
        },
            error => {
                console.error(error);
                this.statusMessage = "Problem with the service.Please try again after sometime";

        });

这是我的服务 .ts 代码:

CatchDetail(id: number, name: string, address: string, 
about: string, number: number, count: number)
    : Observable<Idetails[]> {
      let data= JSON.stringify([{ id: id, name: name, address: address, about: about, nu: number, count: count }]);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this._http.post('http://localhost:1234/api/contrllername/' + 'detail', data,
        {
            params: [{ id: id, name: name, address: address, about: about, nu: number, count: count }]
        })
        .map((response: Response) => <Idetails[]>response.json())

}

这是 workdetails.ts (类):

export class Idetails {
constructor(
    public id: number,
    public name: string,
    public address: string,
    public about: string,
    public number: number,
    public count: number
  ) { }
}

这是我的控制器:

   [HttpPost]
    public void Detail([FromBody] List<spGetDetails_Result> jsonvalues)

    {


        foreach (spGetDetails_ResultDatadetail in jsonvalues)
        {
            spGetDetails_ResultDetailobject = new spGetDetails_Result();

            Detailobject.id = Datadetail.id;
            Detailobject.name= Datadetail.name;
            Detailobject.address= Datadetail.address;
            Detailobject.about= Datadetail.about;
            Detailobject.number= Datadetail.number;
            Detailobject.count = Datadetail.count;

            enqentities.spGetDetails_Result(Datadetail.id, Datadetail.name, Datadetail.address, Datadetail.about, Datadetail.number, Datadetail.count);
        }

    }


    public class spGetDetails
    {

        public int id { get; set; }
        public string name{ get; set; }
        public string address{ get; set; }
        public string about{ get; set; }
        public int number{ get; set; }
        public int count { get; set; }

    }
}

zone.js:2933 POST http://localhost:1234/api/controllername/Detail?id=1&name=abc&address=hj1hgj368&about=dfghjkl&number=31&count=3 415(不支持的媒体类型)。body : "{"Message":"该资源不支持请求实体的媒体类型 'text/plain'。","ExceptionMessage":"没有 MediaTypeFormatter 可用于从内容中读取类型为 'List`1' 的对象媒体类型为“文本/纯文本”............等

当我使用邮递员时,post 方法工作正常,因为我将内容类型更改为 application/json。但是在角度上它不起作用。

标签: angularasp.net-web-apiangular-ui-routerangular2-servicesangular4-httpclient

解决方案


尝试正确设置 WebAPI 以读取~/AppStart/WebApiConfig.cs中的 JSON 请求

    public static void Register(HttpConfiguration config)
    {
        // ...
        var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver =
            new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
        // ...
    }

推荐阅读