ylinkz ylinkz - 2 years ago 298
ASP.NET (C#) Question

Web api: The requested resource does not support http method 'GET'

I created an API controller on my MVC4 project

Here is the method I created to test the functionality of the API

private string Login(int id)
Employee emp = db.Employees.Find(id);
return emp.Firstname;

When I try to access this api with
, I get

{"$id":"1","Message":"The requested resource does not support http method 'GET'."}

What am I doing wrong?

Also, here is my api config file

public static void Register(HttpConfiguration config)
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

Answer Source

Change the method modifier from private to public, also add the relevant accept verbs to the action

private string Login(int id)

Change to:

[HttpGet] // Or [AcceptVerbs("GET", "POST")]
public string Login(int id)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download