gpt4 book ai didi

asp.net-mvc-3 - 自定义 html 助手在 MVC3 中不起作用

转载 作者:行者123 更新时间:2023-12-03 17:33:32 24 4
gpt4 key购买 nike

我刚刚安装了 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;
}
}
}

然后我尝试将“using”添加到我的 razor View 中,并将 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>

但是助手仍然不起作用。

并且它不会出现在智能感知中。

我做错了什么?

最佳答案

我遇到了类似的问题,找不到我的自定义 HTML 帮助程序。我认为将助手的命名空间放在 web.config 中就足够了。但您还需要在 View 中使用 using 语句(我正在使用 razor):

@using MyProject.HtmlHelpers;

感谢您发布此内容。

关于asp.net-mvc-3 - 自定义 html 助手在 MVC3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com