【问题标题】:Post method returns 404 in Asp.net core Web API ControllerPost 方法在 Asp.net 核心 Web API 控制器中返回 404
【发布时间】:2017-12-31 07:51:34
【问题描述】:

以下路线代码:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

控制器代码如下:

// POST api/values
        [HttpPost]
        public void Post([FromBody]Employee employee)
        {
            employeeManager.CreateAsync(employee);
        }

除了 post 方法之外的所有其他方法。

从角度组件调用:

 onSubmit(employeeItems: any) {        
        console.log(employeeItems);
        this.getData();
        var headers = new Headers();
        headers.append('Content-Type', 'application/json; charset=utf-8');
        this.http.post('api/Employee/Post', employeeItems, { headers: headers }).subscribe();
        this.createEmployeeFlag = false;
    }

我什至尝试过 Postman,但没有运气。

【问题讨论】:

标签: asp.net-mvc angular asp.net-web-api asp.net-core


【解决方案1】:

您的 url 和路由模板不匹配

[Route("api/[controller]")]
public class EmployeeController : Controller {

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]Employee employee) {
        await employeeManager.CreateAsync(employee);
        return Ok();
    }
}

并更新您的调用 URL 以调用默认端点 api/Employee

onSubmit(employeeItems: any) {        
    console.log(employeeItems);
    this.getData();
    var headers = new Headers();
    headers.append('Content-Type', 'application/json; charset=utf-8');
    this.http.post('api/Employee', employeeItems, { headers: headers }).subscribe();
    this.createEmployeeFlag = false;
}

【讨论】:

  • 感谢“并更新您的调用 URL 以调用默认端点 api/Employee”这是我的问题!干杯
【解决方案2】:

这是您在服务中需要的代码,这里有两个问题,首先是 URL,它需要是完整的 URL 路径。第二个是您在将其映射到 Observable 之前尝试订阅某些内容

onSubmit(employeeItems: any) {
    let url: string = 'http://localhost/api/employee'; //this will be the complete url that you would hit with say postman  
    this.getData(); //I'm not sure what this is so I'm leaving it here
    this.http.post(url, employeeItems)
      .map((response: Response) => response.json())
      .Subscribe((response: any) => {
        //do whatever with the response here.
      });
    this.createEmployeeFlag = false;
}

我建议将其分解为 *.service.ts 文件。

*.service.ts

public postEmployee(employeeItems: any): Observable<any> {
  let url: string = 'http://localhost/api/employee'; //this will be the complete url that you would hit with say postman  
  this.http.post(url, employeeItems)
   .map((response: Response) => response.json());
}

在你的*.component.ts里面

构造函数(私人服务:服务){}

onSubmit(employeeItems: any) {
  this.getData(); //I'm not sure what this is so I'm leaving it here
  this.service.postEmployee(employeeItems)
    .Subscribe((response: any) => {
      //do whatever with the response here.
    });
  this.createEmployeeFlag = false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2017-07-29
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多