Using Kentico's documentation I've set up a new project in my Kentico solution with the following:
- Microsoft.AspNet.WebApi from Nuget
- Added
[assembly: CMS.AssemblyDiscoverable]toAssemblyInfo.cs - Referenced:
- CMS.Base
- CMS.Core
- CMS.DataEngine
Added a class which inherits
CMS.DataEngine.Moduleand has the following in itsOnInit():GlobalConfiguration.Configuration.Routes.MapHttpRoute( "customapi", "customapi/{controller}/{id}", new { id = RouteParameter.Optional });
Added a controller inheriting
ApiControllercalledTestControlleras per documentation referenced above.
Because I want this API hosted under the same domain as my main Kentico website I've then made this custom project a dependency of the CMCApp_AppCode project. When I re-build and run I can now call my API as expected at: http://dev.local/customapi/test
The problem is that now I want to work with Kentico's document API and return page data via the API. However, if I add calls to Kentico's API inside my API controller I get all sorts of errors. For example:
- Calling
CMS.SiteProvider.SiteContext.CurrentSitereturns:
Evaluation of method CMS.SiteProvider.SiteContext.get_CurrentSite requires calling method System.RuntimeType.IsDelegate, which cannot be called in this context.
- Calling
CMS.DocumentEngine.DocumentHelper.GetDocumentthrows:
Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.
Things I've tried:
- Adding the following at the start of the controller action but this makes no difference -
CMS.DataEngine.CMSApplication.Init(); - Adding
/customapito the excluded URLs in Kentico settings.
So I'm completely stuck - how can I get Kentico to work with my web API?

