gpt4 book ai didi

c# - 为什么我的 [ActionName] 属性不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:56 24 4
gpt4 key购买 nike

我正在尝试学习 John Papa 在 PluralSight.com 上关于 SPA 的类(class),但遇到了这个奇怪的问题。

   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 }
);

关于c# - 为什么我的 [ActionName] 属性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17476285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com