我在我的MVC4项目上创建了一个API控制器
以下是我创建的用于测试API功能的方法
private string Login(int id)
{
Employee emp = db.Employees.Find(id);
return emp.Firstname;
}
当我试图使用localhost:xxxx/api/controllerName/Login?id=2
访问此API时,我收到以下错误:
{"$id":"1","Message":"The requested resource does not support http method 'GET'."}
我做错了什么?
另外,这是我的API配置文件:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
[HttpGet]
或[AcceptVerbs("GET", "POST")]
... - gdoron