我有一个主页面,当我返回到这个主网址时,我想要保留一些过滤器。但在加载另一页后我想回去。获得@Request.UrlReferrer
可以轻松完成。但是,它仅在返回上一页时有效,我需要前两页。
我可以使用Session["ReturnToMainUrl"] = Request.UrlReferrer
来完成,但只有在离开第一页时才设置它。
所以如果我有3个级别:
网站\ PAGE3
我现在在第2页或第3页,我想回到网站\ page1?过滤器= ABC
当我在第3页时,我可以使用Request.UrlReferrer
返回第2页,但当我返回第1页时,我需要保留参数,以便我从Session加载。
如何以更智能的方式实现,而不是使用会话?
答案 0 :(得分:2)
当您从一个页面进展到另一个页面时,您应该递归地构建一个returnUrl
查询字符串参数。
例如:https://dotnetfiddle.net/HtoX6b
var page0 = new Uri("http://www.example.com/page0");
Console.WriteLine("Page 0: {0}", page0);
var page1 = new Uri("http://www.example.com/page1?paramA=foo¶mB=bar&returnUrl=" + HttpUtility.UrlEncode(page0.ToString()));
Console.WriteLine("Page 1: {0}", page1);
var page2 = new Uri("http://www.example.com/page2?paramC=baz¶mD=qux&returnUrl=" + HttpUtility.UrlEncode(page1.ToString()));
Console.WriteLine("Page 2: {0}", page2);
var page2ReturnUrl = HttpUtility.ParseQueryString(page2.Query)["returnUrl"];
Console.WriteLine("Return to page 1 from page 2: {0}", page2ReturnUrl);
var page1ReturnUrl = HttpUtility.ParseQueryString(page1.Query)["returnUrl"];
Console.WriteLine("Return to page 0 from page 1 : {0}", page1ReturnUrl);
Page 0: http://www.example.com/page0
Page 1: http://www.example.com/page1?paramA=foo¶mB=bar&returnUrl=http:%2f%2fwww.example.com%2fpage0
Page 2: http://www.example.com/page2?paramC=baz¶mD=qux&returnUrl=http:%2f%2fwww.example.com%2fpage1%3fparamA%3dfoo%26paramB%3dbar%26returnUrl%3dhttp:%252f%252fwww.example.com%252fpage0
Return to page 1 from page 2: http://www.example.com/page1?paramA=foo¶mB=bar&returnUrl=http:%2f%2fwww.example.com%2fpage0
Return to page 0 from page 1 : http://www.example.com/page0
这可以持续很多层次,但导出上一页网址的过程始终是相同的 - 只需解码returnUrl
参数。
答案 1 :(得分:2)
尝试这个解决方案,它有效。
ClassIndex模型:
public class ClassIndex {public int Id {get;组; }}
查看:
_Layout.cshtml
<body>
<a href="/Home/Back/">Back</a>
<br />
<a href="/Home/Index/1">Index 1 </a> | <a href="/Home/Index/2">Index 2 </a> | <a href="/Home/Index/3">Index 3 </a>
@RenderBody()
</body>
Index.cshtml
@model MvcApplication1.Models.ClassIndex @ { ViewBag.Title =“索引”; Layout =“〜/ Views / Shared / _Layout.cshtml”; }
Index @ Model.Id
家庭控制器
public class HomeController:Controller { string key =“ItemKey”;
public ActionResult Index(ClassIndex idx) { return View(idx); } public ActionResult Back() { var pageNumber = getPageUrl(); if (pageNumber == "/") pageNumber = "1"; return View("Index", new ClassIndex { Id = Convert.ToInt32(pageNumber) }); } string getPageUrl() { Dictionary<int, string> items = (Dictionary<int, string>)ControllerContext.RequestContext.HttpContext.Items[key]; var lastItem = items.LastOrDefault(); var returnUrl = lastItem.Value; if (string.IsNullOrEmpty(returnUrl)) returnUrl = "1"; items.Remove(lastItem.Key); return returnUrl.Replace("/Index/", ""); } }
Global.asax中
Delcare这些变数。
IDictionary indexes = new Dictionary(); INT counter = 0;
的Application_BeginRequest: 触发新请求时会发生此事件。
void Application_BeginRequest(object sender, EventArgs e)
{
var key = "ItemKey";
if (!HttpContext.Current.Request.RawUrl.Contains("Back"))
{
indexes.Add(++counter, HttpContext.Current.Request.RawUrl.Replace("/Home", ""));
}
HttpContext.Current.Items[key] = indexes;
}