我在 MVC4 中有一个 Web API。使用 ajax 发布数据时出现 404,我不明白为什么。
语言控制器:
[AcceptVerbs("POST")]
public void Delete(string id)
{
Guid guid = Guid.Parse(id);
Language language = db.Languages.Find(guid);
db.Languages.Remove(language);
db.SaveChanges();
}
路由:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
和 javascript(使用 AngularJS):
this.delete = function (lang) {
$http({
method: "POST",
url: "/api/language/delete",
data: JSON.stringify({ id: lang.id })
})
.success(function (response) {
return true;
})
.error(function (response) {
return false;
});
};
我收到此错误消息:
**{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:32166/api/language/delete'.","MessageDetail":"No action was found on the controller 'Language' that matches the request."}**
我只是不明白为什么,它看起来应该工作。我觉得我错过了一条重要信息。