views:

1047

answers:

4

Title said it all.

Some context:
I got a search mechanism - search view, search results view and a details view (which represents one item of results, like a formview in webforms). I want a link in details view, which would return user to search results view.

Ideas:
Just read about TempData, but i guess that wouldn't help, cause user might call some actions before he wants to return.

Session might work, but I'm not sure how exactly i should handle it.

I don't want to use javascript to accomplish this.

Edit:
Seems that i'll stick with eu-ge-ne`s solution. Here's result:

#region usages

using System.Web.Mvc;
using CompanyName.UI.UIApp.Infrastructure.Enums;

#endregion

namespace CompanyName.UI.UIApp.Infrastructure.Filters
{
    /// <summary>
    /// Apply on action method to store URL of request in session
    /// </summary>
    public class RememberUrlAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting  
           (ActionExecutingContext filterContext)
        {
            var httpContext = filterContext.HttpContext;

            if (httpContext.Request.RequestType == "GET"
                && !httpContext.Request.IsAjaxRequest())
            {
                SessionManager
                .Save(SessionKey.PreviousUrl,
                      SessionManager.Get(SessionKey.CurrentUrl) ??
                      httpContext.Request.Url);

                SessionManager
                .Save(SessionKey.CurrentUrl,
                      httpContext.Request.Url);
            }
        }
    }
}

Btw, how does .IsAjaxRequest() method works? It understands only MS AJAX or it's smarter than that?

A: 

You can examine the HTTP Referrer header to retrieve the previous URL.

Of course, you'll have to handle gracefully just in case the user does not pass in this value.

Adrian Godong
What if user does something at details page? He might call some actions there. That would make this examination and remembering of Referrer header quite tricky.
Arnis L.
+1  A: 

I think you need something like this custom filter (not tested - have no VS at the moment):

public class PrevUrlAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var httpContext = filterContext.HttpContext;
        var session = filterContext.HttpContext.Session;

        if (httpContext.Request.RequestType == "GET"
            && !httpContext.Request.IsAjaxRequest())
        {
            session["PrevUrl"] = session["CurUrl"] ?? httpContext.Request.Url;
            session["CurUrl"] = httpContext.Request.Url;
        }
    }
}
eu-ge-ne
Aha... thought about a filter too. I'll give it a try.
Arnis L.
One more issue - yours filter picks up previous URL, not current one (i named filter as RememberUrl, so - it's supposed to remember current URL). But that's already peace of cake to fix up. ;)
Arnis L.
A: 

This is a duplicate (in execution at least) of this question:

http://stackoverflow.com/questions/815229/how-do-i-redirect-to-the-previous-action-in-asp-net-mvc/815272#815272

Nathan Ridley
Somehow didn't find it.
Arnis L.
And those answers aren't exactly what i'm looking for.
Arnis L.
A: 
<a href="javascript:go(-1)">Yo</a>

:)

omoto
<a href="javascript:history.go(-1)">Yo</a>
Seba Illingworth