asp.net-mvc – 使用多值键创建RouteValueDictionary

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用多值键创建RouteValueDictionary前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想返回一个RedirectToRouteResult,将用户发送到如下所示的URL:
/MyController/MyAction/id?newid=3&newid=5&newid=7

newid参数有几个值.

我的电话看起来像:return RedirectToAction(string.Empty,routeValues);

这是我迄今为止所尝试过的,这不行:

// ...?newid%5B0%5D=3&newid%5B1%5D=5&newid%5B2%5D=7
var routeValues = new RouteValueDictionary {
    {"id",myid},{"newid[0]",3},{"newid[1]",5},{"newid[2]",7},};

// ...?newid=System.Int32%5B%5D
var routeValues = new { id = myid,newid = new int[] { 3,5,7 } };

// ...?newid=System.String%5B%5D
var routeValues = new { id = myid,newid = new string[] { "3","5","7" } };

// ...?newid=System.Int32%5B%5D
var routeValues = new RouteValueDictionary {
    {"id",{"newid",new int[] { 3,7 } }
};

使这项工作有什么秘密?

解决方法

这是框架中真正缺失的一件事.你最好的办法是手动滚动它:
public ActionResult Foo()
{
    var ids = new[] { 3,7 };
    var url = new UriBuilder(Url.Action("MyAction","MyController",new { id = "123" },Request.Url.Scheme));
    url.Query = string.Join("&",ids.Select(x => "newid=" + HttpUtility.UrlEncode(x.ToString())));
    return Redirect(url.ToString());
}

将其放入自定义扩展方法中可以增加可读性.

原文链接:https://www.f2er.com/aspnet/250273.html

猜你在找的asp.Net相关文章