newman
newman

Reputation: 6911

Why is my [ActionName] attribute not working?

I'm trying to follow John Papa's course on SPA at PluralSight.com, and run into this strange problem.

   public class LookupController : ApiControllerBase
   {
        // GET: api/lookup/samples
        [ActionName("samples")]
        public IEnumerable<Sample> GetSamples()
        {
            return Uow.Samples.GetAll().OrderBy(x => x.Name);
        }        
    }

If I use localhost:49210/api/lookup/getsamples and I get a list of samples. However, when I use localhost:49210/api/lookup/samples, I got following error:

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

Why?

Upvotes: 3

Views: 3240

Answers (1)

Marco Ramires
Marco Ramires

Reputation: 1116

You must check the routing on /App_Start/WebApiConfig.cs

It should look like this:

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

Upvotes: 2

Related Questions