RedirectToAction作为“获取请求"格式 [英] RedirectToAction As Get Request format

查看:75
本文介绍了RedirectToAction作为“获取请求"格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个操作结果,我想重定向到具有动态ID的操作.

I have an action result that I want to redirect to an action with a dynamic ID.

  return RedirectToAction("Engine", new {id = latestVersion.Id});

但是,返回的URL最终为:

However, the URL that returns ends up being:

domain.com/project/engine/xxx

但是我需要的是:

domain.com/project/engine?id=xxx

这是我当前的地图路线:

Here are my current maproutes:

  routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    routes.MapRoute(
        "PrevSession", // Route name
        "{controller}/{action}/{id}/{id2}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional } // Parameter defaults
    );

是否有办法在控制器级别更改其格式化方式?

Is there a way to change the way this is formatted at the controller level?

推荐答案

选项1.

您可以将 id 路由参数更改为其他名称,例如

you can change the id route param to some other name like,

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{myId}", // URL with parameters
            new { controller = "Home", action = "Index", myId = UrlParameter.Optional } // Parameter defaults
        );

,然后在控制器(使用它们的地方)中进行相应的更改,或者

and change accordingly in the controllers(where they are used), or

选项2.

您可以添加不带 id 参数的虚拟路由定义,例如

You can add a dummy route definition without id parameter like,

routes.MapRoute(
                "RouteWithoutId", // Route name
                "{controller}/{action}" 
            );

并使用类似的 RedirectToRoute 方法

return RedirectToRoute("RouteWithoutId", new { action = "Engine", id = latestVersion.Id});

希望这会有所帮助.

这篇关于RedirectToAction作为“获取请求"格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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