Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use custom route with RedirectToAction (asp.net mvc)

I use the RedirectToAction with parameters in my action method

return RedirectToAction("ListThreads", "View", new { category = "1", subcategory = "49"});

After redirect the url comes out like this http://domain/Forum/View/ListThreads?category=1&subcategory=49

I want it to be generated like http://domain/Forum/View/ListThreads/1/49

How to do it?

Note : I already have a route in global.asax that is used by all the pages/links.

context.MapRoute(
            "Forum_subcategory",
            "Forum/{controller}/{action}/{category}/{subcategory}",
            new { controller = "View", action = "ListThreads", category = "", subcategory = "" }
        );
like image 560
maX Avatar asked Mar 08 '11 10:03

maX


2 Answers

I had a simular problem with routing in my application, to fix my problem I used: RedirectToRoute. If you change your RedirectToAction to this:

return RedirectToRoute("Forum_subcategory", new { controller = "View", action = "ListThreads", category = "1", subcategory = "49" });

You should get the result you are looking for. For more information the Controller.RedirectToRoute Method refer to: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoroute(v=vs.98).aspx

like image 119
Sean Keating Avatar answered Nov 06 '22 01:11

Sean Keating


I've seen similar issues before. As crap as it sounds, you might have to wrap your object in a RouteValueDictionary:

return RedirectToAction("ListThreads", "View", new RouteValueDictionary(new { category = "1", subcategory = "49"}));

Give it a try, see how it goes.

like image 2
OJ. Avatar answered Nov 06 '22 01:11

OJ.