ahmed ali
ahmed ali

Reputation: 7

Web Api MVC error : (The requested resource does not support http method 'POST'.)

when i try add some value at Post method the operator was rejected with message error The requested resource does not support http method 'POST' .

Employee Class :

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

Department Class :

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

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

Output Of Employee 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": []
    }
}
]

Output Of Department 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
        }
    ]
}
]

Method post Of Employee class :

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

Method Get Of Employee class :

public IEnumerable<Employee> Get()
    {

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

    }

Method post Of Department class :

 public IHttpActionResult Post(Department dep) {

        if (ModelState.IsValid)
        {

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

        }
        return BadRequest(ModelState);
    }

Method Get Of Department class :

public IEnumerable<Department> Get() {

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

    }

Upvotes: 0

Views: 487

Answers (1)

Felix
Felix

Reputation: 10078

Just because you call the method Post, it doesn't mean that it will accept HTTP POST method. You need to decorate with [HttpPost] attribute.

GET is the default; so you don't have to decorate Get methods - but I consider it to be good style to put [HttpGet] as well.

Note, that you will probably get other errors, regarding submitted data; but at least the system will find a method that will respond to your request...

One more note - it is very unusual to put HTTP methods in model classes - that's what you have controllers for. So you may not even be able decorate Get/Post methods, if they are really in Department/Employee classes

UPDATE: Probably the last paragraph (Get/Post in Model class, rather than in controller) is the root cause of the problem!

Upvotes: 1

Related Questions