0

I am trying to build a simple API using MVC ApiControllers. But it seems that my routing is incorrect, because I always get a 404 response code when I hit my endpoint.

I have the ApiController code:

namespace MyProject.Web.Controllers
{
    public class TestController : ApiController
    {
        [AllowAnonymous]
        [Route("test")]
        public IHttpActionResult Test() {
            return Ok();
        }
    }
}

And my WebApiConfig.cs is:

namespace MyProject.Web 
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config) 
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

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

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
    }
}

When I go to the endpoint http://localhost:7248/test I get a 404 (NOT FOUND) error. I've also tried removing config.MapHttpAttributeRoutes(); and hitting the url http://localhost:7248/api/test/test but I still see a 404.

I can't seem to make this work regardless of what I try. Does anyone see any place that I am going wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
Brett
  • 11,637
  • 34
  • 127
  • 213
  • what would happen if you remove the route("test") and change the method to GetTest() ?! – Dan Hunex Oct 04 '14 at 03:46
  • @DanHunex Stil a `404`. I'm at a loss here. Could it be something in the `web.config`? (although I'm not sure what to look for there) – Brett Oct 04 '14 at 03:48
  • 1
    Found the answer here: [http://stackoverflow.com/a/26171997/304684][1] Odd one to have pop up. [1]: http://stackoverflow.com/a/26171997/304684 – Brett Oct 04 '14 at 03:57

0 Answers0