Using ASP.NET Web API with Kentico

You need to perform certain tasks to make ASP.NET Web API work with Kentico. The steps vary based on the version of Web API that you want to use.

Note that Kentico uses ASP.NET Web API 1 for internal APIs. These are registered at the /cmsapi/ endpoint and aren’t meant for public use.

Using ASP.NET Web API 1

  1. Add the following references to your custom project. Find the libraries in the Lib folder of your Kentico solution:

    • CMS.Base
    • CMS.Core
    • CMS.DataEngine
  2. In your project’s AssemblyInfo.cs file, add: [assembly: CMS.AssemblyDiscoverable]

  3. Add the following references to your custom project. Find the libraries in the CMS/CMSDependencies folder of your Kentico solution:

    • Newtonsoft.Json
    • System.Net.Http
    • System.Net.Http.Formatting
    • System.Web.Http
    • System.Web.Http.WebHost
  4. Set the ‘Copy Local’ property of these references to ‘False’.

  5. Register the controllers in your customer project using attribute [assembly: RegisterApiController(typeof(MyWebAPIController))].
    The controllers need to inherit from the System.Web.Http.ApiController class.

    
    
    
     using System.Web.Http;
    
     [assembly: RegisterApiController(typeof(CustomWebAPIController))]
    
     namespace MyCompany.MySpace
     {
         public class CustomWebAPIController : ApiController
         {
             public HttpResponseMessage Get(int id)
             {
                 // You can return a variety of things here, for more details see http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results
                 return Request.CreateResponse(HttpStatusCode.OK, new { Data = "test data", ID = id });
             }
         }
     }
    
    
     
  6. In your startup logic , register custom routes that do not conflict with Kentico’s ‘cmsapi’ or any other page paths.

    
    
    
     protected override void OnInit()
     {
         base.OnInit();
         GlobalConfiguration.Configuration.Routes.MapHttpRoute("customapi", "customapi/{controller}/{id}", new { id = RouteParameter.Optional });
     }
    
    
     
  7. Rebuild the solution.

You can now make calls on the custom route that you registered.




$http.get("http://myserver/customapi/MyWebAPI", { id: 1})
    .success(function (data) {
        alert(data);
    })
    .error(function(error) {
        // handle the error
    });


Using ASP.NET Web API 2

  1. Create a new library project in the Kentico solution. The project will contain your Api Controllers.

  2. Reference the class library project in the CMSApp project. Or in your website if you are using a website project.

  3. Add the following references to the class library’s project. Find the libraries in the Lib folder of your Kentico solution:

    • CMS.Base
    • CMS.Core
    • CMS.DataEngine
  4. In your project’s AssemblyInfo.cs file, add:
    [assembly: CMS.AssemblyDiscoverable]

  5. Add the following references to your custom project. Find the libraries in the CMS/CMSDependencies folder of your Kentico solution:

    • Newtonsoft.Json
    • System.Net.Http
    • System.Net.Http.Formatting
    • System.Web.Http
    • System.Web.Http.WebHost
  6. Set the ‘Copy Local’ property of these references to ‘False’.

  7. Make all the controllers you are placing in the class library inherit from the System.Web.Http.ApiController class.

    
    
    
     using System.Web.Http;
    
     namespace MyCompany.MySpace
     {
         public class CustomWebAPIController : ApiController
         {
             public HttpResponseMessage Get(int id)
             {
                 // You can return a variety of things here, for more details see http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results
                 return Request.CreateResponse(HttpStatusCode.OK, new { Data = "test data", ID = id });
             }
         }
     }
    
    
     
  8. In your startup logic, register custom routes that do not conflict with Kentico’s ‘cmsapi’ or any other page paths.

    
    
    
     protected override void OnInit()
     {
         base.OnInit();
         GlobalConfiguration.Configuration.Routes.MapHttpRoute("customapi", "customapi/{controller}/{id}", new { id = RouteParameter.Optional });
     }
    
    
     
  9. Remove the CMSApp_MVC project from the solution and delete the /Lib/MVC folder or upgrade to MVC 5 if you use MVC.

Synchronizing a correct version of the Newtonsoft.Json library

You will need to keep an updated version of the Newtownsoft.Json library in your Kentico project. That is, keep the version from /packages synchronized with a version of the library in the Kentico’s CMS/CMSDependencies folder.

  1. In the CMS/CMSDependencies folder, create a new folder called Newtonsoft.Json.6.0.0.0 (or a different number, according to the version of the library that you want to use).
  2. Place the same version of the library into the folder.
    • Note that every time you update the library, you will have to update the name of the folder as well.
  3. Rebuild the solution.

You can now make calls on the custom route that you registered.




$http.get("http://myserver/customapi/MyWebAPI", { id: 1})
    .success(function (data) {
        alert(data);
    })
    .error(function(error) {
        // handle the error
    });