ASP.NET MVC 将列表传递给 RouteData QueryString

发布于 2024-08-13 03:40:23 字数 1017 浏览 6 评论 0原文

将值列表作为 QueryString 传递的推荐方法是

www.site.com/search?value=1&value=2&value=3&value=4

ASP.NET 可以很好地处理这一点:

string value = QueryString.Get("value"); // returns "1,2,3,4"

但我无法找出将这些值传递到 RouteData 的方法。显而易见的方法是添加

int[] value = {1,2,3,4};

到 RouteData 中,并让超级智能的 MVC 为我解决问题。不幸的是,MVC 在将数组传递到 RouteData 时是转储的,它基本上调用 .ToString() 将 value=int[] 添加到我的 QueryString 中。

我尝试将值添加到 RouteValueDictionary (但作为字典无法处理这个:)

RouteValueDictionary dict = new RouteValueDictionary();
dict.Add("value","1");
dict.Add("value","2"); // Throws Exception (Keys must be unique)

我可以尝试传递这样的值:

www.site.com/search?value=1,2,3,4

但这是编码到 URL 的,因为

www.site.com/search?value=1%2C2%2C3%2C4

我不确定这是否是一件坏事,但是它看起来确实很糟糕。

那么,如何在 ASP.NET MVC 中将值列表作为 RouteData 传递。有什么方法可以添加到 MVC 以使其处理 int[] 吗?或者,基于字典的底层数据结构是否是一个将值列表轻松传递到 ASP.NET MVC 的阻碍?

The recommended approach for passing lists of values as a QueryString is

www.site.com/search?value=1&value=2&value=3&value=4

ASP.NET handles this well:

string value = QueryString.Get("value"); // returns "1,2,3,4"

But I can't figure out a way of passing these values by into RouteData. The obvious approach would be to add

int[] value = {1,2,3,4};

into the RouteData and have super smart MVC sort things out for me. Unfortunately MVC is dump when it comes to passing arrays into RouteData, it basically calls .ToString() adding value=int[] to my QueryString.

I tried adding the values to RouteValueDictionary (but being a dictionary can't handle this:)

RouteValueDictionary dict = new RouteValueDictionary();
dict.Add("value","1");
dict.Add("value","2"); // Throws Exception (Keys must be unique)

I could try passing values like this:

www.site.com/search?value=1,2,3,4

but this is Encoded to the URL as

www.site.com/search?value=1%2C2%2C3%2C4

I'm not sure if this is a bad thing or not,but it sure looks bad.

So, how do you pass lists of values as RouteData in ASP.NET MVC. Is there any way I can add to MVC to make it handle an int[]? Or is the underlying Dictionary-based data structure a show-stopper for passing lists of values easily to ASP.NET MVC?

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

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

发布评论

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

评论(1

拥抱我好吗 2024-08-20 03:40:23

我自己想出了一个解决方案,通过创建一个新类 RouteDataList()

public class RouteDataList<T> : List<T>
{
    public RouteDataList(IEnumerable<T> enumerable) : base(enumerable) { }

    public RouteDataList() : base() { }

    public override string ToString()
    {            
        string output = "";
        for (int i = 0; i < this.Count; i++)
        {
            output += i < this.Count - 1 ? this[i] + "-" : this[i].ToString();
        }

        return output;
    }

    public static List<Int32> ParseInts(string input)
    {
        if (String.IsNullOrEmpty(input)) return null;

        List<Int32> parsedList = new List<int>();
        string[] split = input.Split('-');
        foreach (string s in split)
        {
            int value;
            if(Int32.TryParse(s, out value)) parsedList.Add(value);
        }
        return parsedList;
    }
}

使用如下:

RouteDataList<Int32> valuelist = new RouteDataList<Int32>(){5,6,7,8};
RouteDataList<Int32> anothervaluelist = new RouteDataList<Int32>(){12,13,14,15};

然后传递给任何采用 RouteValueDictionary/Anonymous 类型的函数:

return RedirectToAction("View", "Browse", new {valuelist, anothervaluelist } );
// Produces http://www.site.com/browse/view?valuelist=5-6-7-8&anothervaluelist=12-13-14-15

// To Parse back to a list:
List<Int32> values = RouteDataList<Int32>.ParseInts(HttpContext.Current.Request.QueryString["valuelist"]) 

I've come up with a solution to this myself by making a new class RouteDataList()

public class RouteDataList<T> : List<T>
{
    public RouteDataList(IEnumerable<T> enumerable) : base(enumerable) { }

    public RouteDataList() : base() { }

    public override string ToString()
    {            
        string output = "";
        for (int i = 0; i < this.Count; i++)
        {
            output += i < this.Count - 1 ? this[i] + "-" : this[i].ToString();
        }

        return output;
    }

    public static List<Int32> ParseInts(string input)
    {
        if (String.IsNullOrEmpty(input)) return null;

        List<Int32> parsedList = new List<int>();
        string[] split = input.Split('-');
        foreach (string s in split)
        {
            int value;
            if(Int32.TryParse(s, out value)) parsedList.Add(value);
        }
        return parsedList;
    }
}

Use as follows:

RouteDataList<Int32> valuelist = new RouteDataList<Int32>(){5,6,7,8};
RouteDataList<Int32> anothervaluelist = new RouteDataList<Int32>(){12,13,14,15};

Then Pass to any function that takes a RouteValueDictionary/Anonymous Type:

return RedirectToAction("View", "Browse", new {valuelist, anothervaluelist } );
// Produces http://www.site.com/browse/view?valuelist=5-6-7-8&anothervaluelist=12-13-14-15

// To Parse back to a list:
List<Int32> values = RouteDataList<Int32>.ParseInts(HttpContext.Current.Request.QueryString["valuelist"]) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文