代码之家  ›  专栏  ›  技术社区  ›  Vigneshwaran Markandan

Post方法在Asp中返回404。net core Web API控制器

  •  4
  • Vigneshwaran Markandan  · 技术社区  · 9 年前

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

    2 回复  |  直到 9 年前
        1
  •  4
  •   Nkosi    9 年前

    您的url和路由模板不匹配

    [Route("api/[controller]")]
    public class EmployeeController : Controller {
    
        [HttpPost]
        public async Task<IActionResult> Post([FromBody]Employee employee) {
            await employeeManager.CreateAsync(employee);
            return Ok();
        }
    }
    

    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;
    }
    
        2
  •  1
  •   Wesley Coetzee    9 年前

    这是您在服务中需要的代码,这里有两个问题,第一个是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 文件

    *.服务.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;
    }