{"id":3298,"date":"2017-03-28T23:38:24","date_gmt":"2017-03-28T23:38:24","guid":{"rendered":"http:\/\/www.rezafaisal.net\/?p=3298"},"modified":"2017-03-28T23:42:35","modified_gmt":"2017-03-28T23:42:35","slug":"asp-net-core-web-api-versioning","status":"publish","type":"post","link":"http:\/\/www.rezafaisal.net\/?p=3298","title":{"rendered":"ASP.NET Core Web API Versioning"},"content":{"rendered":"<p>ASP.NET Web API merupakan implementasi RESTful API service, adalah application program interface (API) yang menggunakan HTTP request untuk melakukan GET, PUT, POST dan DELETE data.\u00a0 RESTful API Service ini biasanya diakses oleh aplikasi client seperti aplikasi desktop\u00a0 atau aplikasi web, tapi umumnya lebih sering digunakan oleh aplikasi mobile.\u00a0 Dalam dunia nyata, contoh implementasi seperti yang dilakukan oleh Twitter atau Facebook.\u00a0 Keduanya memiliki Restful API service untuk dapat diakses oleh aplikasi client.\u00a0 Selain digunakan oleh aplikasi mobile Twitter dan Facebook yang dibangun oleh Twitter dan Facebook sendiri tetapi juga dapat digunakan oleh kita atau software developer lain yang ingin mengambil data dari Twitter dan Facebook, atau jika ingin menggunakan proses otentikasi milik Twitter dan Facebook.<\/p>\n<p>Nah seperti halnya software pada umumnya, fungsi-fungsi pada Restful API service juga dapat mengalami perbaikan, penambahan atau mungkin ada fungsi yang dihilangkan.\u00a0 Jika RESTful API service sekelas Twitter dan Facebook begitu saja mengganti dan menghapus fungsi yang ada maka dampaknya mungkin akan membuat aplikasi client yang kita dan software developer buat akan mengalami kegagalan. Oleh karena itu, Twitter dan Facebook mengelola perubahan ini dengan melakukan versioning. Teknik versioning ini memungkinkan kita untuk tetap dapat mengakses dan menggunakan fungsi-fungsi lama dari Restful API service, dan juga dapat mengakses dan menggunakan fungsi-fungsi baru.\u00a0 Artinya kedua versi dari layanan bisa diakses. Hal ini memungkinkan kita dapat memiliki waktu untuk mengupgrade aplikasi yang kita bangun untuk menggunakan fungsi-fungsi Restful API service yang baru.<\/p>\n<p>Sekarang implementasi versioning pada ASP.NET Web API dapat dilakukan dengan mudah dengan menggunakan library \u201cASP.NET API Versioning\u201d.\u00a0 Pada posting ini akan diberikan contoh implementasi versioning pada ASP.NET Core Web API.<\/p>\n<p>Dengan asumsi telah dimiliki project ASP.NET Core Web API, langkah pertama yang perlu dilakukan adalah menambahkan library \u201cASP.NET API Versioning\u201d ke dalam project. Caranya adalah dengan memodifikasi file *.csproj menjadi seperti contoh berikut.\u00a0 Penambahkan library dapat dilihat pada baris 18.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum1\" style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">Project<\/span> <span style=\"color: #ff0000;\">Sdk<\/span><span style=\"color: #0000ff;\">=\"Microsoft.NET.Sdk.Web\"<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum2\" style=\"color: #606060;\">   2:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum3\" style=\"color: #606060;\">   3:<\/span>   <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PropertyGroup<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum4\" style=\"color: #606060;\">   4:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">TargetFramework<\/span><span style=\"color: #0000ff;\">&gt;<\/span>netcoreapp1.1<span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">TargetFramework<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum5\" style=\"color: #606060;\">   5:<\/span>   <span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">PropertyGroup<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum6\" style=\"color: #606060;\">   6:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum7\" style=\"color: #606060;\">   7:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum8\" style=\"color: #606060;\">   8:<\/span>   <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">ItemGroup<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum9\" style=\"color: #606060;\">   9:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.AspNetCore\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.1\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum10\" style=\"color: #606060;\">  10:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.AspNetCore.Mvc\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.2\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum11\" style=\"color: #606060;\">  11:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.AspNetCore.StaticFiles\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.1\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum12\" style=\"color: #606060;\">  12:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.Extensions.Logging.Debug\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.1\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum13\" style=\"color: #606060;\">  13:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.VisualStudio.Web.BrowserLink\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.0\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum14\" style=\"color: #606060;\">  14:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"MySql.Data.Core\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"7.0.4-IR-191\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum15\" style=\"color: #606060;\">  15:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.EntityFrameworkCore\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.1\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum16\" style=\"color: #606060;\">  16:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.EntityFrameworkCore.Tools\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.1.0\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum17\" style=\"color: #606060;\">  17:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"MySql.Data.EntityFrameworkCore\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"7.0.4-IR-191\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum18\" style=\"color: #606060;\">  18:<\/span>     <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">PackageReference<\/span> <span style=\"color: #ff0000;\">Include<\/span><span style=\"color: #0000ff;\">=\"Microsoft.AspNetCore.Mvc.Versioning\"<\/span> <span style=\"color: #ff0000;\">Version<\/span><span style=\"color: #0000ff;\">=\"1.0.3\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum19\" style=\"color: #606060;\">  19:<\/span>   <span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">ItemGroup<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum20\" style=\"color: #606060;\">  20:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum21\" style=\"color: #606060;\">  21:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum22\" style=\"color: #606060;\">  22:<\/span> <span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">Project<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>Kemudian perlu ditambahkan method services.AddApiVersioning(); di dalam method ConfigureServices pada file Startup.cs, seperti yang dapat dilihat pada baris no. 5.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum1\" style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">void<\/span> ConfigureServices(IServiceCollection services)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum2\" style=\"color: #606060;\">   2:<\/span> {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum3\" style=\"color: #606060;\">   3:<\/span>     <span style=\"color: #008000;\">\/\/ Add framework services.<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum4\" style=\"color: #606060;\">   4:<\/span>     services.AddMvc();<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum5\" style=\"color: #606060;\">   5:<\/span>     services.AddApiVersioning();<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum6\" style=\"color: #606060;\">   6:<\/span>     . . .<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum7\" style=\"color: #606060;\">   7:<\/span> }<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>{<strong><em>Versi 1.0<\/em><\/strong>}<\/p>\n<p>Berikut ini adalah contoh class Web API Controller.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum1\" style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Collections.Generic;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum2\" style=\"color: #606060;\">   2:<\/span> <span style=\"color: #0000ff;\">using<\/span> Microsoft.AspNetCore.Mvc;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum3\" style=\"color: #606060;\">   3:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum4\" style=\"color: #606060;\">   4:<\/span> [ApiVersion( <span style=\"color: #006080;\">\"1.0\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum5\" style=\"color: #606060;\">   5:<\/span> [Route( <span style=\"color: #006080;\">\"api\/v{version:apiVersion}\/[controller]\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum6\" style=\"color: #606060;\">   6:<\/span> <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> HelloWorldController : Controller {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum7\" style=\"color: #606060;\">   7:<\/span>     [HttpGet]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum8\" style=\"color: #606060;\">   8:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">string<\/span> Get() {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum9\" style=\"color: #606060;\">   9:<\/span>         <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #006080;\">\"Hello world ver 1.0 !\"<\/span>;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum10\" style=\"color: #606060;\">  10:<\/span>     }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum11\" style=\"color: #606060;\">  11:<\/span> }<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>Pada class di atas dapat dilihat untuk menentukan versi API dengan cara memberikan atribut pada class HelloWorldController seperti yang dilihat pada baris ke-4.\u00a0 Sedangkan format URL untuk mengakses class web API ini adalah baris ke-5.\u00a0 Artinya class ini dapat diakses dengan URL berikut.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<pre id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">http:\/\/localhost:5000\/api\/v1.0\/helloworld<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Pada baris ke-5, parameter [controller] ditulis dengan huruf kecil, oleh karena itu seluruh nama class controller di atas juga ditulis dengan huruf kecil seperti pada contoh URL di atas.\u00a0 Parameter [controller] akan menyesuaikan dengan nama class yang digunakan, karena nama class adalah HelloWorldController maka pada URL class ini dipanggil dengan cara \u201chelloworld\u201d.\u00a0 Jika nama class adalah HelloWebAPI maka pada URL class ditulis \u201chellowebapi\u201d.<\/p>\n<p>{<strong><em>Ver 2.0 &amp; 3.0<\/em><\/strong>}<\/p>\n<p>Sebuah class web API controller dapat digunakan untuk menangani 2 versi sekaligus. Berikut adalah contoh implementasinya.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum1\" style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Collections.Generic;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum2\" style=\"color: #606060;\">   2:<\/span> <span style=\"color: #0000ff;\">using<\/span> Microsoft.AspNetCore.Mvc;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum3\" style=\"color: #606060;\">   3:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum4\" style=\"color: #606060;\">   4:<\/span> [ApiVersion( <span style=\"color: #006080;\">\"2.0\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum5\" style=\"color: #606060;\">   5:<\/span> [ApiVersion( <span style=\"color: #006080;\">\"3.0\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum6\" style=\"color: #606060;\">   6:<\/span> [Route( <span style=\"color: #006080;\">\"api\/v{version:apiVersion}\/helloworld\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum7\" style=\"color: #606060;\">   7:<\/span> <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> HelloWorld2Controller : Controller {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum8\" style=\"color: #606060;\">   8:<\/span>     [HttpGet]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum9\" style=\"color: #606060;\">   9:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">string<\/span> Get() {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum10\" style=\"color: #606060;\">  10:<\/span>         <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #006080;\">\"Hello world ver 2.0 !\"<\/span>;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum11\" style=\"color: #606060;\">  11:<\/span>     }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum12\" style=\"color: #606060;\">  12:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum13\" style=\"color: #606060;\">  13:<\/span>     [HttpGet, MapToApiVersion( <span style=\"color: #006080;\">\"3.0\"<\/span> )]<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum14\" style=\"color: #606060;\">  14:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">string<\/span> GetV3() {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum15\" style=\"color: #606060;\">  15:<\/span>         <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #006080;\">\"Hello world ver 3.0 !\"<\/span>;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum16\" style=\"color: #606060;\">  16:<\/span>     }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum17\" style=\"color: #606060;\">  17:<\/span> }<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>Pada baris ke-4 dan ke-5 adalah atribut pada class untuk memberikan atribut versi.\u00a0 Secara default versi class ini adalah baris ke-4\u00a0 yaitu versi 2.0, karena versi ini dideklarasikan pertama.\u00a0 Kemudian dapat dilihat nama class controller di atas adalah HelloWorld2Controller, jadi semestinya secara default class ini diakses dengan cara menulis \u201chelloworld2\u201d.\u00a0 Tetapi pada baris ke-6 dapat dilihat format untuk akses class ini, pada baris ini tidak digunakan parameter [controller], tetapi secara manual digunakan nilai \u201chelloworld\u201d.\u00a0 Sehingga class ini dapat diakses dengan URL berikut.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<pre id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">http:\/\/localhost:5000\/api\/v2.0\/helloworld<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>hasilnya akan dikeluarkan output berikut ini.<\/p>\n<p><a href=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/blog01.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"blog01\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/blog01_thumb.jpg\" alt=\"blog01\" width=\"550\" height=\"300\" border=\"0\" \/><\/a><\/p>\n<p>Pada class ini dapat dilihat terdapat 2 method yang menggunakan HTTP request GET yaitu Get() dan GetV3().\u00a0 Pada contoh di atas method GetV3() dimapping sebagai method versi 3.0 dengan cara memberikan atribut pada method ini seperti yang dilihat pada baris ke-13. Untuk mengakses method ini dapat dilakukan dengan cara menggunakan URL berikut ini.<\/p>\n<div id=\"codeSnippetWrapper\" style=\"font-size: 8pt; overflow: auto; cursor: text; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<pre id=\"codeSnippet\" style=\"font-size: 8pt; overflow: visible; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">http:\/\/localhost:5000\/api\/v3.0\/helloworld<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p><a href=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/blog02.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"blog02\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/blog02_thumb.jpg\" alt=\"blog02\" width=\"550\" height=\"298\" border=\"0\" \/><\/a><\/p>\n<p>Dengan penjelasan di atas dapat dilihat pengelolaan versi web API dapat dilakukan dengan mudah.<\/p>\n<p>Berikut adalah source code dari contoh di atas: <a title=\"https:\/\/github.com\/rezafaisal\/ASPNETCoreMySQL\/tree\/master\/EFCoreWebAPI\" href=\"https:\/\/github.com\/rezafaisal\/ASPNETCoreMySQL\/tree\/master\/EFCoreWebAPI\">https:\/\/github.com\/rezafaisal\/ASPNETCoreMySQL\/tree\/master\/EFCoreWebAPI<\/a>.<\/p>\n<p>Semoga bermanfaat <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" style=\"border-style: none;\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/wlEmoticon-smile-1.png\" alt=\"Smile\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Web API merupakan implementasi RESTful API service, adalah application program interface (API) yang menggunakan HTTP request untuk melakukan GET, PUT, POST dan DELETE data.\u00a0 RESTful API Service ini biasanya diakses oleh aplikasi client seperti aplikasi desktop\u00a0 atau aplikasi web,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3269,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[68,160,108],"class_list":["post-3298","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-softwaredev","tag-asp-net","tag-asp-net-core","tag-web-api"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2017\/03\/Capture.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1sNAL-Rc","_links":{"self":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/3298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3298"}],"version-history":[{"count":3,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/3298\/revisions"}],"predecessor-version":[{"id":3302,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/3298\/revisions\/3302"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/media\/3269"}],"wp:attachment":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3298"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}