Accept 헤더는 브라우저가 데이터를 원하는 파일 형식을 서버에 알려줍니다. 이러한 파일 형식은 일반적으로 MIME 형식이라고 합니다. MIME은 다목적 인터넷 메일 확장 프로그램의 약자입니다.
버전 관리는 아래와 같이 헤더로 보낼 수 있습니다.
Version=1 StudentsV1Controller Version=2 StudentsV2Controller

수락 헤더에서 버전을 처리하지 않았기 때문에 StudentV1 및 StudentV2 컨트롤러만 있으므로 404 not found 오류가 발생합니다. 자체 CustomControllerSelector를 추가해 보겠습니다. DefaultHttpControllerSelector 클래스를 구현합니다.
CustomControllerSelector -
예시
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
namespace WebAPI.Custom{
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 = "";
var acceptHeader = request.Headers.Accept.Where(a => a.Parameters
.Count(p => p.Name.ToLower() == "version") > 0);
if (acceptHeader.Any()){
versionNumber = acceptHeader.First().Parameters
.First(p => p.Name.ToLower() == "version").Value;
}
HttpControllerDescriptor controllerDescriptor;
if (versionNumber == "1"){
controllerName = string.Concat(controllerName, "V1");
}
else if (versionNumber == "2"){
controllerName = string.Concat(controllerName, "V2");
}
if (controllers.TryGetValue(controllerName, out controllerDescriptor)){
return controllerDescriptor;
}
return null;
}
}
} 다음으로 기본 컨트롤러 선택기를 사용자 지정 컨트롤러 선택기로 교체해야 합니다. 이것은 WebApiConfig.cs 파일에서 수행됩니다. IHttpControllerSelector를 CustomControllerSelector로 교체하고 있습니다. DefaultHttpControllerSelector는 IHttpControllerSelector를 구현하므로 IHttpControllerSelector를 교체합니다.
예시
public static class WebApiConfig{
public static void Register(HttpConfiguration config){
config.Services.Replace(typeof(IHttpControllerSelector), new
CustomControllerSelector(config));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
} StudentV1Controller -
예시
using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
public class StudentV1Controller : ApiController{
List<StudentV1> students = new List<StudentV1>{
new StudentV1{
Id = 1,
Name = "Mark"
},
new StudentV1{
Id = 2,
Name = "John"
}
};
public IEnumerable<StudentV1> Get(){
return students;
}
public StudentV1 Get(int id){
var studentForId = students.FirstOrDefault(x => x.Id == id);
return studentForId;
}
}
} StudentV2Controller -
예시
using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
public class StudentV2Controller : ApiController{
List<StudentV2> students = new List<StudentV2>{
new StudentV2{
Id = 1,
FirstName = "Roger",
LastName = "Federer"
},
new StudentV2{
Id = 2,
FirstName = "Tom",
LastName = "Bruce"
}
};
public IEnumerable<StudentV2> Get(){
return students;
}
public StudentV2 Get(int id){
var studentForId = students.FirstOrDefault(x => x.Id == id);
return studentForId;
}
}
} 아래 출력은 수락 헤더의 버전 관리를 통해 StudentV1 및 StudentV2 컨트롤러에서 얻은 결과를 보여줍니다.

