Mal*_*olm 2 razor asp.net-mvc-3
我刚安装了MVC3和VS 2010 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试在我的剃刀视图中添加"使用",并在views/webconfig文件中添加namepsace.
@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>
Run Code Online (Sandbox Code Playgroud)
但帮助者仍然无法正常工作.
它并没有出现在intellisense中.
我究竟做错了什么?
小智 6
我有一个类似的问题,我的自定义HTML助手没有找到.我认为将帮助程序的命名空间放在web.config中已经足够了.但是你还需要在视图中使用using语句(我正在使用razor):
@using MyProject.HtmlHelpers;
Run Code Online (Sandbox Code Playgroud)
感谢发布此内容.
| 归档时间: |
|
| 查看次数: |
2878 次 |
| 最近记录: |