我在我的动作方法
中使用带有参数的RedirectToActionreturn RedirectToAction("ListThreads", "View", new { category = "1", subcategory = "49"});
重定向后,网址会像这样出现 http://domain/Forum/View/ListThreads?category=1&subcategory=49
我希望它像生成一样 http://domain/Forum/View/ListThreads/1/49
怎么做?
注意:我已经在global.asax中有一条路由,供所有页面/链接使用。
context.MapRoute(
"Forum_subcategory",
"Forum/{controller}/{action}/{category}/{subcategory}",
new { controller = "View", action = "ListThreads", category = "", subcategory = "" }
);
答案 0 :(得分:12)
我的应用程序中出现了类似的路由问题,以解决我使用过的问题: RedirectToRoute 。如果您将RedirectToAction更改为:
return RedirectToRoute("Forum_subcategory", new { controller = "View", action = "ListThreads", category = "1", subcategory = "49" });
你应该得到你想要的结果。有关Controller.RedirectToRoute方法的更多信息,请参阅:http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoroute(v=vs.98).aspx
答案 1 :(得分:2)
之前我见过类似的问题。听起来很糟糕,你可能需要将对象包裹在RouteValueDictionary
:
return RedirectToAction("ListThreads", "View", new RouteValueDictionary(new { category = "1", subcategory = "49"}));
试一试,看看它是怎么回事。