2017-01-16 4 views
2

私はangle2フロントエンドと.NETバックエンドを持つサイトを構築しています.GETコールバックエンドは完璧に動作しています。HTTP2からAngle2から.NET Web APIへのポストが動作しません

今私はサーバーにいくつかのものをPOSTしたいと思っていますが、動作させることはできません。

角度2サービスメソッド

postCategory(category: Category){ 
    let endpoint = this.url + 'add'; 
    let body = JSON.stringify(category); 
    let options = new RequestOptions({headers: this.headers}); 
    return this.http.post(endpoint, body, options) 
     .map((res:Response) => res.json()) 
     .catch((error:any) => Observable.throw(error.json().error || 'Server Error')); 
} 

角度2 DTOモデル

export class Category{ 
    constructor(
     public CategoryName: string, 
     public ParentId: number, 
     public ChildCategories: Category[] 
    ){} 
} 

.NET DTOモデル

public class CategoryDTO 
{ 
    public string CategoryName { get; set; } 
    public int? ParentId { get; set; } 
    public IList<CategoryDTO> ChildCategories { get; set; } 

    public CategoryDTO(string name, int? parent, IList<CategoryDTO> child) 
    { 
     CategoryName = name; 
     ParentId = parent; 
     ChildCategories = child; 
    } 
} 

.NET WEB APIのコントローラ

[HttpPost] 
[Route("add")] 
public IHttpActionResult PostCategory([FromBody]CategoryDTO category) 
{ 
    var newCategory = _categoryService.AddCategory(_dtoBuilder.CategoryDtoToCategory(category)); 
    if(newCategory == null) return NotFound(); 
    return Ok(_dtoBuilder.CategoryToDto(newCategory)); 
} 

エンドポイントが対応し、モデルが対応します。

コールが行われているが、コントローラの開始時にブレークポイントを設定して、それが入るかどうかを確認する。何か不足していますか?

ありがとうございます!

編集:ここ

メソッドの取得calles:

@Component({ 
    moduleId: module.id, 
    selector: 'admin-category', 
    templateUrl: 'admin-category.component.html', 
    styleUrls: ['admin-category.component.css'] 
}) 
export class AdminCategoryComponent{ 
    name: string; 
    parent: number; 

    constructor(
     private categoryService: CategoryService 
    ){} 

    addCategory(): void{ 
     this.categoryService.postCategory(new Category(this.name, this.parent, null)); 
    } 
} 

そのコンポーネントのテンプレート:彼らにしない限り、あなたsubscribe

<h1>Add Category</h1> 
<div class="form-group"> 
    <label for="name">Name:</label> 
    <input type="text" class="form-control" id="name" [(ngModel)]="name"> 
</div> 
<div class="form-group"> 
    <label for="parent">ParentId:</label> 
    <input type="text" class="form-control" id="parent" [(ngModel)]="parent"> 
</div> 
<button class="btn btn-default" (click)="addCategory()">Submit</button> 
+0

? – echonax

+0

postCategory getは、自分のコンポーネントの1つから呼び出されます。既にテストされてPOST呼び出しが行われています。 – TanguyB

+0

あなたの質問にあなたがそこに潜んでいるコードを共有してください。どのようにテストしましたか?応答も含めることができますか? – echonax

答えて

2

観測はリクエストをすることはありませんので、あなたバックエンドコールを行っていません。

あなたはこのようにそれにsubsribe必要があります:あなたはpostCategory` `に加入ん

this.categoryService.postCategory(new Category(this.name, this.parent, null)).subscribe((response)=>{ 
    console.log(response); 
}); 
+0

私は今それをテストします。 – TanguyB

+0

ありがとう、あなたは上司です! – TanguyB

+0

@TanguyB haha​​、私は助けることができてうれしい。 – echonax

関連する問題