Last active
September 16, 2019 10:45
-
-
Save gagannn/fc8bec5fc45216995bae299bc49caeee to your computer and use it in GitHub Desktop.
Versioning Using Controllers
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Web; | |
using System.Web.Http; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Dispatcher; | |
namespace Versioning | |
{ | |
public class CustomControllerSelector :DefaultHttpControllerSelector | |
{ | |
private HttpConfiguration _config; | |
public CustomControllerSelector(HttpConfiguration config): base(config) | |
{ | |
_config = config; | |
} | |
public override HttpControllerDescriptor SelectController(HttpRequestMessage request) | |
{ | |
var controllers = GetControllerMapping(); | |
var routeData = request.GetRouteData(); | |
var controllerName = routeData.Values["controller"].ToString(); | |
string versionNumber = "1"; | |
string customHeader = "X-EmployeesService-Version"; | |
if (request.Headers.Contains(customHeader)) | |
{ | |
versionNumber = request.Headers.GetValues(customHeader).FirstOrDefault(); | |
} | |
if (versionNumber == "1") | |
{ | |
controllerName = controllerName + "V1"; | |
} | |
else | |
{ | |
controllerName = controllerName + "V2"; | |
} | |
HttpControllerDescriptor controllerDescriptor; | |
if(controllers.TryGetValue(controllerName,out controllerDescriptor)) | |
{ | |
return controllerDescriptor; | |
} | |
return null; | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace Versioning.Models | |
{ | |
public class EmployeeV1 | |
{ | |
public int EmployeeID { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using Versioning.Models; | |
namespace Versioning.Controllers | |
{ | |
public class EmployeeV1Controller : ApiController | |
{ | |
EmployeeV1[] e1 = new EmployeeV1[] | |
{ | |
new EmployeeV1(){EmployeeID =1, FirstName="DJ",LastName="Foo" }, | |
new EmployeeV1(){EmployeeID =2, FirstName="MJ",LastName="Koo" }, | |
new EmployeeV1(){EmployeeID =3, FirstName="RJ",LastName="Hoo" }, | |
}; | |
public IEnumerable<EmployeeV1> GetEmployees() | |
{ | |
return e1; | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace Versioning.Models | |
{ | |
public class EmployeeV2 | |
{ | |
public int EmployeeID { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public int Age { get; set; } | |
public int Salary { get; set; } | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using Versioning.Models; | |
namespace Versioning.Controllers | |
{ | |
public class EmployeeV2Controller : ApiController | |
{ | |
EmployeeV2[] e1 = new EmployeeV2[] | |
{ | |
new EmployeeV2(){EmployeeID =1, FirstName="DJ",LastName="Foo" ,Age=2,Salary=10}, | |
new EmployeeV2(){EmployeeID =2, FirstName="MJ",LastName="Koo",Age=3,Salary=20 }, | |
new EmployeeV2(){EmployeeID =3, FirstName="RJ",LastName="Hoo" ,Age=4,Salary=30}, | |
}; | |
public IEnumerable<EmployeeV2> GetEmployees() | |
{ | |
return e1; | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Http; | |
using System.Web.Http.Dispatcher; | |
namespace Versioning | |
{ | |
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// Web API configuration and services | |
config.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(config)); | |
// Web API routes | |
config.MapHttpAttributeRoutes(); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment