<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Anas Ghanem  : Tips and Tricks</title><link>http://weblogs.asp.net/anasghanem/archive/tags/Tips+and+Tricks/default.aspx</link><description>Tags: Tips and Tricks</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Putting your website in a maintanance mode based on a weekly schedule </title><link>http://weblogs.asp.net/anasghanem/archive/2008/03/30/putting-your-website-in-a-maintanance-mode-based-on-a-weekly-schedule.aspx</link><pubDate>Sun, 30 Mar 2008 19:55:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6049604</guid><dc:creator>anas</dc:creator><slash:comments>8</slash:comments><comments>http://weblogs.asp.net/anasghanem/archive/2008/03/30/putting-your-website-in-a-maintanance-mode-based-on-a-weekly-schedule.aspx#comments</comments><description>&lt;p&gt;In this blog i will talk about how to redirect the website visitors to a page that display a " web site&amp;nbsp; under maintenance"&amp;nbsp; ,&lt;/p&gt;
&lt;p&gt;you can implement this functionality in your Global application file ( &lt;a href="http://msdn2.microsoft.com/en-us/library/1xaas8a2%28VS.71%29.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/1xaas8a2(VS.71).aspx"&gt;Global.asax&lt;/a&gt; file) , &lt;/p&gt;
&lt;p&gt;you can use&amp;nbsp; Application_BeginRequest event handler which will be called when your website receives a new request ,&lt;/p&gt;
&lt;p&gt;this example will put your website in a maintenance mode if the current day is Saturday and for a one hour (from 10 -11) &lt;/p&gt;
&lt;p&gt;add the following function to Global.asax file ,&lt;/p&gt;
&lt;p&gt;(if you don't have this file in your website , in visual studio , right click&amp;nbsp; on the website , select add new Item , select global Application Class)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="width: 100%; background-color: rgb(238, 238, 238);"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub Application_BeginRequest(ByVal source As Object, ByVal e As EventArgs)&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim application As HttpApplication = CType(source, _&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpApplication)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim context As HttpContext = application.Context&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Now.DayOfWeek = DayOfWeek.Saturday AndAlso Now.Hour = 10 Then&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.RewritePath("~/UnderMaintanace.aspx")&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/p&gt;

&lt;/div&gt;&lt;br&gt;
&lt;p&gt;&amp;nbsp;adding&amp;nbsp; the above code will redirect your website users to UnderMaintanace.aspx page if the day is Saturday and if the time between 10 -11 .&lt;br&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;Note that i used &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.httpcontext.rewritepath%28VS.71%29.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(VS.71).aspx"&gt;RewritePath&lt;/a&gt; and not Response.redirect because the second one will make a new request to the website and so we will have infinite loop (until we redirect to an HTML page which will not handled by asp net runtime and the function will not get executed )&amp;nbsp;&lt;br&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;the above solution use the date and time of the week to put the website in a maintenance mode , but you can extend this to more practical scenarios ,&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;for example , you can use a settings file for your website, so that the administrator can set some flag and put the website in maintenance mode !&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Another thing you may want to do is to use http module instead of using the Global application file , so that you can have a reusable module that can be plugged&amp;nbsp; to any new website you design.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Further resources:&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/aa479332.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/aa479332.aspx"&gt;Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components &lt;br&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://support.microsoft.com/kb/307996" mce_href="http://support.microsoft.com/kb/307996"&gt;How To Create an ASP.NET HTTP Module Using Visual C# .NET&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Anas Ghanem&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6049604" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/anasghanem/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/anasghanem/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category></item><item><title>Tip/ Trick: Preventing Session Loss when using Cookieless sessions with  TreeView and Menu controls</title><link>http://weblogs.asp.net/anasghanem/archive/2008/03/08/tip-trick-preventing-session-loss-when-using-cookieless-sessions-with-treeview-and-menu-controls.aspx</link><pubDate>Sat, 08 Mar 2008 21:16:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5939756</guid><dc:creator>anas</dc:creator><slash:comments>6</slash:comments><comments>http://weblogs.asp.net/anasghanem/archive/2008/03/08/tip-trick-preventing-session-loss-when-using-cookieless-sessions-with-treeview-and-menu-controls.aspx#comments</comments><description>
&lt;p&gt;By default .net runtime uses the cookies to remember the session Id between the requests ,&lt;br __designer:dtid="16607045100765187"&gt;but when using Cookie less sessions ,the runtime inserts the Session Id to the requested url &amp;nbsp;,&lt;br __designer:dtid="16607045100765188"&gt;this way the runtime can remember the session id and prevent the session loss .&lt;br __designer:dtid="16607045100765189"&gt;&lt;br __designer:dtid="16607045100765190"&gt;The problem is , when using the Menu and TreeView Controls , these controls doesn't handle this issue ,&lt;br __designer:dtid="16607045100765191"&gt;so when those controls display there data from the Site maps, they didn't append the session id to the Navigation Urls of there Items ,&lt;br __designer:dtid="16607045100765192"&gt;and so when the User Navigate to a page using those controls , he will redirected to a Url that didn't contains the session Id,&lt;br __designer:dtid="16607045100765193"&gt;and so the runtime can't extract the session id , Hence the session will be &amp;nbsp;lost .&lt;br __designer:dtid="16607045100765194"&gt;&lt;br __designer:dtid="16607045100765195"&gt;&lt;b&gt;The Solution:&lt;/b&gt;&lt;br __designer:dtid="16607045100765196"&gt;the solution&amp;nbsp; is to Manually Append the Session Id to NavigateUrl &amp;nbsp;of the Items for those Navigation controls,&lt;br __designer:dtid="16607045100765197"&gt;we can use &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.applyapppathmodifier%28VS.71%29.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.applyapppathmodifier(VS.71).aspx"&gt;HttpContext.Current.Response.ApplyAppPathModifier&lt;/a&gt; to modify the Item Urls as Follows:&lt;br __designer:dtid="16607045100765198"&gt;&lt;br __designer:dtid="16607045100765199"&gt;For the Menu Control , we can use MenuItemDataBound Event Handler to accomplish this:&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:00fec3bb-5f7c-47ff-a057-9c9bc6fca1e6" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: White;"&gt;&lt;div&gt;&lt;!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Menu1_MenuItemDataBound(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sender, MenuEventArgs e)
 {
   &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; appened the SessionId to Menu Item URL to Avoid sessin loss&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   e.Item.NavigateUrl &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpContext.Current.Response.ApplyAppPathModifier(e.Item.NavigateUrl);
 }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;For the TreeView Control, we can use TreeNodeDataBound&amp;nbsp; to accomplish this&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ac075d79-52ef-4bad-ace8-6a8b043b65dd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: White;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TreeView1_TreeNodeDataBound(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sender, TreeNodeEventArgs e)
  {
    e.Node.NavigateUrl &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpContext.Current.Response.ApplyAppPathModifier(e.Node.NavigateUrl);
  }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Regards, 
&lt;/p&gt;

&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5939756" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/anasghanem/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/anasghanem/archive/tags/Session/default.aspx">Session</category><category domain="http://weblogs.asp.net/anasghanem/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category></item></channel></rss>