我有一个 HtmlHelper 扩展方法,它从数据库缓存中提取本地化文本。代码是这样的。(MVCWeb 是我的 MVC 应用程序的命名空间。)
using System.Web;
using System.Web.Mvc;
namespace MVCWeb.PresentationExtensions
{
public static class HtmlHelperExtensions
{
public static HtmlString GetText(this HtmlHelper Html, string keyword)
{
// code to get the text based on the keyword
}
}
}
我@using MVCWeb.PresentationExtensions
在我的视图中使用。在 ~/Views 文件夹中,调用扩展方法工作正常。
我最近添加了一个区域。我在 ~/Areas/AreaName/Views 文件夹中的 View 文件中使用扩展方法,并且代码正在编译并且它确实有效,但是我在 IDE 中遇到错误。
每次我@Html.GetText("SomeKeyword")
从“区域”视图中使用时,错误列表中都会显示以下两个错误。
- “System.Web.WebPages.Html.HtmlHelper”不包含“GetText”的定义,并且最佳扩展方法重载“MVCWeb.PresenationExtension.HtmlHelperExtensions.GetText(System.Web.Mvc.HtmlHelper, string)”有一些无效参数
- 实例参数:无法从“System.Web.WebPages.Html.HtmlHelper”转换为“System.Web.Mvc.HtmlHelper”
我发现在 ~/Views 中,@Html 有以下代码注释:
HtmlHelper<dynamic> WebViewPage<dynamic>.Html
Gets or sets the System.Web.Mvc.HtmlHelper object that is used to render HTML elements.
在 ~/Area/AreaName/Views 中,@Html 有以下注释:
HtmlHelper WebPage.Html
Gets the System.Web.WebPages.Html.HtmlHelper object that is associated with a page.
作为参考,我在 ~/Views 和 ~/Areas/AreaName/Views 中的 Web.config 文件匹配。这是 .NET 4.5 上的 MVC4 应用程序,尚未从先前版本的 MVC 转换。
- @Html 在常规视图与区域视图中被定义为不同类型是否正常?
- 如果 IDE 显示错误,为什么编译和运行正确?这是一个IDE错误吗?
- 如何阻止这些错误在 IDE 中显示?