मीडिया टाइप्स एक API को क्लाइंट को यह बताने की अनुमति देते हैं कि पेलोड में डेटा की व्याख्या कैसे करें। HTTP प्रोटोकॉल में, मीडिया टाइप्स को text/html, application/json, और application/xml जैसे पहचानकर्ताओं के साथ निर्दिष्ट किया जाता है, जो क्रमशः HTML, JSON, और XML से मेल खाते हैं। अन्य API-विशिष्ट मीडिया टाइप्स भी हैं, जैसे application/vnd.api+json।
मीडिया टाइप्स में वर्जन भेजना
application/vnd.demo.students.v1+json StudentsV1Controller
application/vnd.demo.students.v2+json StudentsV2Controller

अपना खुद का CustomControllerSelector जोड़ने से उपरोक्त त्रुटि ठीक हो जाएगी।
CustomControllerSelector
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
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 = "";
string regex = @"application\/vnd\.demo\.([a-z]+)\.v(?[0-9]+)\+([a-z]+)";
var acceptHeader = request.Headers.Accept
.Where(a => Regex.IsMatch(a.MediaType, regex, RegexOptions.IgnoreCase));
if (acceptHeader.Any())
{
var match = Regex.Match(acceptHeader.First().MediaType, regex, RegexOptions.IgnoreCase);
versionNumber = match.Groups["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;
}
}
}
WebApi.Config.cs में पंजीकरण
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 students = new List
{
new StudentV1 { Id = 1, Name = "Mark" },
new StudentV1 { Id = 2, Name = "John" }
};
public IEnumerable 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 students = new List
{
new StudentV2 { Id = 1, FirstName = "Roger", LastName = "Federer" },
new StudentV2 { Id = 2, FirstName = "Tom", LastName = "Bruce" }
};
public IEnumerable Get()
{
return students;
}
public StudentV2 Get(int id)
{
var studentForId = students.FirstOrDefault(x => x.Id == id);
return studentForId;
}
}
}
नीचे दिए गए आउटपुट StudentV1 और StudentV2 कंट्रोलर्स से कस्टम मीडिया टाइप्स में वर्जनिंग के साथ मिलने वाले परिणाम दिखाते हैं।


अब यदि हम कस्टम मीडिया टाइप का उपयोग करके XML फॉर्मेट में समान आउटपुट प्राप्त करना चाहते हैं, तो webapiconfig.cs में नीचे दिए गए कस्टम मीडिया टाइप जोड़ें:
XML के लिए कस्टम मीडिया टाइप जोड़ना
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(config));
config.Formatters.XmlFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("application/vnd.demo.student.v1+xml"));
config.Formatters.XmlFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("application/vnd.demo.student.v2+xml"));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

उपरोक्त उदाहरण में हम देख सकते हैं कि आउटपुट कस्टम मीडिया टाइप में निर्दिष्ट अनुसार XML फॉर्मेट में है।