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
Ramco Ramco
441
3.4k
522.7k
Delete Api not working
Feb 25 2021 5:03 AM
Hi
I have below code. when i type in Url - http://localhost:40991/api/Employees/DeleteEmployee?id=1
It is going in public HttpResponseMessage GetEmployee(int id) . Why so
public
class
EmployeesController : ApiController
{
private
PMDbEntities db =
new
PMDbEntities();
// GET: api/Employees
public
IHttpActionResult GetEmployees()
{
try
{
var results = (from d
in
db.Employees
join f
in
db.Departments
on d.DepartmentId equals f.ID
select
new
{
Id = d.ID,
Name = d.Name,
Department = f.Description
}).ToList();
if
(results ==
null
)
{
return
NotFound();
}
return
Ok(results);
}
catch
(Exception)
{
return
BadRequest();
}
}
[ResponseType(
typeof
(Employee))]
public
HttpResponseMessage GetEmployee(
int
id)
{
Employee employee = db.Employees.Find(id);
if
(employee ==
null
)
{
return
Request.CreateErrorResponse(HttpStatusCode.NotFound,
"Employee Code : "
+ id +
"not found"
);
}
//return Request.CreateResponse(HttpStatusCode.OK,employee);
return
Request.CreateResponse(HttpStatusCode.OK);
}
// DELETE: api/Employees/5
// [ResponseType(typeof(Employee))]
public
IHttpActionResult DeleteEmployee(
int
id)
{
try
{
Employee employee = db.Employees.Find(id);
if
(employee ==
null
)
{
return
Content(HttpStatusCode.NotFound,
"Employee not found"
);
}
else
{
db.Employees.Remove(employee);
db.SaveChanges();
return
Ok();
//(employee);
}
}
catch
(Exception ex)
{
return
BadRequest(
"Error Encountered : "
+ ex);
}
}
}
Thanks
Reply
Answers (
4
)
Blazor with oAuth 2.0 Authorization
Post Api with few fields