【问题标题】:Web Api MVC error : (The requested resource does not support http method 'POST'.)Web Api MVC 错误:(请求的资源不支持 http 方法 'POST'。)
【发布时间】:2019-10-07 04:16:42
【问题描述】:

当我尝试在 Post 方法中添加一些值时,操作员被拒绝并显示消息错误请求的资源不支持 http 方法“POST”。

员工类:

   public class Employee
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public decimal sallary { get; set; }

    public int Age { get; set; }

    public Department Department { get; set; }
}

部门类:

 public class Department
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public ICollection<Employee> Employee { get; set; }
}

员工 Json 的输出:

[
{
    "Id": 1,
    "Name": "ibrahim",
    "sallary": 6200,
    "Age": 20,
    "Department": {
        "Id": 3,
        "Name": "IOS",
        "Employee": []
    }
},
{
    "Id": 2,
    "Name": "ibrahimmmm",
    "sallary": 6200,
    "Age": 20,
    "Department": {
        "Id": 2,
        "Name": "android",
        "Employee": []
    }
}
]

部门Json的输出:

[
{
    "Id": 1,
    "Name": "design",
    "Employee": []
},
{
    "Id": 2,
    "Name": "android",
    "Employee": [
        {
            "Id": 2,
            "Name": "ibrahimmmm",
            "sallary": 6200,
            "Age": 20
        }
    ]
},
{
    "Id": 3,
    "Name": "IOS",
    "Employee": [
        {
            "Id": 1,
            "Name": "ibrahim",
            "sallary": 6200,
            "Age": 20
        }
    ]
}
]

Employee类的方法post:

  public IHttpActionResult Post(Employee employee, int DepartmentId)
    {
        if (ModelState.IsValid)
        {
            var _department = db.Department.Find(DepartmentId);
            employee.Department = _department;
            db.Employee.Add(employee);
            db.SaveChanges();
            return Ok(employee);
        }

        return BadRequest(ModelState);
    }

Employee类的Get方法:

public IEnumerable<Employee> Get()
    {

        return db.Employee.Include(m => m.Department).ToList();

    }

Department class的方法post:

 public IHttpActionResult Post(Department dep) {

        if (ModelState.IsValid)
        {

            db.Department.Add(dep);
            db.SaveChanges();
            return Ok(dep);

        }
        return BadRequest(ModelState);
    }

部门类的获取方法:

public IEnumerable<Department> Get() {

        var a = db.Department.Include(e => e.Employee).ToList();
        return a;
        //return db.Department.Include(item => item.Employee).ToList();

    }

【问题讨论】:

  • POST 的代码在哪里?

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


【解决方案1】:

仅仅因为你调用了Post方法,并不意味着它会接受HTTP POST方法。您需要使用[HttpPost] 属性进行装饰。

GET 是默认值;所以您不必必须装饰 Get 方法 - 但我认为将 [HttpGet] 也放入是很好的风格。

请注意,关于提交的数据,您可能会遇到其他错误;但至少系统会找到一种方法来响应您的请求...

还有一点需要注意 - 将 HTTP 方法放在模型类中是非常不寻常的 - 这就是您拥有控制器的目的。因此,如果 Get/Post 方法真的在 Department/Employee 类中,您甚至可能无法装饰它们

更新:可能最后一段(在模型类中获取/发布,而不是在控制器中)是问题的根本原因!

【讨论】:

  • 不是这样。在Web API,by convention:"你可以用属性指定,否则,否则,如果控制器方法的名称以“Get”、“Post”、“Put”开头、“删除”、“头”、“选项”或“补丁”,那么按照惯例该操作支持该 HTTP 方法。”
  • 虽然这可能是正确的,但好的做法是使用正确的属性,特别是因为文档说 .... 以 .... 开头,为了保存而犯下愚蠢的错误太容易了一个属性的写法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 2012-06-15
相关资源
最近更新 更多