0

I want to know how to how to step back 2 steps on browser history using redirect inside an action in MVC controller?

I have this code on my Controller, a post action, that returns 1 step back on history:

return Redirect(Request.UrlReferrer.ToString());  

But I need to back 2 steps.

No solution on client solves my problem, because I need to use inside the Action on my controller. I hope something similar to .history(-2) on client, when using javascript. Does anyone can help me?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
ASPaiva
  • 143
  • 9
  • You know that routes map to actions right? So if you hit a URL from 2 steps ago, it will go to whatever action is mapped... Your server code does not know your browser history implicitly, you would have to do some real hacky things to even _try_ to get it. Plus the approach would change browser to browser, its not a good idea – maccettura Sep 20 '18 at 19:52
  • 1
    the server really shouldn't control the browser like that. – Daniel A. White Sep 20 '18 at 19:53
  • [This might be what you are looking for](https://stackoverflow.com/questions/37771555/redirecttoaction-how-to-go-back-2-pages-back) – Patrick Sep 21 '18 at 09:44

1 Answers1

0

Your controller is supposed to control your model and your view, not the browser.

If you need this kind of behavior you will have to keep track of the user's history inside your application. If the user is authenticated you could attach a list of their last n# of controller actions to the user object and use the information like bread crumbs. You would then redirect them as necessary from your own list. If the user is not authenticated you could get clever with session variables.

All of that will be more complicated if you have anything other then HTTP GET actions because you would have to also save any form variables and reuse them.

It might be a good idea to rethink the business logic behind needing to go back 2 pages.

Carmon Colvin
  • 58
  • 1
  • 8
  • you're completely right. I have to get a better approach when using MVC. I am very new and keep studying the basic concepts. The URL is not my best answer, like you said. I will look for the correct way to do it. Other comments remembered me that I have to use primarily the Actions on the controller, not the browser resources. Thanks for all for helping me to improve the MVC approach. – ASPaiva Oct 22 '18 at 12:49