我对我的MVC4项目的Web API:请求的资源不支持HTTP方法“GET”
这里创建了一个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”, “消息”: “所请求的资源不支持HTTP方法 '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);
}
开始使该方法公开 – 2013-03-16 22:45:13
@GiorgioMinardi,继续'[HttpGet] //或[AcceptVerbs(“GET”,“POST”)]'... – gdoron 2013-03-16 22:47:15
@giorgio Minardi,@ gdoron哦。多谢你们。我的错。所以很抱歉用这么琐碎的东西来打扰社区。它现在可行,但我期待它能给我带来d格式的“名字:约翰”。它只是给“约翰”。这是正常的行为? – ylinkz 2013-03-16 23:15:48