我正试图在PluralSight.com上关注John Papa关于SPA的课程,并遇到这个奇怪的问题。
public class LookupController : ApiControllerBase
{
// GET: api/lookup/samples
[ActionName("samples")]
public IEnumerable<Sample> GetSamples()
{
return Uow.Samples.GetAll().OrderBy(x => x.Name);
}
}
如果我使用localhost:49210/api/lookup/getsamples
,我会得到一份样本列表。但是,当我使用localhost:49210/api/lookup/samples
时,出现以下错误:
{"message":"No HTTP resource was found that matches the request URI
'http://localhost:49210/api/lookup/samples'.","messageDetail":"No
action was found on the controller 'Lookup' that matches the name
'samples'."}
为什么?
答案 0 :(得分:1)
您必须检查/App_Start/WebApiConfig.cs
上的路由它应该是这样的:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);