0

When I navigate to /Controller/Action in ASP.NET MVC, the action returns a View and the browser URL is updated. How could I keep the URL intact but return the requested View at the same time?

For example, /Home/Index would return the View for Index whereas /Home/SignUp would return a different View. I want to make sure after both calls, the URL stays the same.

Mark13426
  • 2,569
  • 6
  • 41
  • 75
  • In your words, and in web, "call" means requesting resource by specifying "url". There are no "both calls" if there's single url. You could use [Ajax](http://en.wikipedia.org/wiki/Ajax_(programming)) to request resources from server without updating page and url – archil Oct 03 '12 at 08:40

3 Answers3

0

You could explicitly specify the view you want to return in the controller action:

return View("~/Views/SomeController/SomeView.cshtml");
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I don't think that would execute the action method though. I think it would simply return the View. I'm setting ViewBag properties in the action. – Mark13426 Oct 03 '12 at 08:41
  • No, this will not execute the action. Since you will be returning a different view you could set the required ViewBag properties in this action. – Darin Dimitrov Oct 03 '12 at 08:44
0

These would be GET calls and this behaviour is intrinsic.

If you want to stay on the same page or even have a single page application then you need to consider using ajax and http POST to get the different views you need to build up your page.

dove
  • 20,469
  • 14
  • 82
  • 108
0

You can achieve that by performing several approaches:

1. Configure your route config

 routes.MapRoute(
                name: null,
                url: "Home/FirstMethod",
                defaults: new { controller = "Home", action = "FirstMethod" }
            );

            routes.MapRoute(
                name: null,
                url: "Home/SecondMethod",
                defaults: new { controller = "Home", action = "FirstMethod" }
            );

2. Using custom MVCTransferResult: How to simulate Server.Transfer in ASP.NET MVC?

3. You can specifiy view explictly, for example:

return View(viewName: "Contact");
Community
  • 1
  • 1
testCoder
  • 7,155
  • 13
  • 56
  • 75