为什么不能剃须刀找到我的HTML帮助? [英] Why can't Razor find my HTML Helper?
问题描述
我试图端口一个经典ASP.NET MVC视图剃刀,并试图使用传统(非剃刀)HTML辅助方法时卡住了。辅助方法具有以下签名:
I'm trying to port a "classic" ASP.NET MVC view to Razor and got stuck when trying to use a traditional (non-Razor) Html helper method. The helper method has the following signature:
public static string WrappedValidationSummary(this HtmlHelper htmlHelper, string SummaryError)
{
...
}
助手方法不会在常规(非剃刀)视图中使用时,它做工精细。
The helper method does work fine when using it in regular (non-Razor) views.
当在这样的的Razor视图使用它:
When using it in the Razor view like this:
@Html.WrappedValidationSummary("Mitarbeiter konnnte nicht angelegt werden.");
我得到一个运行时错误消息
I get a run-time error message that
System.Web.Mvc.HtmlHelper
不包含一个定义
WrappedValidationSummary无
扩展方法
WrappedValidationSummary接受
类型的第一个参数
System.Web.Mvc.HtmlHelper
可以找到(是否缺少
使用指令或程序集
引用?)
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'WrappedValidationSummary' and no extension method 'WrappedValidationSummary' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
在Visual Studio和智能感知剃刀语法检查都没有问题,找到我的扩展方法的定义。重新编译该项目没有帮助。
The Razor syntax checker in Visual Studio and Intellisense have no problem finding my extension method's definition. Recompiling the project does not help.
这是怎么回事了?
推荐答案
你有没有加入辅助的命名空间到你的浏览/ web.config中?
Have you added the helper's namespace to your Views/web.config?
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="CUSTOM_NAMESPACE" />
</namespaces>
</pages>
</system.web.webPages.razor>
如果您使用的是RC,如果你在一个早期测试版,你需要添加命名空间中的页面或Global.asax中以上才有效。
The above will only work if you're using an RC, if you're on an early Beta, you'll need to add the namespace in the page or Global.asax.
另外,我建议改变返回类型为 HtmlString
。
Also, I'd suggest changing the return type to HtmlString
.
return new HtmlString(STRING_VALUE);
这篇关于为什么不能剃须刀找到我的HTML帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!