如何从 Web API 访问 Kentico 内容?
How can I access Kentico content from Web API?
使用 Kentico's documentation 我在我的 Kentico 解决方案中建立了一个新项目,内容如下:
- Microsoft.AspNet.WebApi 来自 Nuget
- 已将
[assembly: CMS.AssemblyDiscoverable]
添加到 AssemblyInfo.cs
- 引用:
- CMS.Base
- CMS.Core
- CMS.DataEngine
添加了一个 class,它继承了 CMS.DataEngine.Module
并在其 OnInit()
中包含以下内容:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
"customapi",
"customapi/{controller}/{id}",
新 { id = RouteParameter.Optional });
根据上面引用的文档添加了一个继承 ApiController
的控制器,称为 TestController
。
因为我希望这个 API 托管在与我的主要 Kentico 网站相同的域下,所以我将这个自定义项目作为 CMCApp_AppCode
项目的依赖项。当我重新构建 运行 时,我现在可以按预期在以下位置调用我的 API:http://dev.local/customapi/test
问题是现在我想通过 API 使用 Kentico 的文档 API 和 return 页面数据。但是,如果我在我的 API 控制器中添加对 Kentico 的 API 的调用,我会收到各种错误。例如:
- 正在调用
CMS.SiteProvider.SiteContext.CurrentSite
returns:
Evaluation of method CMS.SiteProvider.SiteContext.get_CurrentSite
requires calling method System.RuntimeType.IsDelegate, which cannot be
called in this context.
- 调用
CMS.DocumentEngine.DocumentHelper.GetDocument
抛出:
Cannot evaluate expression because a thread is stopped at a point
where garbage collection is impossible, possibly because the code is
optimized.
我尝试过的事情:
- 在控制器动作开始时添加以下内容,但这没有区别 -
CMS.DataEngine.CMSApplication.Init();
- 将
/customapi
添加到 Kentico 设置中排除的 URL。
所以我完全被卡住了 - 我怎样才能让 Kentico 与我的网站一起工作 API?
这种异常只发生在调试时(在watch 或immediate window 中评估表达式时)。
检查您在调试时是否选择了"Debug"(=不是"Release")配置。
此外,请确保调试配置未启用 "Optimize code"。该调试信息设置为 "Advanced" 下的 "Full"。
如果它没有帮助,那么只需存储您对变量的调用结果并对其进行评估。其他选项是使用 Debug
或 Trace
记录结果,如下所示:https://support.microsoft.com/en-us/help/815788/how-to-trace-and-debug-in-visual-c
使用 Kentico's documentation 我在我的 Kentico 解决方案中建立了一个新项目,内容如下:
- Microsoft.AspNet.WebApi 来自 Nuget
- 已将
[assembly: CMS.AssemblyDiscoverable]
添加到AssemblyInfo.cs
- 引用:
- CMS.Base
- CMS.Core
- CMS.DataEngine
添加了一个 class,它继承了
CMS.DataEngine.Module
并在其OnInit()
中包含以下内容:GlobalConfiguration.Configuration.Routes.MapHttpRoute( "customapi", "customapi/{controller}/{id}", 新 { id = RouteParameter.Optional });
根据上面引用的文档添加了一个继承
ApiController
的控制器,称为TestController
。
因为我希望这个 API 托管在与我的主要 Kentico 网站相同的域下,所以我将这个自定义项目作为 CMCApp_AppCode
项目的依赖项。当我重新构建 运行 时,我现在可以按预期在以下位置调用我的 API:http://dev.local/customapi/test
问题是现在我想通过 API 使用 Kentico 的文档 API 和 return 页面数据。但是,如果我在我的 API 控制器中添加对 Kentico 的 API 的调用,我会收到各种错误。例如:
- 正在调用
CMS.SiteProvider.SiteContext.CurrentSite
returns:
Evaluation of method CMS.SiteProvider.SiteContext.get_CurrentSite requires calling method System.RuntimeType.IsDelegate, which cannot be called in this context.
- 调用
CMS.DocumentEngine.DocumentHelper.GetDocument
抛出:
Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.
我尝试过的事情:
- 在控制器动作开始时添加以下内容,但这没有区别 -
CMS.DataEngine.CMSApplication.Init();
- 将
/customapi
添加到 Kentico 设置中排除的 URL。
所以我完全被卡住了 - 我怎样才能让 Kentico 与我的网站一起工作 API?
这种异常只发生在调试时(在watch 或immediate window 中评估表达式时)。
检查您在调试时是否选择了"Debug"(=不是"Release")配置。
此外,请确保调试配置未启用 "Optimize code"。该调试信息设置为 "Advanced" 下的 "Full"。
如果它没有帮助,那么只需存储您对变量的调用结果并对其进行评估。其他选项是使用 Debug
或 Trace
记录结果,如下所示:https://support.microsoft.com/en-us/help/815788/how-to-trace-and-debug-in-visual-c