views:

81

answers:

5

Hi,

I have an ASP.NET application. On some pages it requires a specific querystring to be called with the page, so data can be processed.

Where is the most appropriate place to check whether the required querystring is included in the URL, otherwise redirect to somewhere else?

I have only used one masterpage.

Thoughts and suggestions would be appreciated.

Thanks.

A: 

The sooner the better - why spend more time with the page than you need to if you really want to just redirect/transfer elsewhere?

Joel Coehoorn
+1  A: 

Check for the querystring on the page that uses it. You want to keep related code together where possible.

RedFilter
A: 

I do it in the Page_Load handler, though probably it should be in Page_Init().

The Master Page code does not execute until after Page_Init(), I think.

egrunin
+2  A: 

I would check in the Page_Load function or better the Page_Init function in each page which needs the query string item.

Link to ASP.NET page execution lifecycle.

protected override void Page_Init (object sender, EventArgs e)
{
   if(Request.QueryString["key1"] == "" || Request.QueryString["key1"] == null)
    {
      Response.Redirect("YOUR_PAGE_HERE");
    }
}
Kevin
A: 

You could create a custom attribute that you place on the page definition for any page that requires a query string. Then you could check for: a) the presence of that attribute on the handler (Page); and b) a non-null query string if the attribute is found. I've done similar custom attributes before. The check can go in the Global.asax.cs code, but it has to be late enough that the handler (the Page-derived class for standard web forms .aspx pages) has been identified by the asp.net runtime.

This would keep you from having to repeat the same code on any page that has the requirement.

soccerdad