Home > front end >  Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed
Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed

Time:10-24

Get, Post methods are working, but I'm going to run the Put and Delete request then I face an error message.

Complete Project Url : enter image description here

CodePudding user response:

Your delete endpoint should also have a [Route(...)] data annotation:

[Route("api/employee/{EmpId}")]

CodePudding user response:

you have to decide what are you going to use - attribute routing or default routing from config file.

For today the most common way to use API is to assign attribute routing to the controller

[Route("~/api/[controller]/[action]]
public class EmployeeController : ApiController

you can use https//localhost:44350/api/employee/get for Get()

and so on

 // /api/employee/get
 public IEnumerable<Employee> Get()

// /api/employee/get/5 
[HttpGet("{empId}")]
 public HttpResponseMessage Get(int empId)

 //   /api/employee/post" for 
 public HttpResponseMessage Post([FromBody] Employee employee)

  // /api/employee/delete/5   
[Route("{empId}")]
 public HttpResponseMessage Delete(int empId)

 // /api/employee/put/5   
[Route("{empId}")]
 public HttpResponseMessage Put(int empId, [FromBody] Employee employee)
      

and since you don't put methods as action attributes , you don't need to use delete and put, you can use get and post instead.

  • Related