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?