当前上下文中不存在 ViewContext

ViewContext does not exist in current context

我有一个 MVC 项目。在视图中,我正在使用 ViewContext 从这样的资源文件中加载值。

@ViewContext.HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel")

我遇到一个错误- The name ViewContext does not exit in current context

以下是我在页面中添加的命名空间。

@using System.Configuration
@using System.Globalization
@using System.Web.Configuration
@using Telerik.Sitefinity.Utilities.Lucene.Net.Documents
@using Telerik.Sitefinity.UI.MVC

我不知道为什么它会给我这个错误。如果我 运行 项目有错误,它会显示没有任何问题的页面。我还从资源文件中获取值。

但是不知道怎么解决

谢谢。

您不需要使用 ViewContext。在您看来,您只能访问这样的资源:

@System.Web.HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel")

或者你可以这样使用:

@HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel")

但是 Sitefinity 有更好的方法来处理资源本地化。

在 Sitefintiy 后端,您已经可以管理资源标签。 (在 Administration->Labels & Messages) 然后,在视图中,您可以像这样访问它:

@Html.Resource("AmazonSearchServiceTitle", "ProductsResources")

您可以在本文中找到有关 Sitefinity 资源的更多信息:http://docs.sitefinity.com/for-developers-create-custom-resource-classes