[Translation] creating ASP. NET WebApi RESTful Service (10)

Source: Internet
Author: User

Version Management through URI

Another method to implement version management is to use URI for processing, similar to http: // localhost: {your_port}/api/v1/students /. The advantage of this method is that users can clearly know their current version. The implementation is also simple:

   1:  config.Routes.MapHttpRoute(
   2:                  name: "Students",
   3:                  routeTemplate: "api/v1/students/{userName}",
   4:                  defaults: new { controller = "students", userName = RouteParameter.Optional }
   5:                  );
   6:   
   7:  config.Routes.MapHttpRoute(
   8:                  name: "Students2",
   9:                  routeTemplate: "api/v2/students/{userName}",
  10:                  defaults: new { controller = "studentsV2", userName = RouteParameter.Optional }
  11:                  );

Although this method solves the problem, it actually violates the REST feature, because the routing rules are constantly changing.

Controller Selector Technology

Before introducing the third technology, let's review the routing principles of Web APIs. There is a method SelectController in the DefaultHttpControllerSelectro class, which accepts an HttpRequestMessage-type object and stores various route data (including the names of various controllers, these are all defined in the WebApiConfig file ). With this information, you can use the reflection technology to retrieve all objects derived from the ApiController class and then perform matching. If it is repeated or not found, an exception is thrown. Otherwise, the corresponding Controller object is returned.

To override this default implementation, You need to derive a class from Http. Dispatcher. DefaultHttpControllerSelector, which is called LearningControllerSelector and can then override SelectController. The Code is as follows:

   1:  public class LearningControllerSelector : DefaultHttpControllerSelector
   2:      {
   3:          private HttpConfiguration _config;
   4:          public LearningControllerSelector(HttpConfiguration config)
   5:              : base(config)
   6:          {
   7:              _config = config;
   8:          }
   9:   
  10:          public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
  11:          {
  12:              var controllers = GetControllerMapping(); //Will ignore any controls in same name even if they are in different namepsace
  13:   
  14:              var routeData = request.GetRouteData();
  15:   
  16:              var controllerName = routeData.Values["controller"].ToString();
  17:   
  18:              HttpControllerDescriptor controllerDescriptor;
  19:   
  20:              if (controllers.TryGetValue(controllerName, out controllerDescriptor))
  21:              {
  22:   
  23:                  var version = "2";
  24:   
  25:                  var versionedControllerName = string.Concat(controllerName, "V", version);
  26:   
  27:                  HttpControllerDescriptor versionedControllerDescriptor;
  28:                  if (controllers.TryGetValue(versionedControllerName, out versionedControllerDescriptor))
  29:                  {
  30:                      return versionedControllerDescriptor;
  31:                  }
  32:   
  33:                  return controllerDescriptor;
  34:              }
  35:   
  36:              return null;
  37:   
  38:          }
  39:      }

The main content of the Code includes:

First, replace the default Controller Selector with the custom Controller Selector, which is implemented in the Register Method of the WebApiConfig file.

   1:  config.Services.Replace(typeof(IHttpControllerSelector), new LearningControllerSelector((config)));
Version Management Using QueryString

The next step is more direct. You can directly APPEND "? V = 2 ", for example, http: // localhost: {your_port}/api/students /? V = 2. If no similar information is appended, the default version is the oldest version.

Add a function to implement this process.

   1:      private string GetVersionFromQueryString(HttpRequestMessage request)
   2:      {
   3:          var query = HttpUtility.ParseQueryString(request.RequestUri.Query);
   4:   
   5:          var version = query["v"];
   6:   
   7:          if (version != null)
   8:          {
   9:              return version;
  10:          }
  11:   
  12:          return "1";
  13:   
  14:      }

Add these logics to the SelectorController logic for on-demand calling. Of course, this implementation method will still violate the REST principle, because the URI is still not fixed.

Version Management Using custom Headers

Simply put, a record is added to the Header information. The user adds a version information to the request information and then processes it in the ControllerSelector location. The simple implementation code is as follows:

   1:      private string GetVersionFromHeader(HttpRequestMessage request)
   2:      {
   3:          const string HEADER_NAME = "X-Learning-Version";
   4:   
   5:          if (request.Headers.Contains(HEADER_NAME))
   6:          {
   7:              var versionHeader = request.Headers.GetValues(HEADER_NAME).FirstOrDefault();
   8:              if (versionHeader != null)
   9:              {
  10:                  return versionHeader;
  11:              }
  12:          }
  13:   
  14:          return "1";
  15:      }

Next, you can use Fiddler for testing and add a corresponding message to the Request Header.

The disadvantage of this method is that custom Header information must be added to Headers, so we will continue to introduce another method.

Use AcceptHeader for Version Management

Next we will use the Accept Header for version management. The final GET request Header should be "Accept: application/json; version = 2 ". Therefore, we can modify the logic as follows:

   1:      private string GetVersionFromAcceptHeaderVersion(HttpRequestMessage request)
   2:      {
   3:          var acceptHeader = request.Headers.Accept;
   4:   
   5:          foreach (var mime in acceptHeader)
   6:          {
   7:              if (mime.MediaType == "application/json")
   8:              {
   9:                  var version = mime.Parameters
  10:                  .Where(v => v.Name.Equals("version", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
  11:   
  12:                  if (version != null)
  13:                  {
  14:                      return version.Value;
  15:                  }
  16:                  return "1";
  17:              }
  18:          }
  19:          return "1";
  20:      }

This code retrieves version information from the Header whose MediaType is application/json, which is the test result of Fiddler.

This is a more standard method, because you do not need to add new custom headers or use different Uris.

If you subscribe to Plural sight and are interested in further research, you can view the Shawn Wildermuth course, which introduces other version management methods.

Source: http://bitoftech.net/2013/12/16/asp-net-web-api-versioning-accept-header-query-string/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.