I have a screen to create records in MVC. If the model validates then I want to return back to my Home screen. I have seen the following:
return new RedirectToRouteResult(
new RouteValueDictionary(
new { controller = "Access", action = "Home" }
)
);
This seems rather overkill as it specifies the controller and I don’t want to change controllers. Is there a simpler way to do this?
The default route looks like this in global.asax:
That means that the
"Home"controller will be used if none has been specified (as inhttp://yoursite/). The default action is"Index"(as inhttp://yoursite/somename/).Redirect to site home page
With this knowledge we can use
RedirectToRouteto visit the home page of the site and the controller. Let’s start with the controller:Redirect to default action of a controller
If we want to get to the default action for a controller, we can either use the action name directly as in:
or specify the controller in
RedirectToRoute:But that would generate duplicated code in each controller, so let’s move the code to our
BaseControllerinstead: