Question

I don't have any code to show, because I'm not sure how to approach this. I am sending a user from one view to another where we will be doing CRUD OPERATIONS, I need to have a way of knowing what the last view I came to before the CRUD OPERATION so that I can send my user back to that view. I would also like to use this for redirection once someone has logged in. I want to have a way of setting the view I want them to get sent back to so that they can log in from any page on the site and it will remember that page instead of just dropping you on the home page.

I would accept a good tutorial as well, I'm pretty desperate to figure this out. SHoudl I just use ViewBag?

Was it helpful?

Solution

If you making <a></a> Through Html Helper then you globally set the querystring in function that can be later used for know the last view user visit.

As Balachandra mention you can use ReturnUrl inside the Request object.

Some other idea which can help you are

Request.Server["HTTP_REFERER"] 
Request.UrlReferrer

A another simple algorithm to solve this issue is making JavaScript cookie to know what is current url. and last url. it's only take 2 url to remember in cookie.

When you want to know last referrer then you can easily look in user cookie to know the referrer.

If you want to know the url refeffer in inside Action then make a ActionFilter and just call this code

HttpContext.Current.Request.UrlReferrer.ToString()

In ASP.NET MVC we have TempData which used to pass Data from Views to Views. From this post http://www.rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications You can get better explanation.

This are enough Tricks to pass the data from Views to Views. For Example TempData will help you to store current url and you can get same information on next view. Remember that TempData is based on Session so it doesn't store the information for long time. For me TempData look perfect solution that you need to use for your own solution.

OTHER TIPS

If you enable asp.net mvc authentication and try to browse any page in application, it will redirect user to login page with 'ReturnUrl' querystring parameter. This parameter holds the name of previous page from where user redirected from.

You can use similar approach even after login as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top