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?

3
  • You might want to explain that you don't want to use javascript... at least I'm assuming you don't want to? Commented Jun 27, 2009 at 8: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. Commented Jun 27, 2009 at 8: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 Commented Jun 27, 2009 at 9:55

3 Answers 3

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Aha... thought about a filter too. I'll give it a try.
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. ;)
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.

2 Comments

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.
If the referrer header proves to be useful in your scenario, it can be found in Request.UrlReferrer
-2
<a href="javascript:go(-1)">Yo</a>

:)

2 Comments

<a href="javascript:history.go(-1)">Yo</a>
If the user is in a page and do some action that redirects him to the same page, this will just return him to the same page again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.