为什么我的[ActionName]属性不起作用
本文关键字:属性 不起作用 ActionName 我的 为什么 | 更新日期: 2023-09-27 18:20:58
我正试图在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'."}
为什么?
您必须在/App_Start/WebApiConfig.cs 上检查路由
它应该是这样的:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);