我刚刚安装了MVC3和VS2010 express。
用MVC3编写我的第一个应用,但不是我的第一个MVC应用。
我已经编写了一个自定义的html助手,如下所示
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;
}
}
}然后,我尝试将"using“添加到我的剃刀视图中,并将namepsace添加到views/webconfig文件中。
@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>但帮助器仍然不起作用。
它不会出现在intellisense中。
我做错了什么?
发布于 2011-07-09 02:43:37
我遇到了一个类似的问题,我的自定义HTML助手找不到了。我认为将助手的名称空间放在web.config中就足够了。但您还需要在视图中使用using语句(我使用的是razor):
@using MyProject.HtmlHelpers;感谢你张贴这篇文章。
发布于 2011-02-22 18:33:36
问题是我为HtmlHelper类添加了错误的using
using System.Web.WebPages.Html;应该是
using System.Web.Mvchttps://stackoverflow.com/questions/5076760
复制相似问题