在RedirectToAction来电传播查询参数 [英] Propagating QueryString parameter in RedirectToAction calls

查看:138
本文介绍了在RedirectToAction来电传播查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要确保在查询字符串特定参数,在我的情况下 REQUEST_ID 传播到重定向动作。

比方说,我有一个行动首先

  [HttpPost]
公众的ActionResult第一()
{
    ////////////////////
    // code大量的...
    ////////////////////    返回RedirectToAction(二);
}

现在说了,首先回传了一个参数查询字符串,我想传递给的行动。做到这一点的一种方法是通过在值 RedirectToAction 调用自身,

 字符串的requestId =的Request.QueryString [REQUEST_ID_KEY]
返回RedirectToAction(二,新的{REQUEST_ID_KEY =的requestId});

但我必须这样做一系列操作的,我不愿意纳入行动内请求ID传播的逻辑。它会更好,如果我可以纳入这里面的 ActionFilter ,但我不能弄清楚如何参数从ActionFilter添加到查询字符串。任何想法?


解决方案

 公共类preserveQueryStringAttribute:ActionFilterAttribute
{
    公共覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
    {
        VAR redirectResult = filterContext.Result为RedirectToRouteResult;
        如果(redirectResult == NULL)
        {
            返回;
        }        VAR的查询= filterContext.HttpContext.Request.QueryString;
        //注:在这里,你可以决定是否要传播所有
        //查询字符串值或特定的一个。在我的例子,我
        //传播的已经不是一部分的所有查询字符串值
        //路线值
        的foreach(在query.Keys字符串键)
        {
            如果(!redirectResult.RouteValues​​.ContainsKey(键))
            {
                redirectResult.RouteValues​​.Add(键,查询[关键]);
            }
        }
    }
}

和则:

  [HttpPost]
[preserveQueryString]
公众的ActionResult第一()
{
    ////////////////////
    // code大量的...
    ////////////////////    返回RedirectToAction(二);
}

I want to make sure that a particular parameter in the QueryString, in my case the request_id is propagated to the redirected action.

Say for example, I have an Action First,

[HttpPost]
public ActionResult First() 
{
    ////////////////////
    // Lots of code ...
    ////////////////////

    return RedirectToAction("Second");
}

Now say, the First postback had a parameter in the QueryString, which I would like to pass to the Second action. One way to do it would be to pass the value in the RedirectToAction call itself,

string requestId = Request.QueryString[REQUEST_ID_KEY];
return RedirectToAction("Second", new { REQUEST_ID_KEY = requestId });

But I have to do this in a series of Actions and I am unwilling to incorporate request id propagation logic inside the action. It would be better if I could incorporate this inside an ActionFilter, but I cant figure out how to add parameters to the QueryString from an ActionFilter. Any ideas?

解决方案

public class PreserveQueryStringAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var redirectResult = filterContext.Result as RedirectToRouteResult;
        if (redirectResult == null)
        {
            return;
        }

        var query = filterContext.HttpContext.Request.QueryString;
        // Remark: here you could decide if you want to propagate all
        // query string values or a particular one. In my example I am
        // propagating all query string values that are not already part of
        // the route values
        foreach (string key in query.Keys)
        {
            if (!redirectResult.RouteValues.ContainsKey(key))
            {
                redirectResult.RouteValues.Add(key, query[key]);
            }
        }
    }
}

and then:

[HttpPost]
[PreserveQueryString]
public ActionResult First() 
{
    ////////////////////
    // Lots of code ...
    ////////////////////

    return RedirectToAction("Second");
}

这篇关于在RedirectToAction来电传播查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆