views:

44

answers:

1

OK I have two different controllers, say controllerA and controllerB, Now from within controllerA I have to redirect with some parameters to controllerB and inside controllerA i wrote

RedirectToAction("ControllerBAction", new { keywords = text });

How do I define the routes in global.asax.cs?

I am a newbie to MVC. Thanks for all the help

+1  A: 

First off, you don't have to define a route for this. If you leave it as is, MVC will generate a query string for you and your route will look like (given keywords text of "abc"):

/ControllerBAction?keywords=abc

If you don't want your keywords as part of the query string, then you can define a route like this:

routes.MapRoute("KeywordsRoute", "{controller}/{action}/{keywords}");

If you do this, put this before your default route. This will produce a URL that looks like this:

/ControllerBAction/abc

Update: If all you want to know is how redirect from one controller to a different controller, then you simply have to use a different overload of the RedirectToAction() method like this:

return RedirectToAction("ControllerBAction", "ControllerBName", new { keywords = text });
Steve Michelotti
I've verified that it works. When you say it's not working, what behavior are you seeing? Did you put this route *first* in your route setup in your global.asax?
Steve Michelotti
Well doesn't seem to be working. Any other way of doing it?
Wajih
Well ok let me explain a little. I have two similar named asp pages, GetResult located at Views/FolderA/results and Views/FolderB/results. Now do I still not need a route?
Wajih
Ohhh, in that case you don't have to focus on the route - just use a different overload of RedirectToAction(). I edited my answer above with code.
Steve Michelotti
I get this "No route in the route table matches the supplied values", guess I am lost completely. When a new controller is added, do we need to modify any file/assembly/global value that will tell the project that we added a new controller?
Wajih
@Wajih - It would be best if you showed not only the latest code you are using for the RedirectToAction() but also the code you have for the routes in global.asax. Again, I've verified this is working so there must be something small out of place. To answer your latest question, *if* you are using the same route pattern that is always in use, then No, you do not have to add anything else when adding a new controller. If you are trying to use a different route pattern, then you might need to code a new route in global.asax. But again, the more specifics you can show, the easier I can help.
Steve Michelotti
Accepted your answer. I was missing a parameter. Thanks anyway.
Wajih