asp.net - 从 angular2 到 .NET Web api 的 HTTP Post 不起作用

标签 asp.net http angular post asp.net-web-api

我正在构建一个具有 angular2 前端和 .NET 后端的网站,对后端的 GET 调用一直运行得很好。

现在我想将一些内容发布到我的服务器,但我似乎无法让它工作。

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 模型

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

端点对应,模型对应。

正在调用电话,我在 Controller 的开头放置了一个断点以查看它是否进入,但我没有。我错过了什么吗?

谢谢!

编辑:

方法 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));
    }
}

该组件的模板:

<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 不会发出请求,因此您不会进行后端调用。

您应该像这样订阅它:

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

关于asp.net - 从 angular2 到 .NET Web api 的 HTTP Post 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673969/

相关文章:

javascript - 单击确认对话框按钮时如何返回 true 或 false?

c# - 在asp.net C#中动态捕获特定url的图像

asp.net - 在asp.net页面生命周期中,session是什么时候设置的?

javascript - ng-serve 命令打开编辑器

Angular 2 根据 POST 请求重新加载页面

即使#threads, requests/sec 很低,asp.net 请求也会排队

linux - 通过 http 的 svn 凭证 [Linux]

c# - HTTP 可以用来跟踪连接吗?

http - 发出 http 请求时打开的文件太多

Angular 8 ng-build 使用 cordova 抛出 MIME 错误