Question

I have javascript code like this:

<a href="javascript:window.history.back()"...

When I test my website (using HTML5, MVC4) in a browser, it works fine. But when I run it in an Android/iPhone app using a embedded browser my back link doesn't work.

Is there a way to simulate a history.back using razor, like Url.Action?

Was it helpful?

Solution

You can save a previous page url in session. Something like this:

public ActionResult SomeCoolController(SomeCoolClass parameters) {
    //some logic

    var previousPageUrl = Session["PreviousPageUrl"];
    if(previousPageUrl == null)
        Session["PreviousPageUrl"] = Request.Url;

    var isTimeToChangePreviousUrl = Session["IsTimeToChangePreviousUrl"];
    if(isTimeToChangePreviousUrl != null) {
        if(isTimeToChangePreviousUrl) {
            Session["IsTimeToChangePreviousUrl"] = false;
            Session["PreviousPageUrl"] = Request.Url;
        } else {
            Session["IsTimeToChangePreviousUrl"] = true;
        }
    } else {
        Session["IsTimeToChangePreviousUrl"] = false;
    }

    //some return
}

Also to don't always copy/paste this code you can write your own SuperDupaActionResult which will be inherit from ActionResult and contain the code above (as a method for example).

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