ASP.NET MVC如何实现链接,返回到previous页? [英] ASP.NET MVC how to implement link which returns to previous page?
问题描述
标题说明了一切。
的一些背景:的
我有一个搜索机制 - 搜索视图,搜索结果视图和详细信息视图(其中重presents结果的一个项目,像web表单一个FormView)。我想详细查看链接,这将返回用户搜索结果视图。
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.
的创意的:
刚刚看了一下TempData的,但我想这不会帮助,导致用户可能他想回到之前调用一些行动。
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.
我不想使用JavaScript来实现这一目标。
I don't want to use javascript to accomplish this.
编辑:
看来,我将与EU-GE-ne`s解决方案坚持下去。这里的结果是:
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);
}
}
}
}
顺便说一句,请问.IsAjaxRequest()方法的作品?这只能理解MS AJAX或者比这更聪明?
Btw, how does .IsAjaxRequest() method works? It understands only MS AJAX or it's smarter than that?
推荐答案
我认为你需要像这样定义过滤器(未测试 - 无VS此刻):
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;
}
}
}
这篇关于ASP.NET MVC如何实现链接,返回到previous页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!