简体   繁体   English

从 angular2 到 .NET web api 的 HTTP Post 不起作用

[英]HTTP Post from angular2 to .NET web api doesn't work

I'm building a site with an angular2 frontend and .NET backend, the GET calls to the backend have been working flawlessly.我正在构建一个带有 angular2 前端和 .NET 后端的站点,对后端的 GET 调用一直工作得很好。

Now I want to POST some stuff to my server, but I cant seem to get it working.现在我想向我的服务器发布一些东西,但我似乎无法让它工作。

Angular 2 Service Method Angular 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'));
}

Angular 2 DTO Model Angular 2 DTO 模型

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

.NET DTO Model .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 Controller .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));
}

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:方法 Get 在这里调用:

@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>

Observables won't make a request unless you subscribe to them hence you are not making a back-end call.除非您subscribe Observables,否则不会发出请求,因此您不会进行后端调用。

You should subsribe to it like this:你应该像这样订阅它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2 HTTP Post ASP.NET MVC Web API - Angular2 HTTP Post ASP.NET MVC Web API 如何从angular2中的组件http.post方法调用Web api2控制器方法 - how to call to web api2 controller method from component http.post method in angular2 从Angular 2到ASP.net Core的POST请求不起作用。 服务器端的空值 - POST request from Angular 2 to ASP.net Core doesn't work. Null value on server side ASP.net Web API Singleton不起作用 - ASP.net Web API Singleton doesn't work Angular 2 http发布null Web Api Core - Angular 2 http post null Web Api Core 如何在角度$ http.post中传递对象和值,并在ASP .NET Web API中获取它们 - How to pass object and a value in angular $http.post and get those in asp .net web api Ajax POST 调用被 ASP NET Web API 的 CORS 阻止 - 对预检请求的响应未通过访问控制检查 - Ajax POST call blocked by CORS from ASP NET Web API - Response to preflight request doesn't pass access control check Web API控件不能与ASP.NET Web窗体中的POST / PUT / DELETE一起使用 - Web API Controller won't work with POST/PUT/DELETE in ASP.NET Web Forms ASP.NET发布不起作用 - ASP.NET post doesn't work Asp.Net Core 3.1 发布请求不适用于服务器上的 Angular 8 - Asp.Net Core 3.1 post request doesn't work with Angular 8 on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM