<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>网来网去-专注Google&#124;苹果&#124;云服务&#124;智能设备 &#187; ASP.net</title>
	<atom:link href="http://www.webcomgo.com/archives/category/asp-net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webcomgo.com</link>
	<description>专注Google&#124;苹果&#124;阿里巴巴&#124;互联网&#124;智能终端&#124;云计算&#124;电子商务&#124;平板设备</description>
	<lastBuildDate>Sat, 31 Jul 2010 17:52:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ASP.NET 2.0防止同一用户同时登陆</title>
		<link>http://www.webcomgo.com/archives/3695</link>
		<comments>http://www.webcomgo.com/archives/3695#comments</comments>
		<pubDate>Fri, 07 May 2010 05:39:38 +0000</pubDate>
		<dc:creator>网来网去</dc:creator>
				<category><![CDATA[ASP.net]]></category>

		<guid isPermaLink="false">http://www.webcomgo.com/?p=3695</guid>
		<description><![CDATA[<br/>要防止同一用户同时登陆,首页应该记录在线用户的信息(这里与用户名为例),然后判断正在登陆的用户里面是否已存在。在这里使用一个cache存放已经登陆的用户名，但是还有一个问题就是要... ]]></description>
			<content:encoded><![CDATA[<br/><p>要防止同一用户同时登陆,首页应该记录在线用户的信息(这里与用户名为例),然后判断正在登陆的用户里面是否已存在。<span id="more-3695"></span>在这里使用一个cache存放已经登陆的用户名，但是还有一个问题就是要知道用户是什么时候离开系统的呢？这就要定期清除cache中的内容了，也就是设置一个cache的时间。这个时间可以跟用户的session值联系起来，刚好当用户session值失效的时候该用户在cache里面的信息也会被清空．这样就达到了防止同时登陆的效果，具体代码如下：<br />
　　放在登陆成功的地方：</p>
<p>以下是引用片段：<br />
string key = TextBox1.Text; //用户名文本框设为cache关键字 <br />
string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值<br />
//判断cache中是否有用户的信息，如果没有相关的值，说明用户未登陆<br />
if (uer == null || uer == String.Empty)<br />
{<br />
　//定义cache过期时间<br />
　TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);<br />
　//第一次登陆的时候插入一个用户相关的cache值，<br />
　HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);<br />
　Session["ADMINID"] = TextBox1.Text;<br />
　Response.Redirect(&#8220;main.ASPx&#8221;);<br />
}<br />
else<br />
{<br />
　//重复登陆<br />
　Response.Write(&#8220;&#8221;);<br />
}</p>
<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="http://www.webcomgo.com/archives/3695"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div><p  class="related_post_title">相关文章</p><ul class="related_post"><li><a href="http://www.webcomgo.com/archives/2153" title="使用ASP.NET Global.asax 文件">使用ASP.NET Global.asax 文件</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.webcomgo.com/archives/3695/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>先看看ASP.NET页面刷新的实现方法：</title>
		<link>http://www.webcomgo.com/archives/3011</link>
		<comments>http://www.webcomgo.com/archives/3011#comments</comments>
		<pubDate>Fri, 26 Feb 2010 00:30:26 +0000</pubDate>
		<dc:creator>网来网去</dc:creator>
				<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[页面刷新]]></category>

		<guid isPermaLink="false">http://www.webcomgo.com/archives/3011</guid>
		<description><![CDATA[<br/>先看看ASP.NET页面刷新的实现方法： 第一： C# code private void Button1_Click( object sender, System.EventArgs e ) { Response.Redirect( Request.Url.ToString( ) ); } 第二： C# code private void Button2_Click( object sender, System.Ev... ]]></description>
			<content:encoded><![CDATA[<br/><p>先看看ASP.NET页面刷新的实现方法：</p>
<p>第一：</p>
<dl class="code">
<dt>C# code </dt>
<dd>
<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Button1_Click( </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e )
{
  Response.Redirect( Request.Url.ToString( ) );
} </span></div>
</pre>
</dd>
</dl>
<p><span id="more-3011"></span>第二：</p>
<dl class="code">
<dt>C# code </dt>
<dd>
<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Button2_Click( </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e )
{
Response.Write( </span><span style="color: #800000;">"</span><span style="color: #800000;"> &lt; script language=javascript&gt;window.location.href=document.URL; &lt; /script&gt;</span><span style="color: #800000;">"</span><span style="color: #000000;"> );
} </span></div>
</pre>
</dd>
</dl>
<p>第三：</p>
<dl class="code">
<dt>C# code </dt>
<dd>
<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Button3_Click( </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e )
{
Response.AddHeader( </span><span style="color: #800000;">"</span><span style="color: #800000;">Refresh</span><span style="color: #800000;">"</span><span style="color: #000000;">,</span><span style="color: #800000;">"</span><span style="color: #800000;">0</span><span style="color: #800000;">"</span><span style="color: #000000;"> );
} </span></div>
</pre>
</dd>
</dl>
<p>第四：</p>
<dl class="code">
<dt>C# code </dt>
<dd>
<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Button6_Click( </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, System.EventArgs e )
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">好像有些不对？
</span><span style="color: #008000;">//</span><span style="color: #008000;">Response.Write( " &lt; script language=javascript&gt;window.location.reload( ); &lt; /script&gt;" ); }</span></div>
</pre>
</dd>
</dl>
<p>第五：</p>
<dl class="code">
<dt>HTML code </dt>
<dd>
<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000ff;">&lt;</span><span style="color: #800000;"> script</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;"> !-- </span><span style="color: #ff0000;">var limit</span><span style="color: #0000ff;">="3:00"</span><span style="color: #ff0000;"> if ( document.images ) { var parselimit</span><span style="color: #0000ff;">=limit.split( </span><span style="color: #ff0000;">":" )parselimit</span><span style="color: #0000ff;">=parselimit[0]*60+parselimit[1]*1 </span><span style="color: #ff0000;">} function beginrefresh( ) { if ( !document.images )returnif ( parselimit</span><span style="color: #0000ff;">==1 </span><span style="color: #ff0000;">)window.location.reload( )else { parselimit-</span><span style="color: #0000ff;">=1curmin=Math.floor( </span><span style="color: #ff0000;">parselimit/60 )cursec</span><span style="color: #0000ff;">=parselimit%60if </span><span style="color: #ff0000;">( curmin!</span><span style="color: #0000ff;">=0 </span><span style="color: #ff0000;">)curtime</span><span style="color: #0000ff;">=curmin+"分"+cursec+"秒后重刷本页！"elsecurtime=cursec+"秒后重刷本页！"window.status=curtimesetTimeout( </span><span style="color: #ff0000;">"beginrefresh( )",1000 ) } } window.onload</span><span style="color: #0000ff;">=beginrefresh//--</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;"> /script</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;"> DIV </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="Z-INDEX: 102; LEFT: 408px; POSITION: absolute; TOP: 232px"</span><span style="color: #ff0000;"> ms_positioning</span><span style="color: #0000ff;">="text2D"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;"> P</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;"> FONT </span><span style="color: #ff0000;">size</span><span style="color: #0000ff;">="3"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">自动刷新页面</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;"> /FONT</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;"> /P</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"> </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;"> /DIV</span><span style="color: #0000ff;">&gt;</span></div>
</pre>
</dd>
</dl>
<p>第六：<br />
&lt; meta http-equiv=&#8221;refresh&#8221; content=&#8221;300; url=target.html&#8221;&gt; 用window.location.href实现刷新另个框架页面</p>
<p>在写asp.net程序的时候，我们经常遇到跳转页面的问题，我们经常使用Response.Redirect ，如果客户要在跳转的时候使用提示，这个就不灵光了，如：</p>
<p>Response.Write(&#8221; &lt; script&gt;alert(&#8216;恭喜您，注册成功！&#8217;); &lt; /script&gt;&#8221;); Response.Redirect(&#8220;main.html&#8221;); 这时候我们的提示内容没有出来就跳转了，和Response.Redirect(&#8220;main.html&#8221;); 没有任何区别。</p>
<p>这时我们采用下面代码试验一下ASP.NET页面刷新：</p>
<p>Response.Write(&#8221; &lt; script language=javascript&gt;alert(&#8216;恭喜您，注册成功！&#8217;) &lt; /script&gt;&#8221;); Response.Write(&#8221; &lt; script language=javascript&gt;window.location.href=&#8217;main.html&#8217; &lt; /script&gt;&#8221;); 这个即实现了我们的要求，在提示后，跳转页面。</p>
<p>最重要的是window.location.href 语句可以实现一个框架的页面在执行服务器端代码后刷新另一个框架的页面(Response.Redirect无法达到，至少我没有发现)：</p>
<p>如：index.htm页面中有二个框架，分别为 frameLeft和frameRight，在frameRight页面中执行服务器端代码后刷新frameLeft中的页面。</p>
<p>先前最常见的是注册之后，自动刷新登陆框，让登陆框换成已登陆页面，只要在注册成功的代码之后加上一段，即可以实现刷新另个框架的页面。代码如下：</p>
<p>Response.Write(&#8221; &lt; script language=javascript&gt;alert(&#8216;恭喜您，注册成功！&#8217;) &lt; /script&gt;&#8221;); Response.Write(&#8221; &lt; script language=javascript&gt;window.parent.frameLeft.location.href=&#8217;main.html&#8217; &lt; /script&gt;&#8221;); ASP.NET页面刷新：自动刷新页面的实现方法总结:</p>
<p>1)</p>
<p>&lt; meta http-equiv=&#8221;refresh&#8221;content=&#8221;10; url=跳转的页面&#8221;&gt; 10表示间隔10秒刷新一次</p>
<p>2)</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; window.location.reload(true); &lt; /script&gt; 如果是你要刷新某一个iframe就把window给换成frame的名字或ID号</p>
<p>3)</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; window.navigate(&#8220;本页面url&#8221;); &lt; /script&gt; 4&gt;</p>
<p>function abc() { window.location.href=&#8221;/blog/window.location.href&#8221;; setTimeout(&#8220;abc()&#8221;,10000); } 刷新本页：</p>
<p>Response.Write(&#8221; &lt; script language=javascript&gt;window.location.href=window.location.href; &lt; /script&gt;&#8221;) 刷新父页：</p>
<p>Response.Write(&#8221; &lt; script language=javascript&gt;opener.location.href=opener.location.href; &lt; /script&gt;&#8221;) 转到指定页:</p>
<p>Response.Write(&#8221; &lt; script language=javascript&gt;window.location.href=&#8217;yourpage.aspx&#8217;; &lt; /script&gt;&#8221;)<br />
刷新页面实现方式总结(HTML,ASP,JS)</p>
<p>&#8216;by aloxy</p>
<p>定时刷新：</p>
<p>1，</p>
<p>&lt; script&gt;setTimeout(&#8220;location.href=&#8217;url&#8217;&#8221;,2000) &lt; /script&gt; 说明：url是要刷新的页面URL地址</p>
<p>2000是等待时间=2秒，</p>
<p>2，</p>
<p>&lt; meta name=&#8221;Refresh&#8221; content=&#8221;n; url&#8221;&gt; 说明：</p>
<p>n is the number of seconds to wait before loading the specified URL.</p>
<p>url is an absolute URL to be loaded.</p>
<p>n,是等待的时间，以秒为单位</p>
<p>url是要刷新的页面URL地址</p>
<p>3，</p>
<p>&lt; %response.redirect url%&gt; 说明：一般用一个url参数或者表单传值判断是否发生某个操作然后利用response.redirect 刷新。</p>
<p>4，刷新框架页</p>
<p>〈script language=javascript&gt;top.leftFrm.location.reload(); parent.frmTop.location.reload(); &lt; /script〉 弹出窗体后再刷新的问题</p>
<p>Response.Write(&#8221; &lt; script&gt;window.showModalDialog(&#8216;../OA/SPCL.aspx&#8217;,window,&#8217;dialogHeight: 300px; dialogWidth: 427px; dialogTop: 200px; dialogLeft: 133px&#8217;) &lt; /script&gt;&#8221;); //open Response.Write(&#8221; &lt; script&gt;document.location=document.location; &lt; /script&gt;&#8221;); 在子窗体页面代码head中加入 &lt; base target=&#8221;_self&#8221;/&gt;</p>
<p>刷新的内容加在 if (!IsPostBack) 中</p>
<p>在框架页中右面刷新左面</p>
<p>//刷新框架页左半部分 Response.Write(&#8221; &lt; script language=javascript&gt;&#8221;); Response.Write(&#8220;parent.left.location.href=&#8217;PayDetailManage_Left.aspx&#8217;&#8221;); Response.Write(&#8221; &lt; /script&gt;&#8221;);<br />
页面定时刷新功能实现</p>
<p>有三种方法：</p>
<p>1，在html中设置：</p>
<p>&lt; title&gt;xxxxx &lt; /title&gt;之後加入下面这一行即可！</p>
<p>定时刷新：</p>
<p>&lt; META HTTP-EQUIV=&#8221;Refresh&#8221; content=&#8221;10&#8243;&gt; 10代表刷新间隔,单位为秒</p>
<p>2.jsp</p>
<p>&lt; % response.setHeader(&#8220;refresh&#8221;,&#8221;1&#8243;); %&gt; 每一秒刷新一次</p>
<p>3.使用javascript:</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; setTimeout(&#8220;self.location.reload(); &#8220;,1000); &lt; script&gt; 一秒一次</p>
<p>页面自动跳转：</p>
<p>1，在html中设置：</p>
<p>&lt; title&gt;xxxxx &lt; /title&gt;之後加入下面这一行即可！</p>
<p>定时跳转并刷新：</p>
<p>&lt; meta http-equiv=&#8221;refresh&#8221; content=&#8221;20; url=http://自己的URL&#8221;&gt;, 其中20指隔20秒后跳转到http://自己的URL 页面。</p>
<p>点击按钮提交表单后刷新上级窗口</p>
<p>A窗口打开B窗口</p>
<p>然后在B里面提交数据至C窗口</p>
<p>最后要刷新A窗口</p>
<p>并且关闭B窗口</p>
<p>几个javascript函数</p>
<p>//第一个自动关闭窗口</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; &lt; !&#8211; function clock(){i=i-1 document.title=&#8221;本窗口将在&#8221;+i+&#8221;秒后自动关闭!&#8221;; if(i&gt;0)setTimeout(&#8220;clock(); &#8220;,1000); else self.close(); } var i=2 clock(); //&#8211;&gt; &lt; /script&gt; //第二个刷新父页面的函数</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; opener.location.reload(); &lt; /script&gt; //第三个打开窗口</p>
<p>&lt; script language=&#8221;javascript&#8221;&gt; function show(mylink,mytitle,width,height) {mailwin=window.open(mylink,mytitle,&#8217;top=350,left=460,width=&#8217;+width+&#8217;,height=&#8217;+height+&#8217;,scrollbars=no&#8217;)} &lt; /script&gt; 有关asp.net页面刷新的问题,收藏这几种方法会有用的。</p>
<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="http://www.webcomgo.com/archives/3011"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div><p  class="related_post_title">最多留言日志</p><ul class="related_post"><li><a href="http://www.webcomgo.com/archives/2834" title="淘宝网首推应用商店“淘宝箱” 大淘宝战略助力ISV掘金网购蓝海">淘宝网首推应用商店“淘宝箱” 大淘宝战略助力ISV掘金网购蓝海</a></li><li><a href="http://www.webcomgo.com/archives/5969" title="月光：思考微博客">月光：思考微博客</a></li><li><a href="http://www.webcomgo.com/archives/3093" title="Google搜索结果的富文本摘要开始支持HTML 5 microdata">Google搜索结果的富文本摘要开始支持HTML 5 microdata</a></li><li><a href="http://www.webcomgo.com/archives/1890" title="酷6宣布投入3亿元提升内容质量">酷6宣布投入3亿元提升内容质量</a></li><li><a href="http://www.webcomgo.com/archives/5806" title="意识到就好 鲍尔默承认Windows Mobile错过了一代产品">意识到就好 鲍尔默承认Windows Mobile错过了一代产品</a></li><li><a href="http://www.webcomgo.com/archives/7037" title="百度百科与金山词霸合作 推进开放API战略">百度百科与金山词霸合作 推进开放API战略</a></li><li><a href="http://www.webcomgo.com/archives/4266" title="6 月 14 日Google Earth卫星图像更新">6 月 14 日Google Earth卫星图像更新</a></li><li><a href="http://www.webcomgo.com/archives/5876" title="Google首席搜索工程师：没有搜索的搜索">Google首席搜索工程师：没有搜索的搜索</a></li><li><a href="http://www.webcomgo.com/archives/301" title="马上就可以通过 Gtalk 来与 Facebook Chat 聊天啦">马上就可以通过 Gtalk 来与 Facebook Chat 聊天啦</a></li><li><a href="http://www.webcomgo.com/archives/6944" title="百度将推多个客户端：涉足企业即时通信等领域">百度将推多个客户端：涉足企业即时通信等领域</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.webcomgo.com/archives/3011/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用ASP.NET Global.asax 文件</title>
		<link>http://www.webcomgo.com/archives/2153</link>
		<comments>http://www.webcomgo.com/archives/2153#comments</comments>
		<pubDate>Mon, 21 Dec 2009 13:58:14 +0000</pubDate>
		<dc:creator>网来网去</dc:creator>
				<category><![CDATA[ASP.net]]></category>

		<guid isPermaLink="false">http://www.webcomgo.com/?p=2153</guid>
		<description><![CDATA[<br/>Global.asax 文件，有时候叫做 ASP.NET 应用程序文件，提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。下面让我们... ]]></description>
			<content:encoded><![CDATA[<br/><p>Global.asax 文件，有时候叫做 ASP.NET 应用程序文件，提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。下面让我们详细看一下如何在应用程序开发工作中使用这个文件。</p>
<p>概述</p>
<p><span id="more-2153"></span>Global.asax 位于应用程序根目录下。虽然 Visual Studio .NET 会自动插入这个文件到所有的 ASP.NET 项目中，但是它实际上是一个可选文件。删除它不会出问题——当然是在你没有使用它的情况下。.asax 文件扩展名指出它是一个应用程序文件，而不是一个使用 aspx 的 ASP.NET 文件。</p>
<p>Global.asax 文件被配置为任何（通过 URL 的）直接 HTTP 请求都被自动拒绝，所以用户不能下载或查看其内容。ASP.NET 页面框架能够自动识别出对Global.asax 文件所做的任何更改。在 Global.asax 被更改后ASP.NET 页面框架会重新启动应用程序，包括关闭所有的浏览器会话，去除所有状态信息，并重新启动应用程序域。</p>
<p>编程</p>
<p>Global.asax 文件继承自HttpApplication 类，它维护一个HttpApplication 对象池，并在需要时将对象池中的对象分配给应用程序。Global.asax 文件包含以下事件：</p>
<p>·         Application_Init：在应用程序被实例化或第一次被调用时，该事件被触发。对于所有的HttpApplication 对象实例，它都会被调用。</p>
<p>·         Application_Disposed：在应用程序被销毁之前触发。这是清除以前所用资源的理想位置。</p>
<p>·         Application_Error：当应用程序中遇到一个未处理的异常时，该事件被触发。</p>
<p>·         Application_Start：在HttpApplication 类的第一个实例被创建时，该事件被触发。它允许你创建可以由所有HttpApplication 实例访问的对象。</p>
<p>·         Application_End：在HttpApplication 类的最后一个实例被销毁时，该事件被触发。在一个应用程序的生命周期内它只被触发一次。</p>
<p>·         Application_BeginRequest：在接收到一个应用程序请求时触发。对于一个请求来说，它是第一个被触发的事件，请求一般是用户输入的一个页面请求（URL）。</p>
<p>·         Application_EndRequest：针对应用程序请求的最后一个事件。</p>
<p>·         Application_PreRequestHandlerExecute：在 ASP.NET 页面框架开始执行诸如页面或 Web 服务之类的事件处理程序之前，该事件被触发。</p>
<p>·         Application_PostRequestHandlerExecute：在 ASP.NET 页面框架结束执行一个事件处理程序时，该事件被触发。</p>
<p>·         Applcation_PreSendRequestHeaders：在 ASP.NET 页面框架发送 HTTP 头给请求客户（浏览器）时，该事件被触发。</p>
<p>·         Application_PreSendContent：在 ASP.NET 页面框架发送内容给请求客户（浏览器）时，该事件被触发。</p>
<p>·         Application_AcquireRequestState：在 ASP.NET 页面框架得到与当前请求相关的当前状态（Session 状态）时，该事件被触发。</p>
<p>·         Application_ReleaseRequestState：在 ASP.NET 页面框架执行完所有的事件处理程序时，该事件被触发。这将导致所有的状态模块保存它们当前的状态数据。</p>
<p>·         Application_ResolveRequestCache：在 ASP.NET 页面框架完成一个授权请求时，该事件被触发。它允许缓存模块从缓存中为请求提供服务，从而绕过事件处理程序的执行。</p>
<p>·         Application_UpdateRequestCache：在 ASP.NET 页面框架完成事件处理程序的执行时，该事件被触发，从而使缓存模块存储响应数据，以供响应后续的请求时使用。</p>
<p>·         Application_AuthenticateRequest：在安全模块建立起当前用户的有效的身份时，该事件被触发。在这个时候，用户的凭据将会被验证。</p>
<p>·         Application_AuthorizeRequest：当安全模块确认一个用户可以访问资源之后，该事件被触发。</p>
<p>·         Session_Start：在一个新用户访问应用程序 Web 站点时，该事件被触发。</p>
<p>·         Session_End：在一个用户的会话超时、结束或他们离开应用程序 Web 站点时，该事件被触发。</p>
<p>这个事件列表看起来好像多得吓人，但是在不同环境下这些事件可能会非常有用。</p>
<p>使用这些事件的一个关键问题是知道它们被触发的顺序。Application_Init 和Application_Start 事件在应用程序第一次启动时被触发一次。相似地，Application_Disposed 和 Application_End 事件在应用程序终止时被触发一次。此外，基于会话的事件（Session_Start 和 Session_End）只在用户进入和离开站点时被使用。其余的事件则处理应用程序请求，这些事件被触发的顺序是：</p>
<p>·         Application_BeginRequest</p>
<p>·         Application_AuthenticateRequest</p>
<p>·         Application_AuthorizeRequest</p>
<p>·         Application_ResolveRequestCache</p>
<p>·         Application_AcquireRequestState</p>
<p>·         Application_PreRequestHandlerExecute</p>
<p>·         Application_PreSendRequestHeaders</p>
<p>·         Application_PreSendRequestContent</p>
<p>·         &lt;&lt;执行代码&gt;&gt;</p>
<p>·         Application_PostRequestHandlerExecute</p>
<p>·         Application_ReleaseRequestState</p>
<p>·         Application_UpdateRequestCache</p>
<p>·         Application_EndRequest</p>
<p>这些事件常被用于安全性方面。下面这个 C# 的例子演示了不同的Global.asax 事件，该例使用Application_Authenticate 事件来完成通过 cookie 的基于表单（form）的身份验证。此外，Application_Start 事件填充一个应用程序变量，而Session_Start 填充一个会话变量。Application_Error 事件显示一个简单的消息用以说明发生的错误。</p>
<p>protected void Application_Start(Object sender, EventArgs e) {<br />
Application["Title"] = &#8220;Builder.com Sample&#8221;;<br />
}<br />
protected void Session_Start(Object sender, EventArgs e) {<br />
Session["startValue"] = 0;<br />
}<br />
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {<br />
// Extract the forms authentication cookie<br />
string cookieName = FormsAuthentication.FormsCookieName;<br />
HttpCookie authCookie = Context.Request.Cookies[cookieName];<br />
if(null == authCookie) {<br />
// There is no authentication cookie.<br />
return;<br />
}<br />
FormsAuthenticationTicket authTicket = null;<br />
try {<br />
authTicket = FormsAuthentication.Decrypt(authCookie.Value);<br />
} catch(Exception ex) {<br />
// Log exception details (omitted for simplicity)<br />
return;<br />
}<br />
if (null == authTicket) {<br />
// Cookie failed to decrypt.<br />
return;<br />
}<br />
// When the ticket was created, the UserData property was assigned<br />
// a pipe delimited string of role names.<br />
string[2] roles<br />
roles[0] = &#8220;One&#8221;<br />
roles[1] = &#8220;Two&#8221;<br />
// Create an Identity object<br />
FormsIdentity id = new FormsIdentity( authTicket );<br />
// This principal will flow throughout the request.<br />
GenericPrincipal principal = new GenericPrincipal(id, roles);<br />
// Attach the new principal object to the current HttpContext object<br />
Context.User = principal;<br />
}<br />
protected void Application_Error(Object sender, EventArgs e) {<br />
Response.Write(&#8220;Error encountered.&#8221;);<br />
}</p>
<p>这个例子只是很简单地使用了一些Global.asax 文件中的事件；重要的是要意识到这些事件是与整个应用程序相关的。这样，所有放在其中的方法都会通过应用程序的代码被提供，这就是它的名字为Global 的原因。</p>
<p>这里是前面的例子相应的 VB.NET 代码：</p>
<p>Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)<br />
Application(&#8220;Title&#8221;) = &#8220;Builder.com Sample&#8221;<br />
End Sub<br />
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)<br />
Session(&#8220;startValue&#8221;) = 0<br />
End Sub<br />
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As<br />
 EventArgs)<br />
&#8216; Extract the forms authentication cookie<br />
Dim cookieName As String<br />
cookieName = FormsAuthentication.FormsCookieName<br />
Dim authCookie As HttpCookie<br />
authCookie = Context.Request.Cookies(cookieName)<br />
If (authCookie Is Nothing) Then<br />
&#8216; There is no authentication cookie.<br />
Return<br />
End If<br />
Dim authTicket As FormsAuthenticationTicket<br />
authTicket = Nothing<br />
Try<br />
authTicket = FormsAuthentication.Decrypt(authCookie.Value)<br />
Catch ex As Exception<br />
&#8216; Log exception details (omitted for simplicity)<br />
Return<br />
End Try<br />
Dim roles(2) As String<br />
roles(0) = &#8220;One&#8221;<br />
roles(1) = &#8220;Two&#8221;<br />
Dim id As FormsIdentity<br />
id = New FormsIdentity(authTicket)<br />
Dim principal As GenericPrincipal<br />
principal = New GenericPrincipal(id, roles)<br />
&#8216; Attach the new principal object to the current HttpContext object<br />
Context.User = principal<br />
End Sub<br />
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)<br />
Response.Write(&#8220;Error encountered.&#8221;)<br />
End Sub</p>
<p>资源</p>
<p>Global.asax 文件是 ASP.NET 应用程序的中心点。它提供无数的事件来处理不同的应用程序级任务，比如用户身份验证、应用程序启动以及处理用户会话等。你应该熟悉这个可选文件，这样就可以构建出健壮的ASP.NET 应用程序。</p>
<p>本文来自CSDN博客，转载请标明出处：<a href="http://blog.csdn.net/tuwen/archive/2009/12/21/5050055.aspx">http://blog.csdn.net/tuwen/archive/2009/12/21/5050055.aspx</a></p>
<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="small-count" data-url="http://www.webcomgo.com/archives/2153"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div><p  class="related_post_title">相关文章</p><ul class="related_post"><li><a href="http://www.webcomgo.com/archives/3695" title="ASP.NET 2.0防止同一用户同时登陆">ASP.NET 2.0防止同一用户同时登陆</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.webcomgo.com/archives/2153/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
