2

I'm building a site with an angular2 frontend and .NET backend, the GET calls to the backend have been working flawlessly.

Now I want to POST some stuff to my server, but I cant seem to get it working.

Angular 2 Service Method

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'));
}

Angular 2 DTO Model

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

.NET DTO Model

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 Controller

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

The endpoints corresponds, the models correspond.

The call is being made, I put a break point on the start of my controller to see if it gets in but i doesn't. Am I missing something?

Thanks!

EDIT:

Method Get's calles here:

@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));
    }
}

Template of that component:

<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>
11
  • Where do you subscribe to postCategory? Commented Jan 16, 2017 at 10:15
  • postCategory get's called from one of my components. Already tested the POST call is being made. Commented Jan 16, 2017 at 10:16
  • Please share the code where you subsribe to it in your question. How did you test it? Can you also include the response? Commented Jan 16, 2017 at 10:17
  • 1
    You are not answering my questions :) and your question doesn't give me the info that "the actual POST call is being made without a problem. The backend just "ignores" it." Commented Jan 16, 2017 at 10:23
  • 1
    @TanguyB can you please share the code where you call this method? Commented Jan 16, 2017 at 10:29

1 Answer 1

2

Observables won't make a request unless you subscribe to them hence you are not making a back-end call.

You should subsribe to it like this:

this.categoryService.postCategory(new Category(this.name, this.parent, null)).subscribe((response)=>{
  console.log(response);
});
Sign up to request clarification or add additional context in comments.

3 Comments

I will test it now.
Thanks you're the boss!
@TanguyB haha, glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.