ASP.NET MVC - 使用 RedirectToAction 传递当前 GET 参数

发布于 2024-11-11 16:22:47 字数 589 浏览 0 评论 0原文

我正在寻找一种在传递当前请求的 GET 参数时使用 RedirectToAction 的方法。

所以去之后: http://mydomain.com/MyController/MyRedirectAction?somevalue=1234

然后我想要使用重定向来重定向和保留某个值,而无需显式构建路由字典并显式设置某个值

public ActionResult MyRedirectAction()
{
    if (SomeCondition) 
        RedirectToAction("MyAction", "Home");
}

重定向操作可以使用某个值(如果可用):

public ActionResult MyAction()
{
    string someValue = Request.QueryString["somevalue"];
}

是否有一种干净的方法可以做到这一点?

I'm looking for a way to use RedirectToAction while passing along the current request's GET parameters.

So upon going to:
http://mydomain.com/MyController/MyRedirectAction?somevalue=1234

I would then want to redirect and persist somevalue with the a redirect without having to explicitly build a route dictionary and explicitly setting somevalue

public ActionResult MyRedirectAction()
{
    if (SomeCondition) 
        RedirectToAction("MyAction", "Home");
}

The redirected action could then use somevalue if available:

public ActionResult MyAction()
{
    string someValue = Request.QueryString["somevalue"];
}

Is there a clean way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-11-18 16:22:47

自定义操作结果可以完成这项工作:

public class MyRedirectResult : ActionResult
{
    private readonly string _actionName;
    private readonly string _controllerName;
    private readonly RouteValueDictionary _routeValues;

    public MyRedirectResult(string actionName, string controllerName, RouteValueDictionary routeValues)
    {
        _actionName = actionName;
        _controllerName = controllerName;
        _routeValues = routeValues;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var requestUrl = context.HttpContext.Request.Url;
        var url = UrlHelper.GenerateUrl(
            "", 
            _actionName, 
            _controllerName, 
            requestUrl.Scheme,
            requestUrl.Host,
            null,
            _routeValues, 
            RouteTable.Routes, 
            context.RequestContext, 
            false
        );
        var builder = new UriBuilder(url);
        builder.Query = HttpUtility.ParseQueryString(requestUrl.Query).ToString();
        context.HttpContext.Response.Redirect(builder.ToString(), false);
    }
}

然后:

public ActionResult MyRedirectAction()
{
    return new MyRedirectResult("MyAction", "Home", null);
}

A custom action result could do the job:

public class MyRedirectResult : ActionResult
{
    private readonly string _actionName;
    private readonly string _controllerName;
    private readonly RouteValueDictionary _routeValues;

    public MyRedirectResult(string actionName, string controllerName, RouteValueDictionary routeValues)
    {
        _actionName = actionName;
        _controllerName = controllerName;
        _routeValues = routeValues;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var requestUrl = context.HttpContext.Request.Url;
        var url = UrlHelper.GenerateUrl(
            "", 
            _actionName, 
            _controllerName, 
            requestUrl.Scheme,
            requestUrl.Host,
            null,
            _routeValues, 
            RouteTable.Routes, 
            context.RequestContext, 
            false
        );
        var builder = new UriBuilder(url);
        builder.Query = HttpUtility.ParseQueryString(requestUrl.Query).ToString();
        context.HttpContext.Response.Redirect(builder.ToString(), false);
    }
}

and then:

public ActionResult MyRedirectAction()
{
    return new MyRedirectResult("MyAction", "Home", null);
}
很糊涂小朋友 2024-11-18 16:22:47

如果您使用 RedirectToRoute 传递参数,它们将在 URL 中显示为查询字符串。

我不是 ASP .NET MVC 框架方面的专家,但实现此目的的一种方法是将数据放入 TempData 字典中。

TempData["your key"] = someData;

您重定向到的操作应该知道检查此数据,当然还要处理它不存在的情况。该数据不会在重定向之后继续存在——它只适用于一个请求。

如果您不喜欢对键使用字符串文字,则可以为 TempData 键创建常量。

If you pass the parameters, using RedirectToRoute, they will appear as a query string in the URL.

I'm not an expert on the ASP .NET MVC framework, but one way to achieve this is to put your data into the TempData dictionary.

TempData["your key"] = someData;

The action to which you are redirecting should know to check for this data, and of course handle the case where it does not exist. That data won't live beyond the redirection -- it is only good for one request.

You can create constants for your TempData keys if you don't like using string literals for your keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文