I have an ASP.NET Web API 2 application which uses convention-based routing:
[app-url]/v1/[controller]
Now, I want to add another version to the API, and found that attribute routing is the easiest way to achieve what I want. Therefore, I registered the Web API routes in the Application_Start()
method as shown below.
GlobalConfiguration.Configure(WebApiConfig.Register);
Additionally, I made sure that the Register()
method in the WebApiConfig
class only contains the following line:
config.MapHttpAttributeRoutes();
There are two controllers to test the versioning:
/v1/StatusController.cs
[RoutePrefix("v1/status")]
public class StatusController : ApiController
{
[Route("")]
public string Get()
{
return "V1 - OK";
}
}
/v2/StatusController.cs
[RoutePrefix("v2/status")]
public class StatusController : ApiController
{
[Route("")]
public string Get()
{
return "V2 - OK";
}
}
However, when I navigate to either of the routes shown below, I receive an error.
- [app-url]/v1/status
- [app-url]/v2/status
SERVER ERROR
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.