---
title: Using ASP.NET Web API with Kentico
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

You need to perform certain tasks to make [ASP.NET Web API](http://www.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](http://msdn.microsoft.com/en-us/library/ms228042%28v=vs.140%29.aspx)_ 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](http://msdn.microsoft.com/en-us/library/t1zz5y8c%28v=vs.90%29.aspx) 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.

   ```csharp

   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](https://docs.kentico.com/k81/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) , register custom routes that do not conflict with Kentico's 'cmsapi' or any other page paths.

   ```csharp

   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.

```js

$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](http://msdn.microsoft.com/en-us/library/ms228042%28v=vs.140%29.aspx)_ 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](http://msdn.microsoft.com/en-us/library/t1zz5y8c%28v=vs.90%29.aspx) 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.

   ```csharp

   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](https://docs.kentico.com/k81/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md), register custom routes that do not conflict with Kentico's 'cmsapi' or any other page paths.

   ```csharp

   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](https://docs.kentico.com/k81/developing-websites/developing-sites-using-the-mvc-framework/upgrading-the-mvc-version.md) 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](https://www.nuget.org/packages/Newtonsoft.Json/) 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.

```js

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

```
