TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
L A
NA
170
171.6k
WebAPI - 405 - The resource does not support http 'POST'
Jul 24 2018 2:16 PM
Hi, I'm working on WebAPI, with a controller having C[HttpPost] R[HttpGet] U[HttpPut] D[HttpDelete]
/// <summary>
/// WebAPI - Controller
/// </summary>
public
class
PersonController : ApiController
{
static
IList<Person> personsList = LoadPersons();
/// <summary>
/// Get list of all Persons
/// </summary>
/// <returns><list type="Person">Persons</list></returns>
[HttpGet]
[Route(
"api/Person/"
)]
public
IHttpActionResult GetPerson()
{
return
Ok(personsList);
}
/// <summary>
/// Get a person based on input - entityId
/// </summary>
/// <param name="entityId"></param>
/// <returns>A single person matching id</returns>
[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"
);
}
/// <summary>
/// Add a new person with specifed data
/// </summary>
/// <param name="personData"></param>
/// <returns>Ok, if success</returns>
[HttpPost]
public
IHttpActionResult SavePerson([FromBody]Person personData)
{
if
(personData !=
null
)
{
personsList.Add(personData);
return
Ok();
}
return
BadRequest(
"Invalid !!"
);
}
/// <summary>
/// Modify existing person's email-Id based on Id
/// </summary>
/// <param name="personData"></param>
/// <returns></returns>
[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 !!"
);
}
/// <summary>
/// Remove a specifc person based on Id
/// </summary>
/// <param name="entityId"></param>
/// <returns></returns>
[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<Person> LoadPersons()
{
var persons =
new
List<Person>();
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
Reply
Answers (
11
)
all message of validation in french aspnet mvc
Youtube Private videos access