Skip to content

Support API Version Model Binding #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
commonsensesoftware opened this issue Jul 24, 2018 · 0 comments
Closed

Support API Version Model Binding #320

commonsensesoftware opened this issue Jul 24, 2018 · 0 comments

Comments

@commonsensesoftware
Copy link
Collaborator

Overview

The requested ApiVersion is currently accessible via the following extension methods:

  • Request.GetRequestedApiVersion() [Web API]
  • Request.ApiVersioningProperties().RequestedApiVersion [Web API]
  • HttpContext.GetRequestedApiVersion() [Core]
  • HttpContext.Features.GetFeature<IApiVersioningFeature>().RequestedApiVersion [Core]

While this functionality is useful in a variety of contexts, it would be more natural to receive the ApiVersion value in the scope of a controller using model binding.

Proposed Solution

Enable modeling binding in ASP.NET Core and Web API. This will enable controllers to define an ApiVersion parameter and have the value provided by model binding.

[ApiVersion( "1.0" )]
[Route( "api/values" )]
public class ValuesController : ApiController
{
    public IHttpActionResult Get( ApiVersion requestedApiVersion ) =>
        Ok( new { controller = GetType().Name, version = requestedApiVersion.ToString() } );
}

Figure 1: API versioning model binding in ASP.NET Web API

[ApiVersion( "1.0" )]
[Route( "api/[controller]" )]
public class ValuesController : ControllerBase
{
    [HttpGet]
    public IActionResult Get( ApiVersion requestedApiVersion ) =>
        Ok( new { controller = GetType().Name, version = requestedApiVersion.ToString() } );
}

Figure 2: API versioning model binding in ASP.NET Core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant