我想在我的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"],
})
);
答案 0 :(得分: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);