How to track previous URLs in MVC

I have a main page with several filters that I want to keep when I return to this main URL. But after loading other pages, I want to return. This can be easily done by receiving @Request.UrlReferrer . However, it only works when returning to the previous page, I need this for the previous 2 pages.

I could do this using Session["ReturnToMainUrl"] = Request.UrlReferrer , but setting it only when leaving the first page.

So, if I have 3 levels:

  • WebSite \ p. 1? Filter = ABC
  • WebSite \ p. 2
  • Website \ page3

  • Now I'm on page2 or on page3, and I want to return to the site \ page1? Filter = ABC

When I'm on page 3, I can use Request.UrlReferrer to go back to page 2 , but when I go back to page 1 , I need to save the settings, so I'm loading from the session.

How can I do this in a smarter way without using sessions?

+6
source share
2 answers

You must recursively build on the query string parameter returnUrl you move from page to page.

For example: 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&paramB=bar&returnUrl=" + HttpUtility.UrlEncode(page0.ToString())); Console.WriteLine("Page 1: {0}", page1); var page2 = new Uri("http://www.example.com/page2?paramC=baz&paramD=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&paramB=bar&returnUrl=http:%2f%2fwww.example.com%2fpage0 Page 2: http://www.example.com/page2?paramC=baz&paramD=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&paramB=bar&returnUrl=http:%2f%2fwww.example.com%2fpage0 Return to page 0 from page 1 : http://www.example.com/page0 

This may continue for many levels in depth, but the process of getting the previous URL is always the same - just decode the returnUrl parameter.

+2
source

Try this solution, it works.

ClassIndex Model:

public class ClassIndex {public int Id {get; set; }}

View:

_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 = "Index"; Layout = "~ / Views / Shared / _Layout.cshtml"; }

Index @ Model.Id

Home controller

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

Occupy these variables.

IDictionary indexes = new dictionary (); INT counter = 0;

Application_BeginRequest: An event occurs when a new request is launched.

  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; } 
+2
source

Source: https://habr.com/ru/post/988060/


All Articles