5

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • You might want to explain that you don't want to use javascript... at least I'm assuming you don't want to? – Spencer Ruport Jun 27 '09 at 08:37
  • 1
    Actually, i don't see a way how javascript could help. Even it could, i'm cautious against js if it's related with navigation cause search engines don't understand js. – Arnis Lapsa Jun 27 '09 at 08:42
  • "how does .IsAjaxRequest() method works?" - It looks for "X-Requested-With" in the request. Should work with MSAjax and jQuery. Look at ASP.NET MVC source -> AjaxRequestExtension.cs – eu-ge-ne Jun 27 '09 at 09:55

3 Answers3

5

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
  • 28,023
  • 6
  • 71
  • 62
  • Aha... thought about a filter too. I'll give it a try. – Arnis Lapsa Jun 27 '09 at 09:04
  • 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 Lapsa Jun 28 '09 at 20:37
1

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
  • 8,802
  • 8
  • 40
  • 62
  • 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 Lapsa Jun 27 '09 at 08:46
  • 1
    If the referrer header proves to be useful in your scenario, it can be found in Request.UrlReferrer – bingles Feb 24 '11 at 15:15
-2
<a href="javascript:go(-1)">Yo</a>

:)

omoto
  • 1,212
  • 1
  • 17
  • 24