RedirectToAction As Get Request format

故事扮演 提交于 2019-12-12 02:14:31

问题


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

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

However, the URL that returns ends up being:

domain.com/project/engine/xxx

what I need however is it to be:

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:


Option 1.

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

Option 2.

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

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

and use RedirectToRoute method like,

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

hope this helps.




回答2:


Simple answer

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


来源:https://stackoverflow.com/questions/42119959/redirecttoaction-as-get-request-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!