How to get last page visited in a controller - asp.net-mvc

is there a way to get or store the last page visited? Example if I'm on a List Page with a New link that loads a page with form. If the user cancels, you go back to the previous page (List Page).
Thanks

Well, in web forms, you would use: Request.UrlReferrer
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
So I assume you can also use that in MVC, accessible through the HttpContext property of the RequestContext acessible by teh controller.

Related

Browser back goes two JSF pages back

I have a JSF page with a form. The submit button is an <h:commantButton> with an action that calls a backing bean function, which in turn does some calculations and then returns a string which is the name of the next page to view.
This works as expected. On this page is an <h:link> with an outcome of another JSF page. When on this third page if the user clicks the browser back button they are taken to the first page with the form and not the second page. Why is this? I should mention that the backing bean is session scoped.
Thanks,
Doug
This is normal, if you verify your URL, you will see that it doesn't change when you click on the h:commandButton. If you want it to change correctly in the first place, you can modify your action like this : youraction?faces-redirect=true.
More info :
JSF : Page Forward vs Page Redirect

Determining the previous page in mvc 3

How to check the previous page in mvc 3 application.
Previous Page.
On click of the above link I have to go back to previous page.
How to do this ?
This will depend on how is the navigation organized on your website. One possibility is to use the history.go(-1) javascript function which will simply simulate the browser back button:
Previous Page.
Another possibility is to have the calling page pass a ReturnUrl query string parameter to this page which could be used to construct the link:
Previous Page.
Of course this assumes that when you called the controller action that rendered this view you have passed the ReturnUrl query string parameter.
Same as Matthew's answer but using a session variable. That way you could update it selectively in the Action you want. For example, on a POST action you wouldn't want them to go back to that view with form values there. What you really want is for them to go back to the page before that.
public ActionResult MyNextPage(string prevUrl)
{
Session["prevUrl "] = prevUrl;
View();
}
Then in the View:
Previous Page
Note that if session is expired or null it will throw an exception.

ASP.NET MVC 2 Returning to previous screen

Is there a way to return to the previous view without having to record all the parameters that were used to get to the view in question. Consider this situation. You have a search screen with input parameters, you press search and get results displayed on the page. You click on one item to get a detailed look which redirects you to a new view. Does MVC have the ability to get the previous query string that contains the search parameters?
Not that I know of. You could save it in the session, but how about using JS history.back()
Wouldn't the user just use the back button on the browser? Are you wanting to present a "Back to results" link on the detail page? Also look at Request.UrlReferrer if you don't want to save the search parameters in session or a cookie.
You can use Request.UrlReferrer

asp.net mvc: pass information between 2 controllers to the view

hope i can explain myself...
i have a login control in the masterpage. when you click the login button you go to the accountcontroller's logon method which checks your credentials. whether it is ok or not, you will be redirected to the homepage with a redirecttoaction("home","index").
but, in case login failed, i want to show a message to the user.
so what i tried was setting viewdata in the logon method (viewdata["logon"] = "failed") and then check in the masterpage if this viewdata was present and if so, render a span with some text.
but as i understand the viewdata is not preserved with the redirect to action method.
what is the best way to make sure my logon method can somehow notify my masterpage view that login failed?
Michel
If I read your question correctly your problem is that you need to set a value in your action that need to be available after RedirectToAction. You would need to set a key in Tempdata.
TempData["MessageToUser"] = "I dont let you in dude!"
TempData is still available after one redirect.
Check out MvcContrib. Queen3 somewhere mentioned that they have cure for this
(passing information between redirects).
But it would be better to create separate view for authentication. Then problem would just disappear.

Post/Redirect/Get: Redirect to specific route

I have the following scenario:
I have an edit page, which can be called from different pages. These pages could be the detail view for the current entity, or the list view for the entities (with or without a search in the route).
HOW do I cleanly redirect to the original calling page using the MVC framework? Of course I could simply pass the HttpContext.Request.Url value by holding it in my TempData, but that sort of smells, in my eyes (or, err, nose). It's on a lower level than the rest of the code.
Is there a way to get the routevalues for the previous page in a controller context? If I have that, I could store that temporarily and pass that to the redirect.
Do not use TempData when not redirecting. One AJAX request from your edit page, and the TempData will go away.
Tomas is right that a hidden element or query string parameter is the way to go. But make sure you sanitize the value submitted. You don't want to redirect any old site on the web; you need to ensure that the page to which you redirect is part of your sites.
you can always have a hidden form element telling the controller where to redirect when posting a form. when using a get request, you could use a querystring in a similar way. it might not be the most beautiful solution, but it's quite a lot safer than trusting httpreferrer or other headers that could easily be changed (or ommitted) by the browser.

Resources