Hi, I'm working on WebAPI, with a controller having C[HttpPost] R[HttpGet] U[HttpPut] D[HttpDelete]
- ///
- /// WebAPI - Controller
- ///
- public class PersonController : ApiController
- {
- static IList
personsList = LoadPersons(); - ///
- /// Get list of all Persons
- ///
- ///
- Persons
- [HttpGet]
- [Route("api/Person/")]
- public IHttpActionResult GetPerson()
- {
- return Ok(personsList);
- }
- ///
- /// Get a person based on input - entityId
- ///
- ///
- ///
A single person matching id - [HttpGet]
- [Route("api/Person/{entityId:int?}")]
- public IHttpActionResult GetPersonById([FromUri]int? entityId)
- {
- if (entityId != null)
- {
- var person = personsList.Where(p => p.EntityId == entityId).SingleOrDefault();
- return Ok(person);
- }
- return BadRequest("Invalid EntityId !!, can't have invalid chars");
- }
- ///
- /// Add a new person with specifed data
- ///
- ///
- ///
Ok, if success - [HttpPost]
- public IHttpActionResult SavePerson([FromBody]Person personData)
- {
- if (personData != null)
- {
- personsList.Add(personData);
- return Ok();
- }
- return BadRequest("Invalid !!");
- }
- ///
- /// Modify existing person's email-Id based on Id
- ///
- ///
- ///
- [HttpPut]
- public IHttpActionResult UpdatePerson([FromBody]Person personData)
- {
- if (personData != null)
- {
- var person = personsList.Where(p => p.EntityId == personData.EntityId).SingleOrDefault();
- person.Email = personData.Email;
- return Ok(person);
- }
- return BadRequest("Invalid !!");
- }
- ///
- /// Remove a specifc person based on Id
- ///
- ///
- ///
- [HttpDelete]
- public IHttpActionResult RemovePerson(int? entityId)
- {
- if (entityId != null)
- {
- var delPerson = personsList.Where(p => p.EntityId == entityId).SingleOrDefault();
- personsList.Remove(delPerson);
- return Ok();
- }
- return BadRequest("Invalid !!");
- }
- private static List
LoadPersons() - {
- var persons = new List
(); - persons.Add(new Person { Email = "ron's Email", EntityId = 01, Name = "ron" });
- persons.Add(new Person { Email = "saun's Email", EntityId = 02, Name = "saun" });
- persons.Add(new Person { Email = "jae's Email", EntityId = 03, Name = "jae" });
- return persons;
- }
- }
I'm able to do GET request and list person(s)
But when i try to do a POST/ PUT/ DELETE request i'm getting response as '"The requested resource does not support http method 'POST'/ 'PUT'/ 'DELETE''.
What am i missing here? help me to figure my mistake
Ruchi SharavatPosted Jul 30, 2018, 2:40 AM
L APosted Jul 26, 2018, 12:08 PM
Jenith ThakkarPosted Jul 25, 2018, 11:04 PM
L APosted Jul 25, 2018, 7:00 PM
Jignesh KumarPosted Jul 25, 2018, 12:42 AM
Jenith ThakkarPosted Jul 25, 2018, 12:42 AM
Try it with Just Adding Route As I Say in my last Comment
L APosted Jul 25, 2018, 12:37 AM
Jenith ThakkarPosted Jul 25, 2018, 12:10 AM
L APosted Jul 25, 2018, 12:06 AM
Jignesh KumarPosted Jul 24, 2018, 9:00 PM
Jayakumar BalasubramaniamPosted Jul 24, 2018, 7:29 PM