I am working on ..net framework 8.0 with WebApi project and trying to enable asp versioning on the rest api endpoints. All the endpoints are broken and I am unable to figure out what is wrong. Can anyone help?
namespace Testproject1.Controllers
{
using Testproject.Controllers.Models;
using Asp.Versioning;
using System.Web.Http;
[ApiVersion(1.0)]
[Route("api/v{version:apiVersion}/[controller]")]
public class AgreementsController: ApiController
{
[Route("{accountId}")]
public IHttpActionResult Get(string accountId) =>
Ok(new Agreement(GetType().FullName, accountId, "1"));
}
}
namespace Testproject1.Controllers
{
using Testproject.Controllers.Models;
using Asp.Versioning;
using System.Web.Http;
[ApiVersion(2.0)]
[Route("api/v{version:apiVersion}/[controller]")]
public class AgreementsControllerv2 : ApiController
{
[Route("{accountId}")]
public IHttpActionResult Get(string accountId) =>
Ok(new Agreement(GetType().FullName, accountId, "2"));
}
}
namespace Testproject1.Controllers.V1
{
using Testproject.Controllers.Models;
using Asp.Versioning;
using System.Web.Http;
[ApiVersion(2.0)]
[Route("api/v{version:apiVersion}/[controller]")]
public class OrdersController : ApiController
{
// GET ~/v1/orders/{accountId}
[Route("{accountId}")]
public IHttpActionResult Get(string accountId) =>
Ok(new Order(GetType().FullName, accountId, "1"));
}
}
And my Startup looks like
var configuration = new HttpConfiguration();
var httpServer = new HttpServer(configuration);
configuration.MapHttpAttributeRoutes();
configuration.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
});
configuration.Routes.MapHttpRoute(
name: "VersionedApi",
routeTemplate: "api/v{version:apiVersion}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, version = RouteParameter.Optional });
builder.UseWebApi(httpServer);
I am getting following response
Version: 1
http://localhost:9999/api/v1.0/agreements/1
{
“Message”: “No HTTP resource was found that matches the request URI ‘http://localhost:9999/api/v2.0/agreements/1’.”,
“MessageDetail”: “No action was found on the controller ‘Agreements’ that matches the request.”
}
Version 2:
http://localhost:9999/api/v2.0/agreements/1
{
“Message”: “No HTTP resource was found that matches the request URI ‘http://localhost:9999/api/v1.0/agreements/1’.”,
“MessageDetail”: “No action was found on the controller ‘Agreements’ that matches the request.”
}
Orders:
http://localhost:9999/api/v2.0/orders/1
{
“code”: “ApiVersionUnspecified”,
“traceId”: “776cf174-79ab-4386-a856-2f385f5f51ca”,
“type”: “https://docs.api-versioning.org/problems#unspecified”,
“title”: “Unspecified API version”,
“status”: 400,
“detail”: “An API version is required, but was not specified.”
}