Exception 创建了HtmlHelper扩展,但接收;不允许子操作执行重定向操作”;例外

Exception 创建了HtmlHelper扩展,但接收;不允许子操作执行重定向操作”;例外,exception,asp.net-mvc-4,extension-methods,Exception,Asp.net Mvc 4,Extension Methods,我创建了一个HtmlHelper扩展调用ActionButton,它将弹出一个jQuery按钮来调用一个操作,而不是使用ActionLink。但是,我在下面一行的扩展方法中收到一个异常(这只是一个代码示例,它将引发我看到的异常): 下面是完整的方法,但使用UrlHelper创建操作URL(对我来说很有用)。我还有使用System.Web.Mvc和使用System.Web.Mvc.Html public static HtmlString ActionButton(this HtmlHel

我创建了一个HtmlHelper扩展调用ActionButton,它将弹出一个jQuery按钮来调用一个操作,而不是使用ActionLink。但是,我在下面一行的扩展方法中收到一个异常(这只是一个代码示例,它将引发我看到的异常):

下面是完整的方法,但使用UrlHelper创建操作URL(对我来说很有用)。我还有
使用System.Web.Mvc
使用System.Web.Mvc.Html

    public static HtmlString ActionButton(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
    {
        TagBuilder tag = new TagBuilder("a");
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        tag.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

        // the next line causes the exception, not really being used other than to 
        // raise the exception and illustrate the call I was making
        MvcHtmlString str = helper.Action(actionName, controllerName, routeValues);

        tag.MergeAttribute("rel", "external");
        tag.MergeAttribute("data-role", "button");
        tag.MergeAttribute("data-mini", "true");
        tag.MergeAttribute("data-inline", "true");
        tag.MergeAttribute("data-icon", "arrow-r");
        tag.MergeAttribute("data-iconpos", "right");
        tag.MergeAttribute("data-theme", "b");

        tag.SetInnerText(linkText);
        HtmlString html = new HtmlString(tag.ToString(TagRenderMode.Normal));
        return html;
    }
我想知道为什么调用
helper.Action(…)
会引发异常


谢谢

这是因为您正在调用
helper.Action
(即
HtmlHelper
)而不是
urlHelper。Action
HtmlHelper.Action()
将调用操作方法,您将立即传递它,并返回其结果。
那不是你想要的


您需要
UrlHelper.Action()
,它将返回操作的URL。

感谢您的澄清。尽管@Matti也是正确的,但您解释了它引发异常的原因。我错过了工具提示中的一部分,该部分说它将以MvcHtmlString形式返回操作的结果。
    public static HtmlString ActionButton(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues)
    {
        TagBuilder tag = new TagBuilder("a");
        UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        tag.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

        // the next line causes the exception, not really being used other than to 
        // raise the exception and illustrate the call I was making
        MvcHtmlString str = helper.Action(actionName, controllerName, routeValues);

        tag.MergeAttribute("rel", "external");
        tag.MergeAttribute("data-role", "button");
        tag.MergeAttribute("data-mini", "true");
        tag.MergeAttribute("data-inline", "true");
        tag.MergeAttribute("data-icon", "arrow-r");
        tag.MergeAttribute("data-iconpos", "right");
        tag.MergeAttribute("data-theme", "b");

        tag.SetInnerText(linkText);
        HtmlString html = new HtmlString(tag.ToString(TagRenderMode.Normal));
        return html;
    }