2013-07-04 44 views
1

我試圖在PluralSight.com上關注John Papa的SPA課程,並遇到這個奇怪的問題。爲什麼我的[ActionName]屬性不起作用?

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'."} 

爲什麼?

回答

1

您必須檢查/App_Start/WebApiConfig.cs路由

它應該是這樣的:

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