Hide QueryString options, how?

I have url: like this: http://www.example/about/49 .
I want this to be perceived as http://www.example/about/ , but I had to pass these parameters as a QueryString .

Is it possible?

+4
source share
2 answers

Be careful with session variables; it’s easy to open multiple pages that all use the same session and end up mixing values.

It is better to use TempData , which allows you to use the value only once (it is deleted at first access). However, this means that the value will be used almost immediately.

You can also write a cookie with the desired value, intercept the request (ASP.Net provides many ways to do this, such as the BeginRequest event), and internally handle the URL as if it contained the value,

Of course, you must clear the cookie (which will have the same problem as the session-based solution). Remember that cookies are more vulnerable to interference with the client.

Personally, I believe that any of these approaches is far more problematic than worth it. " Hackable URLs " (for example, those that contain a potentially significant identifier) ​​are generally good.

+1
source

My workaround for this (which works REALLY well, thanks to the help of the SO community)

Create a class called SiteSession.cs

Enter the following code:

 using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for SiteSession /// </summary> public class SiteSession { /// <summary> /// The _site session /// </summary> private const string _siteSession = "__SiteSession__"; /// <summary> /// Prevents a default instance of the <see cref="SiteSession" /> class from being created. /// </summary> private SiteSession() { } /// <summary> /// Gets the current Session /// </summary> /// <value>The current.</value> public static SiteSession Current { get { SiteSession session = new SiteSession(); try { session = HttpContext.Current.Session[_siteSession] as SiteSession; } catch(NullReferenceException asp) { } if (session == null) { session = new SiteSession(); HttpContext.Current.Session[_siteSession] = session; } return session; } } //Session properties public int PageNumber {get;set;} } 

You can put anything in Session Properties , just make sure to publish it.

Then install it:

SiteSession.Current.PageNumber = 42

And call him

int whatever = SiteSession.Current.PageNumber

0
source

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


All Articles