skip to Main Content

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 : https://github.com/Dushyantsingh-ds/dotnet-issues/blob/main/Projects/EmployeeService/Readme.md

enter image description here

2

Answers


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

    [Route("api/employee/{EmpId}")]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search