ASP MVC RedirectToRouteResult包含来自请求的所有get查询参数

Arb*_*æde 0 c# asp.net asp.net-mvc

我想在我的ActionFilter中进行重定向,并在请求中显示所有get参数.我应该如何添加到我的代码?

 var routeData = filterContext.RouteData;
     filterContext.Result = new RedirectToRouteResult(
                                 new RouteValueDictionary(
                                     new
                                    {
                                         culture = code,
                                         controller = routeData.Values["controller"],
                                         action = routeData.Values["action"],
                                         id = routeData.Values["id"],

                                     })
                                );
Run Code Online (Sandbox Code Playgroud)

Ste*_*n V 8

您可以直接从HttpContext内部提取请求查询字符串参数filterContext.然后,如果路径集合中的值不适合匹配的路由,则在生成URL时将其添加到查询字符串中.

知道所有这些,您可以枚举查询字符串集合并将它们添加到您的RouteValueDictionary.从您的示例代码中,生成的代码可能如下所示:

var routeData = filterContext.RouteData;

var routeValueDictionary =
    new RouteValueDictionary(
        new
        {
            culture = code,
            controller = routeData.Values["controller"],
            action = routeData.Values["action"],
            id = routeData.Values["id"],
        });

var queryString = filterContext.HttpContext.Request.QueryString;

foreach (var key in queryString.AllKeys)
{
    routeValueDictionary.Add(key, queryString[key]);
}

filterContext.Result = new RedirectToRouteResult(routeValueDictionary);
Run Code Online (Sandbox Code Playgroud)