如何将 int 数组传递给 RouteValueDictionary

发布于 2024-11-16 02:09:24 字数 562 浏览 3 评论 0原文

我需要生成此网址:http://localhost:3178/Reports/?GroupId=1211&GroupId=1237

我正在尝试:

var routeData = new RouteValueDictionary();
routeData.Add("GroupId", "1, 2");

获取:GroupId=1,%202

routeData.Add("GroupId", "1");
routeData.Add("GroupId", "2");

获取:已添加具有相同键的项目

,甚至

routeData.Add("GroupId[0]", "1");
routeData.Add("GroupId[1]", "2");

获取:?GroupId%5B0%5D=1&GroupId%5B1%5D=2

有可能以某种方式解决我的问题吗?

I need generate this url: http://localhost:3178/Reports/?GroupId=1211&GroupId=1237

I'm trying:

var routeData = new RouteValueDictionary();
routeData.Add("GroupId", "1, 2");

getting: GroupId=1,%202

or

routeData.Add("GroupId", "1");
routeData.Add("GroupId", "2");

getting: An item with the same key has already been added

and even

routeData.Add("GroupId[0]", "1");
routeData.Add("GroupId[1]", "2");

getting: ?GroupId%5B0%5D=1&GroupId%5B1%5D=2

it's possible to somehow fix my issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-11-23 02:09:24

RouteValueDictionary 旨在为路由提供信息。因此,我认为它不具备您所要求的功能。我一直在使用自定义帮助程序根据我传递给它的内容填充查询字符串数据:

    public static string BuildPath(RequestContext context, string routeName, RouteValueDictionary routeValues, object additionalParams)
    {
        var vpd = RouteTable.Routes[routeName].GetVirtualPath(context, routeValues);

        if (vpd == null)
            return string.Empty;

        var virtualpath = vpd.VirtualPath;
        var addparams = BuildAdditionalParams(additionalParams);

        if (!virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "?" + addparams;
        else if (virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "&" + addparams;

        return "/" + virtualpath;
    }

    protected static string BuildAdditionalParams(object additionalParams)
    {
        if (additionalParams == null)
            return string.Empty;

        StringBuilder sb = new StringBuilder();
        Type type = additionalParams.GetType();
        PropertyInfo[] props = type.GetProperties();

        Action<string, string> addProperty = (name, value) =>
        {
            if (sb.Length > 0)
                sb.Append("&");
            sb.Append(name);
            sb.Append("=");
            sb.Append(value);
        };

        foreach (PropertyInfo prop in props)
        {
            var simplevalue = prop.GetValue(additionalParams, null);
            if (simplevalue != null)
            {
                Type propertyType = prop.PropertyType;
                if (Nullable.GetUnderlyingType(propertyType) != null)
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                if (propertyType.IsEnum) 
                {
                    addProperty(prop.Name, ((int)simplevalue).ToString());
                }
                else if (propertyType.IsArray && propertyType != typeof(string))
                {
                    foreach (var val in prop.GetValue(additionalParams, null) as IEnumerable)                        
                        addProperty(prop.Name, val.ToString());                                                    
                }                    
                else
                {
                    if (!string.IsNullOrEmpty(simplevalue.ToString()))
                        addProperty(prop.Name, simplevalue.ToString());                        
                }
            }
        }
        return sb.ToString();
    }

此函数将构建路线的完整路径并将附加参数对象中的值作为查询字符串数据附加。这可以通过执行 ToString 方法来处理数组、枚举、可为空值和其他类型。

希望这有帮助!

The RouteValueDictionary is meant to provide information to routes. As such, I don't think it has the capability you're asking for. I've been using a custom helper to populate query string data based on what I pass into it:

    public static string BuildPath(RequestContext context, string routeName, RouteValueDictionary routeValues, object additionalParams)
    {
        var vpd = RouteTable.Routes[routeName].GetVirtualPath(context, routeValues);

        if (vpd == null)
            return string.Empty;

        var virtualpath = vpd.VirtualPath;
        var addparams = BuildAdditionalParams(additionalParams);

        if (!virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "?" + addparams;
        else if (virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "&" + addparams;

        return "/" + virtualpath;
    }

    protected static string BuildAdditionalParams(object additionalParams)
    {
        if (additionalParams == null)
            return string.Empty;

        StringBuilder sb = new StringBuilder();
        Type type = additionalParams.GetType();
        PropertyInfo[] props = type.GetProperties();

        Action<string, string> addProperty = (name, value) =>
        {
            if (sb.Length > 0)
                sb.Append("&");
            sb.Append(name);
            sb.Append("=");
            sb.Append(value);
        };

        foreach (PropertyInfo prop in props)
        {
            var simplevalue = prop.GetValue(additionalParams, null);
            if (simplevalue != null)
            {
                Type propertyType = prop.PropertyType;
                if (Nullable.GetUnderlyingType(propertyType) != null)
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                if (propertyType.IsEnum) 
                {
                    addProperty(prop.Name, ((int)simplevalue).ToString());
                }
                else if (propertyType.IsArray && propertyType != typeof(string))
                {
                    foreach (var val in prop.GetValue(additionalParams, null) as IEnumerable)                        
                        addProperty(prop.Name, val.ToString());                                                    
                }                    
                else
                {
                    if (!string.IsNullOrEmpty(simplevalue.ToString()))
                        addProperty(prop.Name, simplevalue.ToString());                        
                }
            }
        }
        return sb.ToString();
    }

This function will build a full path to your route and append the values in the additionalParams object as query string data. This can handle arrays, enums, nullable values, and other types that by executing their ToString method.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文