---
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. 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.

   ```csharp

   using System.Net;
   using System.Net.Http;
   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](https://docs.kentico.com/k10/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 });
   }

   ```
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.

```js

$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.

   ```csharp

   using System.Net;
   using System.Net.Http;
   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](https://docs.kentico.com/k10/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 });
   }

   ```
8. 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.

```js

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

```
