问题描述:

This question already has an answer here:

  • Passing querystrings to RedirectToRouteResult (beside controller and action)

    1 answer

网友答案:

You can pull the request query string parameters straight from the HttpContext inside the filterContext. Then, if there is a value in the route collection that doesn't fit into the matched route, it's added to the query string when the URL is generated.

Knowing all of that, you can enumerate on the query string collection and add them to your RouteValueDictionary. From your example code, the resulting code might look like:

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);
相关阅读:
Top