【问题标题】:Why is my [ActionName] attribute not working?为什么我的 [ActionName] 属性不起作用?
【发布时间】:2014-01-26 16:18:55
【问题描述】:

我正在尝试在 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'."}

为什么?

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api-routing


    【解决方案1】:

    您必须检查 /App_Start/WebApiConfig.cs 上的路由

    应该是这样的:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2019-02-19
      • 2017-02-16
      相关资源
      最近更新 更多