Let’s say, there is an area “Products” and two controller “Home” and “Suite” (there are more controllers that will behave in this scenario like “Suite”).
I configured the “ProductAreaRegistration.cs” so that the URLs will look like this:
\Products\
(with controller = “Home”, action = “Index”)\Products\Suite
(with controller = “Suite”, action = “Index”)\Products\Suite\TechnicalSpecification
(with controller = “Suite”, action = “TechnicalSpecification”)
The action “TechnicalSpecification” appears in every controller of that area but the “Home” controller is the one, wo actually does the main work. The other controllers like “Suite” just do some controller related stuff and then redirect to “Home”, “TechnicalSpecification”:
// Suite-Controller
public ActionResult TechnicalSpecification()
{
//doSomethingThatOnlyThisControllerHasToDo();
return RedirectToAction("TechnicalSpecification", "Home", new { specificationId = 45 });
}
// Home-Controller
public ActionResult TechnicalSpecification(int specificationId)
{
Object model = getModelByIdWithSomeMethod(specificationId);
//DoEvenMoreThingsHere();
return View(model);
}
My problem is, when I look at the link of “Suite”, “TechnicalSpecification” it shows
\Products\Suite\TechnicalSpecification
but after clicking that link the URL in the address bar changes to
\Products\TechnicalSpecification?specificationId=45
because of the redirection. I would like the URL to stay like mentioned before.
So instead of “return RedirectToAction(…)” is there something like “return ViewResultFromOtherController(…)”, so that it is not redirecting?
Hope you got the idea.
If not, please ask!
Thanks in advance!
There is probably a better way than this to do it, but you could try instantiating the other controller in your Action, calling the method, and returning its results. That would leave you with the same URL you posted to, but give you the result of the other method.
Something like:
This does beg for refactoring, though.