user349026
user349026

Reputation:

How to route to another controller?

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

Upvotes: 0

Views: 1215

Answers (1)

Steve Michelotti
Steve Michelotti

Reputation: 5213

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 });

Upvotes: 1

Related Questions