我對我的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