自定义HTML佣工在不工作MVC3 [英] Custom html helpers dont work in MVC3
问题描述
我刚安装MVC3和VS 2010的前preSS。
I have just installed MVC3 and VS 2010 express.
写我的第一个应用程序在MVC3但不是我的第一个MVC应用程序。
Writing my first app in MVC3 but not my first MVC app.
我已经写了一个自定义的HTML帮助像这样
I have written a custom html helper like so
namespace MyNamespace.Web.Helpers
{
public static class HtmlExtensions
{
public static string RenderHeaderImages(this HtmlHelper html)
{
StringBuilder bldr = new StringBuilder();
List<string> files = (List<string>)HttpContext.Current.Application["HeaderImages"];
bldr.AppendLine("<table><tr>");
foreach (string file in files)
{
bldr.AppendLine(string.Format("<td><img class=\"headerImage\" src=\"{0}/Content/Images/Header/{1}\", alt=\"{2}\" /></td>"
, HtmlExtensions.GetAppPath(""), file, file));
}
bldr.AppendLine("</table></tr>");
return bldr.ToString();
}
public static string GetAppPath(this HtmlHelper html)
{
return HtmlExtensions.GetAppPath("");
}
public static string GetAppPath(this HtmlHelper html, string arg)
{
return HtmlExtensions.GetAppPath(arg);
}
public static string GetAppPath(string arg)
{
if (HttpContext.Current.Request.ApplicationPath.ToString() == @"/")
return "http://localhost:50194" + arg;
else
return HttpContext.Current.Request.ApplicationPath.ToString() + arg;
}
}
}
然后我试图加入使用我的Razor视图,并且还加入namepsace的意见/ webconfig文件。
Then I tried adding "using" to my razor view and also adding namepsace to the views/webconfig file.
@using MyNamespace.Web.Helpers
<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="MyNamespace.Web.Helpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
但还是助手无法正常工作。
But still the helper does not work.
和它不上来的智能感知。
And it does not come up in intellisense.
我在做什么错了?
推荐答案
我在这里没有被找到我自定义的HTML帮助类似的问题。我认为这是足以让助手的名字空间在web.config中。但你还需要使用语句视图里面的(我用剃刀):
I had a similar problem where my custom HTML helper was not being found. I thought it was enough to put the helper's namespace in web.config. But you also need the using statement inside your view (I'm using razor):
@using MyProject.HtmlHelpers;
感谢张贴此。
这篇关于自定义HTML佣工在不工作MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!