Keep a query on all pages in mvc

I need to save a request on all pages of my asp.net mvc application (C #).

For example: I’ll name the page www.example.com?Preview=True . The request must be supported no matter which page I click on www.example.com . those. when I click the About us page at www.example.com , the url should be www.example.com/AboutUs?Preview=True

How can i achieve this? What is the best place for this general operation.?

+6
query-string asp.net-mvc
source share
3 answers

Great direction from @ eu-ge-ne.

I used the custom route idea from @ eu-ge-ne to add a route value to each URL and used the base controller to handle the preview key in the session.

 if ((requestContext.HttpContext.Request.QueryString != null && requestContext.HttpContext.Request.QueryString["Preview"] != null && requestContext.HttpContext.Request.QueryString["Preview"].ToString() =="True") || (requestContext.HttpContext.Request.UrlReferrer != null && requestContext.HttpContext.Request.UrlReferrer.ToString().Contains("Preview=True"))) { //Add the preview key to session } else { //Remove the preview key to session } 

I used the above code in the Initialize method of the base controller. Thus, the preview key will be in the session if querystring has Preview, otherwise it will be deleted from the session.

Thanks @ eu-ge-ne again.

0
source share

Perhaps you need a custom route ?:

 public class PreviewRoute : System.Web.Routing.Route { ... public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { var preview = System.Web.HttpContext.Current.Session["Preview"]; if (!values.ContainsKey("Preview")) values.Add("Preview", preview); var path = base.GetVirtualPath(requestContext, values); return path; } } 

}

Install Session["Preview"] at any time and you will get all your URLs using ?Preview=True :

 System.Web.HttpContext.Current.Session.Add("Preview", true); 

UPDATED:

Use this route in the Global.asax.cs file:

 routes.Add("Default", new PreviewRoute("{controller}/{action}/{id}", new MvcRouteHandler()) { Defaults = new RouteValueDictionary( new { controller = "Home", action = "Index", id = "" } ) } ); 

instead:

 routes.MapRouteLowercase( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); 

You can also try this extension:

 public static class CustomRouteExtensions { public static void MapPreviewRoute(this RouteCollection routes, string name, string url, object defaults) { routes.MapPreviewRoute(name, url, defaults, null); } public static void MapPreviewRoute(this RouteCollection routes, string name, string url, object defaults, object constraints) { if (routes == null) { throw new ArgumentNullException("routes"); } if (url == null) { throw new ArgumentNullException("url"); } var route = new PreviewRoute(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; if (String.IsNullOrEmpty(name)) { routes.Add(route); } else { routes.Add(name, route); } } } 

In Global.asax.cs:

 routes.MapPreviewRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); 
+2
source share

You can create a view helper that adds an existing query string to any links that you create using the new helper.

This may help.

You might be better off storing this information in a session.

0
source share

All Articles