如何在 C# ASP.NET WebAPI 中使用自定义媒体类型进行版本控制?
媒体类型允许 API 让客户端了解如何解释有效负载中的数据。在 HTTP 协议中,媒体类型使用 text/html、application/json 和 application/xml 等标识符指定,媒体类型分别对应最常见的 Web 格式 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(?<version>[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<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 控制器时获得的结果。


因此,如果我们现在希望使用自定义媒体类型获得 XML 格式的相同输出,请在 webapiconfig.cs 中添加以下自定义媒体类型
示例
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 格式,如自定义媒体类型中所指定。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP