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.csfile, add:

    • [assembly: CMS.AssemblyDiscoverable]
  3. Using NuGet Package Manager, install Microsoft.AspNet.WebApi into the project. This adds all the necessary libraries to the project.

  4. 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 = 0)
             {
                 // You can return a variety of things in Web API controller actions. 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 });
             }
         }
     }
    
    
     
  5. 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 });
     }
    
    
     
  6. Rebuild the solution.

    • If your custom project uses a different .NET version, you may need to manually add the Newtonsoft.Json library to the /bin folder.

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




$http.get("http://myserver/customapi/CustomWebAPI", { 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. Using NuGet Package Manager, install Microsoft.AspNet.WebApi into the project. This adds all the necessary libraries to the project.

  6. 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 = 0)
             {
                 // You can return a variety of things in Web API controller actions. 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 });
             }
         }
     }
    
    
     
  7. 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 });
     }
    
    
     
  8. If your solution contains the CMSApp_MVC project, remove the project from the solution and delete all the files in the /Lib/MVC folder or follow the steps for using the CMSApp_MVC project for your MVC development.

  9. Rebuild the solution.

    • If your custom project uses a different .NET version, you may need to manually add the Newtonsoft.Json library to the /bin folder.

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




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