我的HtmlHelper出了什么问题?

Dej*_*n.S 3 asp.net-mvc html-helper

我在Helper类中创建了一个Html扩展方法,但是我无法让它工作.我已经实现了它,就像在不同的教程上看到的那样.

我的MenuItemHelper静态类:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
    {
        var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

        var sb = new StringBuilder();

        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            sb.Append("<li class=\"selected\">");
        else
            sb.Append("<li>");

        sb.Append(helper.ActionLink(linkText, actionName, controllerName));
        sb.Append("</li>");
        return sb.ToString();
    }
Run Code Online (Sandbox Code Playgroud)

导入命名空间

<%@ Import Namespace="MYAPP.Web.App.Helpers" %>
Run Code Online (Sandbox Code Playgroud)

在我的master.page上实现

<%= Html.MenuItem("TEST LINK", "About", "Site") %> 
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息:

找不到方法:'System.String System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper,System.String,System.String,System.String)

编辑: 似乎问题是应用程序名称.该文件夹名为MYAPP-MVC.Web,但在类中它转换为MYAPP_MVC.Web

我只是尝试了一个新的应用程序,它的工作原理

Dar*_*rov 11

尝试以更多ASP.NET MVCish 2.0样式重写助手.另外,不要忘记System.Web.Mvc.Html在帮助程序命名空间中添加使用,以便您可以访问该ActionLink方法:

namespace MYAPP.Web.App.Helpers
{
    using System.Web.Mvc;
    using System.Web.Mvc.Html;

    public static class HtmlExtensions
    {
        public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var li = new TagBuilder("li");
            if (string.Equals(currentControllerName, controllerName, StringComparison.CurrentCultureIgnoreCase) &&
                string.Equals(currentActionName, actionName, StringComparison.CurrentCultureIgnoreCase))
            {
                li.AddCssClass("selected");
            }

            li.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString();
            return MvcHtmlString.Create(li.ToString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,您肯定会遇到System.Web.Mvc正在使用的程序集的一些版本问题.