Created
September 15, 2016 09:07
-
-
Save khellang/c99a80c3f0e7ca3ec00efa4973a41c66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("customers")] | |
public class CustomerController | |
{ | |
public CustomerController(DbContext context) | |
{ | |
Context = context; | |
} | |
private DbContext Context { get; } | |
[HttpGet("{id}")] | |
public async Task<IActionResult> Get(int id, CancellationToken cancellationToken) | |
{ | |
var customer = await Context.Customers.FindAsync(id, cancellationToken); | |
if (customer == null) | |
{ | |
return new NotFoundResult(); | |
} | |
return new ObjectResult(customer); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerMapper : IEntityMapper<Customer> | |
{ | |
public object Map(Customer customer, int? version) | |
{ | |
if (version.HasValue) | |
{ | |
switch (version.Value) | |
{ | |
case 2: return new CustomerModelV2(customer); | |
case 3: return new CustomerModelV3(customer); | |
} | |
} | |
return new CustomerModelV1(customer); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VersioningStrategy : IVersioningStrategy | |
{ | |
public Task<int?> GetVersion(HttpContext context) | |
{ | |
var accept = context.Request.Headers["Accept"]; | |
// Parse out the version from Accept header. Examples: | |
// - application/json -> 1 | |
// - application/json; version=1 -> 1 | |
// - application/vnd.my-app.v2+json -> 2 | |
// - application/vnd.my-app+json; version=3 -> 3 | |
return Task.FromResult<int?>(2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment