0

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

    }
1
  • Where is the code that does the POST? Commented May 21, 2019 at 0:20

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Not so. In Web API, by convention: "you can specify with attribute, otherwise, Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method."
while this may be correct, good practice would be to use the correct attributes, especially since the documentation says .... starts with .... too easy to make silly mistakes for the sake of saving the writing of one attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.