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
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
In your project’s AssemblyInfo.csfile, add:
[assembly: CMS.AssemblyDiscoverable]
Using NuGet Package Manager, install Microsoft.AspNet.WebApi into the project. This adds all the necessary libraries to the project.
Make all the controllers you are placing in the class library inherit from the System.Web.Http.ApiController class.
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 }); } } }
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 }); }
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
Create a new library project in the Kentico solution. The project will contain your Api Controllers.
Reference the class library project in the CMSApp project. Or in your website if you are using a website project.
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
In your project’s AssemblyInfo.cs file, add:
[assembly: CMS.AssemblyDiscoverable]
Using NuGet Package Manager, install Microsoft.AspNet.WebApi into the project. This adds all the necessary libraries to the project.
Make all the controllers you are placing in the class library inherit from the System.Web.Http.ApiController class.
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 }); } } }
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 }); }
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
});