angular2和asp.net核心post方法发送空值

时间:2016-11-04 14:01:45

标签: angular asp.net-web-api http-post asp.net-core-middleware

我刚开始使用asp.net核心asngular2。我已经在asp.net core和angular2服务中提供服务。现在angularjs2 GET方法工作正常,但POST方法将null对象发送到web api。

webapi post方法代码

  // POST api/values
            [HttpPost]
            public JsonResult Post(Hero std)
            {
                //_context.Heros.Add(std);
                return new JsonResult(_context.Heros.Add(std),DefaultJsonSettings);

            }



//angularjs2 service methods
     getHeroes(): Observable<Hero[]> {
            return this.http.get(this.heroesUrl)
                .map(res => res.json())
                .catch(this.handleError);
           
           
        }
        addHero(hero: Hero): Observable<Hero> {
         
            let headers = new Headers({ 'Content-Type': 'application/json' });
            var options = new RequestOptions({
                method: RequestMethod.Post,
                headers: headers,
                url: this.heroesUrl,
                body:hero
            });
    //hero.ts
    export class Hero {
        public id: number;
        public name: string;
        constructor() { }
        //constructor(id: number, Name: string) { }
        
    }
    //Hero.cs
     [JsonObject(MemberSerialization.OptOut)]
        public class Hero
        {
            [Key]
            public int id { get; set; }
            public string name { get; set; }
        }
        public  class HeroVM
        {
            public string name { get; set; }
        }

0 个答案:

没有答案