开发者

Where is the most appropriate location to check "Querystring is needed, else redirect"?

开发者 https://www.devze.com 2023-01-03 00:46 出处:网络
I have an ASP.NET application. On some pages it requires a specific querystri开发者_高级运维ng to be called with the page, so data can be processed.

I have an ASP.NET application. On some pages it requires a specific querystri开发者_高级运维ng 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.


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");
    }
}


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


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


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.


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.

0

精彩评论

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