Angular 角四柱法

Angular 角四柱法,angular,asp.net-web-api,angular-ui-router,angular2-services,angular4-httpclient,Angular,Asp.net Web Api,Angular Ui Router,Angular2 Services,Angular4 Httpclient,我是angular 4 web api的新手。我想使用angular 4 web api将一些数据保存到数据库中。当我从angular调用GET方法时,它可以正常工作,但不能在POST方法上工作。我得到415个不支持的媒体类型。一直以来,但当我使用postman时,post方法工作正常,因为我将内容类型更改为application/json。但从角度看,它不起作用。。。我看到很多问题都有相同的问题,但它们对我不起作用。这里我添加了一些硬编码数据 这是我的组件ts文件: fulldetail

我是angular 4 web api的新手。我想使用angular 4 web api将一些数据保存到数据库中。当我从angular调用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())

}
这是我的控制器:

   [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; }

    }
}
[HttpPost]
公共无效详细信息([FromBody]列表jsonvalues)
{
foreach(jsonvalues中的spGetDetails\U ResultDatadetail)
{
spGetDetails_ResultDetailobject=新的spGetDetails_结果();
Detailobject.id=Datadetail.id;
Detailobject.name=Datadetail.name;
Detailobject.address=Datadetail.address;
Detailobject.about=Datadetail.about;
Detailobject.number=Datadetail.number;
Detailobject.count=Datadetail.count;
enqenties.spGetDetails_结果(Datadetail.id、Datadetail.name、Datadetail.address、Datadetail.about、Datadetail.number、Datadetail.count);
}
}
公共类spGetDetails
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
关于{get;set;}的公共字符串
公共整数{get;set;}
公共整数计数{get;set;}
}
}
zone.js:2933 POST 415(不支持的媒体类型)。 身体 : “{”消息“:“此资源不支持请求实体的媒体类型“text/plain”。,“ExceptionMessage”:“没有MediaTypeFormatter可用于从媒体类型为“text/plain”的内容中读取“List`1”类型的对象……”


当我使用postman时,post方法可以正常工作,因为我将内容类型更改为application/json。但在angular中,它不起作用。尝试正确设置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();
        // ...
    }

首先,您不发送JSON:

let data= JSON.stringify([{ id: id, name: name, address: address, about: about, nu: number, count: count }]);
名称非常明确:您正在从对象创建字符串

其次,您不使用您创建的标题。添加以下内容:

return this._http.post('http://localhost:1234/api/contrllername/' + 'detail', data,
  {
    headers: headers,
    params: [{ id: id, name: name, address: address, about: about, nu: number, count: count }]
  }).map((response: Response) => <Idetails[]>response.json())
返回此信息。_http.post('http://localhost:1234/api/contrllername/“+”细节“,数据,
{
标题:标题,
参数:[{id:id,name:name,address:address,about:about,nu:number,count:count}]
}).map((response:response)=>response.json())

您将标题定义为变量,但从未将其传递给您的请求…@Fussel如何在其中传递请求???@trichetriche的awnser显示了如何将标题添加到您的请求中request@Fussel事实上,我已经在这个解决方案中呆了一个星期了。请帮忙me@Fussel我尝试了trichetriche先生的笔记,但没有得到相同错误的答案在邮递员那里得到api,所以我认为比api好?你的意见是什么?
return this._http.post('http://localhost:1234/api/contrllername/' + 'detail', data,
  {
    headers: headers,
    params: [{ id: id, name: name, address: address, about: about, nu: number, count: count }]
  }).map((response: Response) => <Idetails[]>response.json())