加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – mvc ActionLink中的RouteValueDictionary / obje

发布时间:2020-12-16 06:44:32 所属栏目:asp.Net 来源:网络整理
导读:所以这里: 我有一个html帮助器,它使用当前url的可选参数呈现ActionLinks.这个html帮助器还允许您根据需要添加一些可选参数,并将它们合并到1个RouteValueDictionary中. public static string ActionLinkwParams(this HtmlHelper helper,string linktext,stri
所以这里:

我有一个html帮助器,它使用当前url的可选参数呈现ActionLinks.这个html帮助器还允许您根据需要添加一些可选参数,并将它们合并到1个RouteValueDictionary中.

public static string ActionLinkwParams(this HtmlHelper helper,string linktext,string action,string controller,object extraRVs,object htmlAttributes) {

        //get current optional params from current URL
        NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

        //put those in a dict
        RouteValueDictionary r = new RouteValueDictionary();
        foreach (string s in c.AllKeys) {
            r.Add(s,c[s]);
        }

        RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

        RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

        //merge them
        RouteValueDictionary m = RouteValues.MergeRouteValues(r,extra);

        return helper.ActionLink(linktext,action,controller,m,htmlAtts).ToHtmlString();
    }

这很完美,但我现在添加了SecurityAware Actionlinks.

所以

return helper.ActionLink(linktext,htmlAtts).ToHtmlString();

return helper.SecurityTrimmedActionLink(linktext,htmlAtts);

然后调用:

public static string SecurityTrimmedActionLink(this HtmlHelper htmlHelper,string linkText,object htmlAttributes) {
        return SecurityTrimmedActionLink(htmlHelper,linkText,extraRVs,htmlAttributes,false);
    }

    public static string SecurityTrimmedActionLink(this HtmlHelper htmlHelper,object htmlAttributes,bool showDisabled) {
        if (controller == null) {
            RouteData routeData = htmlHelper.ViewContext.RouteData;
            controller = routeData.GetRequiredString("controller");
        }
        if (IsAccessibleToUser(action,controller)) {
            return htmlHelper.ActionLink(linkText,htmlAttributes).ToHtmlString();
        } else {
            return showDisabled ? String.Format("<span>{0}</span>",linkText) : "";
        }
    }

现在这不起作用.它编译,但我的URL看起来不太好.

<a count="3" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/2011-2012/Instelling?Count=3&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D">Back to List</a>

正如你所看到的,之前的工作,现在并不是因为它将RouteValueDictionarys作为对象,这给了我不想要的结果.

所以我想,如果我再次让他们成为RouteValueDictionarys怎么办?

if (IsAccessibleToUser(action,controller)) {

            RouteValueDictionary parsedextraRVs = null;
            if (extraRVs != null && extraRVs.GetType().Name == "RouteValueDictionary") {
                parsedextraRVs = (RouteValueDictionary)extraRVs;
            }

            RouteValueDictionary parsedHtmlAttributes = null;
            if (htmlAttributes != null && htmlAttributes.GetType().Name == "RouteValueDictionary") {
                parsedHtmlAttributes = (RouteValueDictionary)htmlAttributes;
            }


            return htmlHelper.ActionLink(linkText,parsedextraRVs == null ? extraRVs : parsedextraRVs,parsedHtmlAttributes == null ? htmlAttributes : parsedHtmlAttributes).ToHtmlString();
        }

但这也给了我刚刚发布的网址.
为什么这在我的ActionLinkwParams方法中有效,但在ActionLinkwParams调用SecurityTrimmedActionLink方法时却没有?我该如何解决这个问题?

解决方法

将SecurityTrimmedActionLink方法的签名修改为:

public static string SecurityTrimmedActionLink(
    this HtmlHelper htmlHelper,RouteValueDictionary extraRVs,RouteValueDictionary htmlAttributes
)

注意this和this之间的区别.在你的情况下(那个不起作用的那个)你正在调用第二个重载对象,但在你的情况下你不是传递匿名对象而是一个RouteValueDictionary,它被视为匿名对象对象及其公共属性(Count,Keys,Values)被序列化为属性.

备注:您的帮助方法不正确.他们返回字符串.这不是它应该如何.辅助方法应返回MvcHtmlString.

而不是

public static string ActionLinkwParams(...)
{
    ...
    return helper.ActionLink(linktext,htmlAtts).ToHtmlString();
}

它应该是:

public static MvcHtmlString ActionLinkwParams(...)
{
    ...
    return helper.ActionLink(linktext,htmlAtts);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读