C# 自定义Html帮助程序在asp.net MVC 2.0中不起作用

C# 自定义Html帮助程序在asp.net MVC 2.0中不起作用,c#,.net,asp.net-mvc-2,html-helper,C#,.net,Asp.net Mvc 2,Html Helper,我曾在asp.net mvc 1.0中使用此自定义html帮助程序,但现在我试图在2.0项目中使用它,结果它崩溃了 这就是我得到的错误 System.MissingMethodException was unhandled by user code Message=Method not found: 'System.String System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(System.Web.Mvc.HtmlHel

我曾在asp.net mvc 1.0中使用此自定义html帮助程序,但现在我试图在2.0项目中使用它,结果它崩溃了

这就是我得到的错误

System.MissingMethodException was unhandled by user code
  Message=Method not found: 'System.String System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(System.Web.Mvc.HtmlHelper)'.
  Source=CustomHtmlHelpers
  StackTrace:
       at CustomHtmlHelpers.ActionValidationSummaryHelper.ActionValidationSummary(HtmlHelper html, String action)
       at ASP.views_signin_signin_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in  SignIn.aspx:line 23
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer)   Site.Master:line 64
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Page.Render(HtmlTextWriter writer)
       at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
我在同一个库中的其他html助手也可以工作。我将名称空间添加到webconfig中

我有代码

 public static class ActionValidationSummaryHelper
    {
        public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action)
        {
            string currentAction = html.ViewContext.RouteData.Values["action"].ToString();

            if (currentAction.ToLower() == action.ToLower())
            {
                return html.ValidationSummary();
            }

            return MvcHtmlString.Empty;
        }

    }

它抱怨找不到返回字符串的ValidationSummery方法。。。所以我认为这可能是因为
HtmlHelper.ValidationSummary()
现在返回
MvcHtmlString
的实例,而不是
System.String

我尚未对此进行测试,但请尝试将扩展方法更改为:

public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action)
{
    string currentAction = html.ViewContext.RouteData.Values["action"].ToString();

    if (currentAction.ToLower() == action.ToLower())
        return html.ValidationSummary();

    return MvcHtmlString.Empty;
}
让我知道这是否有效:-)

HTHs,

查尔斯

嗯,不,看来还是不行。我得到了相同的错误。它仍然给出完全相同的异常?扩展方法是在您的web项目中还是单独的类库中?似乎是相同的例外。它在一个单独的类库里。冒着告诉你如何吸鸡蛋的风险。。。两个项目引用的是相同的asp.net mvc dll吗?您的意思是它们使用的是相同的版本吗?因为一开始它们并不存在,但如果您尝试使用MvcHtmlString并引用asp.net mvc 1.0 dll,它将不允许您进行编译,因为mvc 1.0中似乎不存在该dll。因此,我确实将库更改为使用2.0。它在你的电脑上工作吗?如果是这样,你能给我发电子邮件解决方案吗?发布该方法的源代码可能会有所帮助。好吧,它的代码与链接相同,现在是Charlino的新建议。