RedirectToAction As Get 请求格式
RedirectToAction As Get Request format
我有一个操作结果,我想重定向到具有动态 ID 的操作。
return RedirectToAction("Engine", new {id = latestVersion.Id});
然而,returns 的 URL 最终是:
domain.com/project/engine/xxx
然而我需要的是:
domain.com/project/engine?id=xxx
这是我当前的路线图:
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
);
有没有办法改变在控制器级别格式化的方式?
简单回答
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
选项 1.
您可以将 id
路由参数更改为其他名称,例如
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{myId}", // URL with parameters
new { controller = "Home", action = "Index", myId = UrlParameter.Optional } // Parameter defaults
);
并在控制器(使用它们的地方)中进行相应更改,或者
选项 2.
您可以添加不带 id
参数的虚拟路由定义,例如,
routes.MapRoute(
"RouteWithoutId", // Route name
"{controller}/{action}"
);
并使用 RedirectToRoute
方法,例如
return RedirectToRoute("RouteWithoutId", new { action = "Engine", id = latestVersion.Id});
希望这对您有所帮助。
我有一个操作结果,我想重定向到具有动态 ID 的操作。
return RedirectToAction("Engine", new {id = latestVersion.Id});
然而,returns 的 URL 最终是:
domain.com/project/engine/xxx
然而我需要的是:
domain.com/project/engine?id=xxx
这是我当前的路线图:
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
);
有没有办法改变在控制器级别格式化的方式?
简单回答
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
选项 1.
您可以将 id
路由参数更改为其他名称,例如
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{myId}", // URL with parameters
new { controller = "Home", action = "Index", myId = UrlParameter.Optional } // Parameter defaults
);
并在控制器(使用它们的地方)中进行相应更改,或者
选项 2.
您可以添加不带 id
参数的虚拟路由定义,例如,
routes.MapRoute(
"RouteWithoutId", // Route name
"{controller}/{action}"
);
并使用 RedirectToRoute
方法,例如
return RedirectToRoute("RouteWithoutId", new { action = "Engine", id = latestVersion.Id});
希望这对您有所帮助。