Click here to Skip to main content
15,891,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a web api controller in my MVC project as follows

public class EmployeeController : ApiController
{
    static readonly IEmployeeRepository repository = new EmployeeRepository();
    // GET api/<controller>
    [HttpGet]
    public object Get()
    {
        var queryString = HttpContext.Current.Request.QueryString;
        var data = repository.GetAll().ToList();
        return new { Items = data, Count = data.Count() };
    }

    public Employee GetEmployee(int id)
    {
        Employee emp = repository.Get(id);
        if (emp == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return emp;
    }

    // POST api/<controller>
    public HttpResponseMessage PostEmployee(Employee emp)
    {
        emp = repository.Add(emp);
        var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, emp);

        string uri = Url.Link("Employee", new { id = emp.EmployeeID });
        response.Headers.Location = new Uri(uri);
        return response;
    }
    [HttpPut]
    // PUT api/<controller>
    public void PutEmployee(Employee emp)
    {
        if (!repository.Update(emp))
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
     }
     [HttpDelete]
     public void Delete(int id)
     {
         Employee emp = repository.Get(id);
         if (emp == null)
         {
             throw new HttpResponseException(HttpStatusCode.NotFound);
         }
         repository.Remove(id);
     }
}


For me, Add(Put), Retrieve (get) and Update (Post) method getting triggered

But, Delete method is not getting triggered

And my URL of Delete Method is :

http://localhost:63187/api/Employee/2

and my Webapicongif.cs

public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "Employee",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        //config.EnableQuerySupport();
    }

and the global.cs file as follows

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        GlobalConfiguration.Configure(WebApiConfig.Register);

        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

    }


What I have tried:

When i do the delete operation,

i got an error report

Message : "No HTTP resource was found that matches the request URI 'http://localhost:63187/api/Employee/2'.

MessageDetail
:
"No action was found on the controller 'Employee' that matches the name '2'."
Name

General:

Request URL:http://localhost:63187/api/Employee/2
Request Method:DELETE
Status Code:404 Not Found
Remote Address:[::1]:63187

Response Header

Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Cache-Control:no-cache
Content-Length:204
Content-Type:application/json; charset=utf-8
Date:Thu, 30 Mar 2017 12:00:21 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcU2FtcGxlXE1WQ1xEYXRhTWFuYWdlclxBZGFwdG9yc1xTYW1wbGVcYXBpXEVtcGxveWVlXDI=?=

Request Header

Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:1
Content-Type:application/json; charset=UTF-8
DataServiceVersion:2.0
Host:localhost:63187
MaxDataServiceVersion:2.0
Origin:http://localhost:63187
Referer:http://localhost:63187/CRUDRemote/Remove
User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
X-Requested-With:XMLHttpRequest

Request Payload

2

Where i am committing the mistake.
Posted
Updated 6-Apr-17 2:06am
v2
Comments
Richard Deeming 4-Apr-17 8:56am    
Have you verified that there is actually an employee with that ID in your database?
Kalai Selvi 6-Apr-17 6:24am    
yes i have verified it.
Richard Deeming 6-Apr-17 7:33am    

1 solution

At last i found the issue, i have done the mistake in Global.cs and WebApiConfig

Updated Global.cs filed

C#
protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        //GlobalConfiguration.Configure(WebApiConfig.Register);

        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);


    //    WebApiConfig.Register(GlobalConfiguration.Configuration);
    }



and WebApiConfig

C#
public static void Register(HttpConfiguration config)
    {
        //config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
                        name: "Employee",
                        routeTemplate: "api/{controller}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                    );
        //config.EnableQuerySupport();
    }

Now, the Delete action updated triggered.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900