开发者

Where to keep a querystring parameter in session?

Users will get to my site using a specific parameter, e.g. : http://www.mysite.com/whatever/?keepTrackOfThisValue=foo or http://www.mysite.com/who/cares/?keepTrackOfThisValue=bar

I would like to store the value of this peculiar parameter in Session everytime I found it in the QueryString. I'm currently using the Session_Start event in Global.asax in order to store this but I would like to override the value each time the parameter value change, which is not possib开发者_Python百科le my way.

Where would you do this ?


Try moving it from Session_Start to Application_BeginRequest


There are [at least] two ways you could go about this...

1) Use an HttpModule to hook in to the application's BeginRequest method, as outlined in this page.

2) Create a base class (inheriting from System.Web.Page) from which all the pages in your application inherit. Then use the Page_Load method in this base class to push the same functionality into all your pages. This also allows you to add any other common functionality you may need across all your pages on Load or in any other event. You can also use this to define common properties to all your pages.

I've never tried #1, but I use #2 in EVERY web application I write. For example, in my PageBase class, I provide a read-only base property which exposes a database-connection-getting mechanism, so each page has easy access to the db without needing to write it's own code for getting a connection from the pool. It can result in MUCH cleaner code overall.


It would depend on how I was using it, but one thing I've done similarly is a small wrapper property on a static class, which, when the getter was called, would optionally set the Session if the querystring was different, and then return the session


Do it during the page_load on every page where this querystring could occur

if(Session["keyName"] != null)
{
  Session["permitError"] = querysting;
}
else
{
   Session.Add("keyName", value);
}


string StrQuery;

StrQuery=Request.QueryString["value"].ToString();

Session["SessionValue"]=StrQuery;


Why don't you just store it on the Page_Load event if it exists? If it doesn't refer to the Session value.

You could even create a base page that all your pages inheirit from that checks for the existance of the QueryString you are interested in.

A MasterPage with a a public property to access this value would also be useful such as:

// in your master page
public string YourSessionVariable
{
    get
    {
        string yourValue = "SomeDefault";  // if session expired and param not passed
        if (Request.QueryString["yourQueryParamName"] != null)
        {
            Session["yourSessionParamName"] = Request.QueryString["yourQueryParamName"];
        }
        return (Session["yourSessionParamName"] == null) ?
            yourValue : Session["yourSessionParamName"].ToString();
    }
}

// Then in your pages your can just fetch it anytime with
Master.YourSessionVariable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜