FastCGI的IIS模块可以让流行的应用框架在IIS上支持FastCGI协议以提供高性能和可用性的访问服务,FastCGI面向现有的网络服务器提供高性能的CGI替代标准。具体PHP 安装方法我记录下来,其中包括出现的一个错误.环境: 操作系统:Windows 2003 Server PHP版本:PHP5.25 1.下载FastCGI For IIS6 http://www.microsoft.com/downloads/details.aspx?FamilyID=2d481579-9a7c-4632-b6e6-dee9097f9dc5&displaylang=en 下载之后,双击运行进行安装. 2.下载PHP5.25 Windows版 http://www.php.net 下载.zip格式的版本,下载后解压至D:\PHP目录,并给IIS启动帐户组或用户赋予读取和运行权限. 3.注册PHP到FastCGI 在命令行下运行 cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"D:\PHP\php-cgi.exe" fcgiconfig.js 默认存放目录是:C:\WINDOWS\system32\inetsrv 4.设置 将D:\PHP\php.ini-recommended重命名为为D:\PHP\php.ini 打开D:\PHP\php.ini,修改: extension_dir = "D:\PHP\ext" fastcgi.impersonate = 1 cgi.fix_pathinfo=1 cgi.force_redirect = 0 其它根据实际需要对php.ini进行设置修改 然后执行: cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:500 cscript fcgiconfig.js -set -section:"PHP" -EnvironmentVars:PHP_FCGI_MAX_REQUESTS:1000 以上两项设置最大池和响应连接数,可以根据运行机器的硬件配置、使用情况修改 一切完毕之后重启IIS,赶紧写一个测试页试一下吧. 我第一次安装时,由于为了服务器的安全我删除了wsshell命令,导致在第3步时出现了如下错误: Microsoft JScript 运行时错误: Automation 服务器不能创建对象 后通过重新注册命令:regsvr32 WSHom.ocx 问题解决 重启IIS后,打开测试页出现如下提示: FastCGI Error The FastCGI Handler was unable to process the request. ——————————————————————————-- Error Details: Error Number: 5 (0x80070005). Error Description: 拒绝访问。 HTTP Error 500 – Server Error. Internet Information Services (IIS) 这个错误是由于在解压PHP之后,没有对IIS启动帐户赋予该目录的读取和运行权限.修改文件夹安全属性,问题解决. 转自:http://www.jb51.net/article/16953.htm
View Details取出richTextBox里面的内容第一种方法:将richTextBox的内容以字符串的形式取出string xw = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);第二种方法:将richTextBox的类容以二进制数据的方法取出FlowDocument document = richTextBox.Document;System.IO.Stream s = new System.IO.MemoryStream(); System.Windows.Markup.XamlWriter.Save(document, s); byte[] data = new byte[s.Length];s.Position = 0;s.Read(data, 0, data.Length);s.Close(); 赋值给richTextBox 第一种方法:将字符串转换为数据流赋值给richTextBox中 System.IO.StringReader sr = new System.IO.StringReader(xw);System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);richTextBox1.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);第二种方法:将二进制数据赋值给richTextBoxSystem.IO.Stream ss = new System.IO.MemoryStream(data);FlowDocument doc = System.Windows.Markup.XamlReader.Load(ss) as FlowDocument;ss.Close();richTextBox1.Document = doc; 清空RichTextBox的方法 System.Windows.Documents.FlowDocument doc = richTextBox.Document;doc.Blocks.Clear(); 如何将一个String类型的字符串赋值给richTextBoxmyRTB.Document = new FlowDocument(new Paragraph(new Run(myString))); FlowDocument doc = new FlowDocument();Paragraph p = new Paragraph(); // Paragraph 类似于 html 的 P 标签Run r = new Run(myString); // Run 是一个 Inline 的标签p.Inlines.Add(r);doc.Blocks.Add(p);myRTB.Document = […]
View Details给大家讲到WPF,我们就不得不提到WinForm,51CTO之前也报导过《WPF与WinForm对比谈 多线程编程优化是关键》的技术文章,对于他们之间的优劣有了更深刻的认识。而今天我们要讲到的是WPF 4中的DataGrid空间。我们也诚挚向您推荐《让你的代码“炫”起来——WPF开发教程》。 提到DataGrid 不管是网页还是应用程序开发都会频繁使用。通过它我们可以灵活的在行与列间显示各种数据。本篇将详细介绍WPF 4 中DataGrid 的相关功能。 自定义列 默认情况下,当我们为DataGrid 控件设置ItemSource 属性后,DataGrid 会根据数据类型自动生成相应的列,下表列出DataGrid 支持的四种列及其数据类型。 在创建DataGrid 时可以通过AutoGenerateColumns 属性设置列是否自动生成,从而加入自定义列。如果DataGrid 中同时包含“自动生成列”与“用户自定义列”,则首先创建“用户自定义列”。下面代码实例中分别创建这四种列:
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">Window</span><span> </span><span class="attribute">x:Class</span><span>=</span><span class="attribute-value">"WPF4ControlTest.MainWindow"</span><span> </span></span></li><li><span> </span><span class="attribute">xmlns</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:x</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span> </span></li><li><span> </span><span class="attribute">xmlns:local</span><span>=</span><span class="attribute-value">"clr-namespace:WPF4ControlTest"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:assembly</span><span>=</span><span class="attribute-value">"clr-namespace:System;assembly=mscorlib"</span><span> </span></li><li><span> </span><span class="attribute">Title</span><span>=</span><span class="attribute-value">"MainWindow"</span><span> </span><span class="attribute">Height</span><span>=</span><span class="attribute-value">"200"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"500"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider</span><span> </span><span class="attribute">x:Key</span><span>=</span><span class="attribute-value">"sexEnum"</span><span> </span><span class="attribute">MethodName</span><span>=</span><span class="attribute-value">"GetValues"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">ObjectType</span><span>=</span><span class="attribute-value">"{x:Type assembly:Enum}"</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">x:Type</span><span> </span><span class="attribute">Type</span><span>=</span><span class="attribute-value">"local:SexOpt"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Name"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Name}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Age"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"50"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Age}"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridComboBoxColumn</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Sex"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">SelectedItemBinding</span><span>=</span><span class="attribute-value">"{Binding Sex}"</span><span> </span></li><li><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding Source={StaticResource sexEnum}}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridCheckBoxColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Pass Exam?"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"100"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Pass}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridHyperlinkColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Email"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"150"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Email}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">DataGrid</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span class="tag"></</span><span class="tag-name">Window</span><span class="tag">></span><span> </span></li></ol> |
其中每列都包含各自的数值类型,在C# 中创建Member 类及SexOpt 枚举,并将memberData 数据绑定到DataGrid:
1 |
<ol class="dp-c"><li class="alt"><span><span class="keyword">using</span><span> System; </span></span></li><li><span class="keyword">using</span><span> System.Windows; </span></li><li class="alt"><span class="keyword">using</span><span> System.Collections.ObjectModel; </span></li><li><span> </span></li><li class="alt"><span class="keyword">namespace</span><span> WPF4ControlTest </span></li><li><span>{ </span></li><li class="alt"><span> </span><span class="comment">/// <summary> </span><span> </span></li><li><span> </span><span class="comment">/// Interaction logic for MainWindow.xaml </span><span> </span></li><li class="alt"><span> </span><span class="comment">/// </summary> </span><span> </span></li><li><span> </span><span class="keyword">public</span><span> partial </span><span class="keyword">class</span><span> MainWindow : Window </span></li><li class="alt"><span> { </span></li><li><span> </span><span class="keyword">public</span><span> MainWindow() </span></li><li class="alt"><span> { </span></li><li><span> InitializeComponent(); </span></li><li><span>ObservableCollection<Member> memberData = </span><span class="keyword">new</span><span> ObservableCollection<Member>(); </span></li><li><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li class="alt"><span> { </span></li><li><span> Name = </span><span class="string">"Joe"</span><span>, Age = </span><span class="string">"23"</span><span>, Sex = SexOpt.Male, </span></li><li class="alt"><span> Pass = </span><span class="keyword">true</span><span>, Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Joe@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Mike"</span><span>, Age = </span><span class="string">"20"</span><span>, </span></li><li><span> Sex = SexOpt.Male, Pass = </span><span class="keyword">false</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Mike@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Lucy"</span><span>, Age = </span><span class="string">"25"</span><span>, </span></li><li><span> Sex = SexOpt.Female, Pass = </span><span class="keyword">true</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Lucy@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> dataGrid.DataContext = memberData; </span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">enum</span><span> SexOpt { Male, Female }; </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Member </span></li><li><span> { </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Name { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Age { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> SexOpt Sex { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">bool</span><span> Pass { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> Uri Email { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> } </span></li><li class="alt"><span>} </span></li></ol> |
这样我们就可以通过自定义列的方式创建出DataGrid: 选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改DataGrid 的选择模式。 SelectionUnit:包含Cell、FullRow 和CellOrRowHeader 三种单元选择模式。 ◆Cell:选择单元格; ◆FullRow:选择全行; ◆CellOrRowHeader:可选择单元格,也可以通过点击行首选择全行; SelectionMode:分为Extended 和Single 两种模式。 ◆Extended:选择多个单元(单元格或全行,由SelectionUnit 定义); ◆Single:选择唯一单元(单元格或全行,由SelectionUnit 定义);
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span class="tag">></span><span>… … </span></li></ol> |
设置SelectionUnit 和SelectionMode 后的实例效果: 编辑 默认情况下,我们可以直接在DataGrid 中编辑数据(下表为相关的编辑命令),当然也可以通过IsReadOnly 属性将DataGrid 设置为只读方式。
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span> </span><span class="attribute">IsReadOnly</span><span>=</span><span class="attribute-value">"True"</span><span class="tag">></span><span>… … </span></li></ol> |
原文标题:WPF 4 DataGrid 控件(基本功能篇) 链接:http://www.cnblogs.com/gnielee/archive/2010/04/16/wpf4-datagrid-control-basic.html
View Details1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.获取所有表名: SELECT Name FROM DatabaseName..SysObjects Where XType=’U' ORDER BY Name XType=’U':表示所有用户表; XType=’S':表示所有系统表; 3.获取所有字段名: SELECT Name FROM SysColumns WHERE id=Object_Id('TableName') access : 如果表存在,如何得到这个表? SELECT * FROM MSysObjects where name=’ExamResultTime ' and type=1 and flags=0 //得到该数据库中所有的表名 SELECT name FROM MSysObjects where type=1 and flags=0 转自:http://www.soaspx.com/dotnet/sql/mssql/sql2000/sqlserver2000_20091215_2051.html
View Details在WPF布局框架中,如果标签不设置布局属性,系统默认为填充模式。当然,我们可以通过一系列标签的属性来达到布局标签的作用。 最常见的属性是Width和Height,是指定标签的宽和高。 MinWidth,MinHeight是指标签的最小宽度和最小高度,即使父容器的宽和高小于这个数字,标签也保持这个值。 MaxWidth,MaxHeight是指标签的最大宽度和最大高度。 最小和最大Width,Height将将会在一个固的区间来调整标签的大小。 HorizontalAlignment是标签在当前容器中左中右的停靠。VerticalAlignment是标签在当前容器中上中下的停靠。这两种停靠都是建立在父容器内的,只有父容器内大于标签是才能看到效果。 Margin,是一个独特的属性,它的值有三种情况,单值,表示该标签距父容器四周的距,两个值,第一个值为标签距父容器左右的距离,第二个值是标签距父器上下的距离,四个值,分别距父容器左,上,右,下的距离。如果存在Width和Height属性,它们的优先级要高于Margin。但一般情况了,两种属性的设置不会同时出现,Width和Height更强调固态的值,Margin强调随性的值,但它距边缘是固定的。 Padding和Margin相似,不同的是Padding是指标签内容与标签边界的距离。 Panel.ZIndex是一个层次的属性,这个值表示标签本身在父容器上位于那一层,值越大,越位于上层。可以用Panel.SetZIndex(标签名, 3);来设置标签元素的层次。 RenderTransofrm是一个缩放的属性,就是对当前标签的直接缩放,例子如下: <Button Content="123" ;50" Height="20" Click="Button_Click" > <Button.RenderTransform> <ScaleTransform. ScaleX="0.5" ScaleY="0.5"> </ScaleTransform> </Button.RenderTransform> </Button> C#代码: button1.RenderTransform. = new ScaleTransform(2, 2); LayoutTransform是一个旋转的属性 <Button Name="button1" Content="123" ;50" Height="20" > <Button.LayoutTransform> <RotateTransform. Angle="45"></RotateTransform> </Button.LayoutTransform> </Button> C#代码: button1.LayoutTransform. = new RotateTransform(90); 转换的类型还有MatrixTransform,如 <Button Name="button1" Content="123" ;50" Height="20" > <Button.LayoutTransform> <MatrixTransform. Matrix="3, 1, 1, 3, 0, 0"></MatrixTransform> </Button.LayoutTransform> </Button> C#代码: button1.LayoutTransform. = new MatrixTransform(3, 1, 1, 3, 0, 0); 上面的转换是用矩阵的方式实现,下面从x和y轴上来建立转换: <Button.LayoutTransform> <SkewTransform. AngleX="30" AngleY="30" ></SkewTransform> </Button.LayoutTransform> C#代码: button1.LayoutTransform. = new SkewTransform(10, 30, […]
View DetailsAsp.net的NamePipe机制给我们提供了很多扩展性. 使用HttpModule我们可能实现的有: 强制站点范围的Cookie策略 集中化监控与日志 编写设置与删除HTTP头 控制response输出,如删除多余空白字符 Session管理 认证与受权 下面我们来看如何实现自定义异常处理:
1 |
<span class="lnum"> 1: </span> <span class="kwrd">public</span> <span class="kwrd">class</span> ErrorModule:IHttpModule |
1 |
<span class="lnum"> 2: </span> { |
1 |
<span class="lnum"> 3: </span> <span class="preproc">#region</span> IHttpModule Members |
1 |
<span class="lnum"> 4: </span> |
1 |
<span class="lnum"> 5: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.</span> |
1 |
<span class="lnum"> 7: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 8: </span> <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose() |
1 |
<span class="lnum"> 9: </span> { |
1 |
<span class="lnum"> 10: </span> <span class="rem">//do nothing </span> |
1 |
<span class="lnum"> 11: </span> } |
1 |
<span class="lnum"> 12: </span> |
1 |
<span class="lnum"> 13: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 14: </span> <span class="rem">/// Initializes a module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 15: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 16: </span> <span class="rem">/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param></span> |
1 |
<span class="lnum"> 17: </span> <span class="kwrd">public</span> <span class="kwrd">void</span> Init(HttpApplication context) |
1 |
<span class="lnum"> 18: </span> { |
1 |
<span class="lnum"> 19: </span> context.Error += <span class="kwrd">new</span> EventHandler(customcontext_Error); |
1 |
<span class="lnum"> 20: </span> } |
1 |
<span class="lnum"> 21: </span> |
1 |
<span class="lnum"> 22: </span> <span class="kwrd">private</span> <span class="kwrd">void</span> customcontext_Error(<span class="kwrd">object</span> sender, EventArgs e) |
1 |
<span class="lnum"> 23: </span> { |
1 |
<span class="lnum"> 24: </span> HttpContext ctx = HttpContext.Current; |
1 |
<span class="lnum"> 25: </span> HttpResponse response = ctx.Response; |
1 |
<span class="lnum"> 26: </span> HttpRequest request = ctx.Request; |
1 |
<span class="lnum"> 27: </span> |
1 |
<span class="lnum"> 28: </span> Exception exception = ctx.Server.GetLastError(); |
1 |
<span class="lnum"> 29: </span> |
1 |
<span class="lnum"> 30: </span> var sboutput = <span class="kwrd">new</span> StringBuilder(); |
1 |
<span class="lnum"> 31: </span> sboutput.Append(<span class="str">"Querystring:<p/>"</span>); |
1 |
<span class="lnum"> 32: </span> <span class="rem">//Get out the query string </span> |
1 |
<span class="lnum"> 33: </span> <span class="kwrd">int</span> count = request.QueryString.Count; |
1 |
<span class="lnum"> 34: </span> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i < count; i++) |
1 |
<span class="lnum"> 35: </span> { |
1 |
<span class="lnum"> 36: </span> sboutput.AppendFormat(<span class="str">"<br/> {0}:-- {1} "</span>, request.QueryString.Keys[i], request.QueryString[i]); |
1 |
<span class="lnum"> 37: </span> } |
1 |
<span class="lnum"> 38: </span> <span class="rem">//Get out the form collection info</span> |
1 |
<span class="lnum"> 39: </span> sboutput.Append(<span class="str">"<p>-------------------------<p/>Form:<p/>"</span>); |
1 |
<span class="lnum"> 40: </span> count = request.Form.Count; |
1 |
<span class="lnum"> 41: </span> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i < count; i++) |
1 |
<span class="lnum"> 42: </span> { |
1 |
<span class="lnum"> 43: </span> sboutput.AppendFormat(<span class="str">"<br/> {0}:-- {1} -- <br/>"</span>, request.Form.Keys[i], request.Form[i]); |
1 |
<span class="lnum"> 44: </span> } |
1 |
<span class="lnum"> 45: </span> sboutput.Append(<span class="str">"<p>-------------------------<p/>ErrorInfo:<p/>"</span>); |
1 |
<span class="lnum"> 46: </span> sboutput.AppendFormat(<span class="str">@"Your request could not processed. Please press the back button on your browser and try again.<br/></span> |
1 |
<span class="lnum"> 47: </span> If the problem persists, please contact technical support<p/> |
1 |
<span class="lnum"> 48: </span> Information below is for technical support:<p/> |
1 |
<span class="lnum"> 49: </span> <p/>URL:{0}<p/>Stacktrace:---<br/>{1}<p/>InnerException:<br/>{2}" |
1 |
<span class="lnum"> 50: </span> , ctx.Request.Url, exception.InnerException.StackTrace, exception.InnerException); |
1 |
<span class="lnum"> 51: </span> |
1 |
<span class="lnum"> 52: </span> response.Write(sboutput.ToString()); |
1 |
<span class="lnum"> 53: </span> |
1 |
<span class="lnum"> 54: </span> <span class="rem">// To let the page finish running we clear the error</span> |
1 |
<span class="lnum"> 55: </span> ctx.Server.ClearError(); |
1 |
<span class="lnum"> 56: </span> } |
1 |
<span class="lnum"> 57: </span> |
1 |
<span class="lnum"> 58: </span> <span class="preproc">#endregion</span> |
1 |
<span class="lnum"> 59: </span> |
1 |
<span class="lnum"> 60: </span> } |
上面的代码实现了IHttpModule接口, 实现基于HttpApplication.Error事件, 接着我们自定义输出了一些信息,包括Form,QueryString. 最后把原来的Error信息清除了,这样你看到以前那个黄页了. 这个自定义的Module可以用于调试Web应用程序使用. Web.config中配置:
1 |
<span class="kwrd"><</span><span class="html">httpModules</span><span class="kwrd">></span> |
1 |
<span class="kwrd"><</span><span class="html">add</span> <span class="attr">name</span><span class="kwrd">="ErrorLoggingModule"</span> <span class="attr">type</span><span class="kwrd">="MyWeb.ErrorModule"</span><span class="kwrd">/></span> |
1 |
<span class="kwrd"></</span><span class="html">httpModules</span><span class="kwrd">></span> |
实际开发中,我们可以做的事儿很多,对这些信息记日志,发邮件. 如下, 我们演示使用Enterprise Library 做异常处理并日志记录,部分代码如下:
1 |
<span class="lnum"> 1: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 2: </span> <span class="rem">/// Handles the Error event of the EntLibLogging control.</span> |
1 |
<span class="lnum"> 3: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 4: </span> <span class="rem">/// <param name="sender">The source of the event.</param></span> |
1 |
<span class="lnum"> 5: </span> <span class="rem">/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param></span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// <remarks>author Petter Liu http://wintersun.cnblogs.com</remarks></span> |
1 |
<span class="lnum"> 7: </span> <span class="kwrd">private</span> <span class="kwrd">void</span> EntLibLogging_Error(<span class="kwrd">object</span> sender, EventArgs e) |
1 |
<span class="lnum"> 8: </span> { |
1 |
<span class="lnum"> 9: </span> var builder = <span class="kwrd">new</span> ConfigurationSourceBuilder(); |
1 |
<span class="lnum"> 10: </span> |
1 |
<span class="lnum"> 11: </span> builder.ConfigureInstrumentation() |
1 |
<span class="lnum"> 12: </span> .ForApplicationInstance(<span class="str">"MyApp"</span>) |
1 |
<span class="lnum"> 13: </span> .EnableLogging() |
1 |
<span class="lnum"> 14: </span> .EnablePerformanceCounters(); |
1 |
<span class="lnum"> 15: </span> |
1 |
<span class="lnum"> 16: </span> <span class="rem">//a single exception handling policy named MyPolicy for exceptions of type ArgumentNullException. </span> |
1 |
<span class="lnum"> 17: </span> <span class="rem">//The handler for this exception policy will log the exception to the General category (defined in the Logging Application Block configuration) </span> |
1 |
<span class="lnum"> 18: </span> <span class="rem">//as a warning with event ID 9000, wrap the ArgumentNullException with an InvalidOperationException, set the new exception message to MyMessage, and then re-throw the exception.</span> |
1 |
<span class="lnum"> 19: </span> builder.ConfigureExceptionHandling() |
1 |
<span class="lnum"> 20: </span> .GivenPolicyWithName(<span class="str">"MyPolicy"</span>) |
1 |
<span class="lnum"> 21: </span> .ForExceptionType<ArgumentNullException>() |
1 |
<span class="lnum"> 22: </span> .LogToCategory(<span class="str">"Exception"</span>) |
1 |
<span class="lnum"> 23: </span> .WithSeverity(System.Diagnostics.TraceEventType.Warning) |
1 |
<span class="lnum"> 24: </span> .UsingEventId(9000) |
1 |
<span class="lnum"> 25: </span> .WrapWith<InvalidOperationException>() |
1 |
<span class="lnum"> 26: </span> .UsingMessage(<span class="str">"MyMessage"</span>) |
1 |
<span class="lnum"> 27: </span> .ThenNotifyRethrow(); |
1 |
<span class="lnum"> 28: </span> |
1 |
<span class="lnum"> 29: </span> <span class="rem">//logging application </span> |
1 |
<span class="lnum"> 30: </span> builder.ConfigureLogging() |
1 |
<span class="lnum"> 31: </span> .WithOptions |
1 |
<span class="lnum"> 32: </span> .DoNotRevertImpersonation() |
1 |
<span class="lnum"> 33: </span> .LogToCategoryNamed(<span class="str">"Exception"</span>) |
1 |
<span class="lnum"> 34: </span> .SendTo.FlatFile(<span class="str">"Exception Logging File"</span>) |
1 |
<span class="lnum"> 35: </span> .FormatWith(<span class="kwrd">new</span> FormatterBuilder() |
1 |
<span class="lnum"> 36: </span> .TextFormatterNamed(<span class="str">"Text Formatter"</span>) |
1 |
<span class="lnum"> 37: </span> . UsingTemplate(<span class="str">"Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}"</span>)) |
1 |
<span class="lnum"> 38: </span> .ToFile(<span class="str">"d:\\logs\\ExceptionsLog.log"</span>) |
1 |
<span class="lnum"> 39: </span> .SendTo.RollingFile(<span class="str">"Rolling Log files"</span>) |
1 |
<span class="lnum"> 40: </span> .RollAfterSize(1024) |
1 |
<span class="lnum"> 41: </span> .ToFile(<span class="str">"d:\\logs\\Rollinglog.log"</span>) |
1 |
<span class="lnum"> 42: </span> .LogToCategoryNamed(<span class="str">"General"</span>) |
1 |
<span class="lnum"> 43: </span> .WithOptions.SetAsDefaultCategory() |
1 |
<span class="lnum"> 44: </span> .SendTo.SharedListenerNamed(<span class="str">"Exception Logging File"</span>); |
1 |
<span class="lnum"> 45: </span> |
1 |
<span class="lnum"> 46: </span> var configSource = <span class="kwrd">new</span> DictionaryConfigurationSource(); |
1 |
<span class="lnum"> 47: </span> builder.UpdateConfigurationWithReplace(configSource); |
1 |
<span class="lnum"> 48: </span> EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); |
1 |
<span class="lnum"> 49: </span> |
1 |
<span class="lnum"> 50: </span> var ex = HttpContext.Current.Server.GetLastError(); |
1 |
<span class="lnum"> 51: </span> var em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>(); |
1 |
<span class="lnum"> 52: </span> em.HandleException(ex.InnerException, <span class="str">"MyPolicy"</span>); |
1 |
<span class="lnum"> 53: </span> } |
注意上面的代码, 为了运行代码您需要引用以下程序集 Enterprise Library Share Common LibraryMicrosoft.Practices.ServiceLocationLogging Application BlockException Handling Application BlockException Handling Logging Provider 这里我们使用Fluent API配置, 因此没有配置XML文件. 所以不需要Web.Config中配置任何信息. 代码中有注释. 为了测试我们使用一个PAGE故意Throw 一个ArgumentNullException. 通过Server.GetLastError()获取Exception, 这时由名为MyPolicy策略处理异常. 对于ArgumentNullException把它们记录日志到名为Exception分类中,这个日志文件位于d:\\logs\\ExceptionLog.log.实际开发你完全按照你的需要来自定义策略. 然后这个日志文件中写出的内容是这样的:
1 |
----------------------------------------Timestamp: 2011-11-12 5:57:08Message: HandlingInstanceID: a99d005d-5f8d-4613-9522-2d60efb089aaAn exception of type <span class="str">'System.ArgumentNullException'</span> occurred and was caught.----------------------------------------------------------------------------11/12/2011 13:57:08Type : System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Message : Value cannot be <span class="kwrd">null</span>.Parameter name: Demo errorSource : MyWebHelp link : ParamName : Demo errorData : System.Collections.ListDictionaryInternalTargetSite : Void Page_Load(System.Object, System.EventArgs)Stack Trace : at MyWeb.About.Page_Load(Object sender, EventArgs e) <span class="kwrd">in</span> H:\My Project\DotNet40\TDD2010\WebHost\MyWeb\About.aspx.cs:line 14 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Additional Info:MachineName : USERTimeStamp : 2011-11-12 5:57:08FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35AppDomainName : 3e5cb21e-3-129655510216406250ThreadIdentity : WindowsIdentity : USER\PetterCategory: Exception |
由于我们日志模块我们配置还需要写Rollinglog.log文件,内容如下:
1 |
----------------------------------------Exception Warning: 9000 : Timestamp: 2011-11-12 5:57:08Message: HandlingInstanceID: a99d005d-5f8d-4613-9522-2d60efb089aaAn exception of type <span class="str">'System.ArgumentNullException'</span> occurred and was caught.----------------------------------------------------------------------------11/12/2011 13:57:08Type : System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Message : Value cannot be <span class="kwrd">null</span>.Parameter name: Demo errorSource : MyWebHelp link : ParamName : Demo errorData : System.Collections.ListDictionaryInternalTargetSite : Void Page_Load(System.Object, System.EventArgs)Stack Trace : at MyWeb.About.Page_Load(Object sender, EventArgs e) <span class="kwrd">in</span> H:\My Project\DotNet40\TDD2010\WebHost\MyWeb\About.aspx.cs:line 14 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Additional Info:MachineName : USERTimeStamp : 2011-11-12 5:57:08FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35AppDomainName : 3e5cb21e-3-129655510216406250ThreadIdentity : WindowsIdentity : USER\PetterCategory: ExceptionPriority: 0EventId: 9000Severity: WarningTitle:Enterprise Library Exception HandlingMachine: USERApp Domain: 3e5cb21e-3-129655510216406250ProcessId: 2444Process Name: C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exeThread Name: Win32 ThreadId:2748Extended Properties: ---------------------------------------- |
1 |
你可以看到上面的EventId=9000与我们之前在CODE中配置是相同的. <br /> |
1 |
在开源社区有一个组件<a href="http://code.google.com/p/elmah/" target="_blank">ELMAH</a>(Error Logging Modules and Handlers for ASP.NET),已实现Error处理发邮件,写DB,发送到SNS等功能. 我们随意来看下它的代码:<br />它就是基于IHttpModule的扩展,下面代码来ELMAH: |
1 |
<span class="lnum"> 1: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 2: </span> <span class="rem">/// Provides an abstract base class for <see cref="IHttpModule"/> that</span> |
1 |
<span class="lnum"> 3: </span> <span class="rem">/// supports discovery from within partial trust environments.</span> |
1 |
<span class="lnum"> 4: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 5: </span> <span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> HttpModuleBase : IHttpModule |
1 |
<span class="lnum"> 6: </span> { |
1 |
<span class="lnum"> 7: </span> <span class="kwrd">void</span> IHttpModule.Init(HttpApplication context) |
1 |
<span class="lnum"> 8: </span> { |
1 |
<span class="lnum"> 9: </span> <span class="kwrd">if</span> (context == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 10: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"context"</span>); |
1 |
<span class="lnum"> 11: </span> |
1 |
<span class="lnum"> 12: </span> <span class="kwrd">if</span> (SupportDiscoverability) |
1 |
<span class="lnum"> 13: </span> HttpModuleRegistry.RegisterInPartialTrust(context, <span class="kwrd">this</span>); |
1 |
<span class="lnum"> 14: </span> |
1 |
<span class="lnum"> 15: </span> OnInit(context); |
1 |
<span class="lnum"> 16: </span> } |
1 |
<span class="lnum"> 17: </span> |
1 |
<span class="lnum"> 18: </span> <span class="kwrd">void</span> IHttpModule.Dispose() |
1 |
<span class="lnum"> 19: </span> { |
1 |
<span class="lnum"> 20: </span> OnDispose(); |
1 |
<span class="lnum"> 21: </span> } |
1 |
<span class="lnum"> 22: </span> |
1 |
<span class="lnum"> 23: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 24: </span> <span class="rem">/// Determines whether the module will be registered for discovery</span> |
1 |
<span class="lnum"> 25: </span> <span class="rem">/// in partial trust environments or not.</span> |
1 |
<span class="lnum"> 26: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 27: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">bool</span> SupportDiscoverability |
1 |
<span class="lnum"> 28: </span> { |
1 |
<span class="lnum"> 29: </span> get { <span class="kwrd">return</span> <span class="kwrd">false</span>; } |
1 |
<span class="lnum"> 30: </span> } |
1 |
<span class="lnum"> 31: </span> |
1 |
<span class="lnum"> 32: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 33: </span> <span class="rem">/// Initializes the module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 34: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 35: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnInit(HttpApplication application) {} |
1 |
<span class="lnum"> 36: </span> |
1 |
<span class="lnum"> 37: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 38: </span> <span class="rem">/// Disposes of the resources (other than memory) used by the module.</span> |
1 |
<span class="lnum"> 39: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 40: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnDispose() {} |
1 |
<span class="lnum"> 41: </span> } |
1 |
这是ErrorLogModule实现之前HttpModuleBase: |
1 |
<span class="lnum"> 1: </span><span class="kwrd">public</span> <span class="kwrd">class</span> ErrorLogModule : HttpModuleBase, IExceptionFiltering |
1 |
<span class="lnum"> 2: </span> { |
1 |
<span class="lnum"> 3: </span> <span class="kwrd">public</span> <span class="kwrd">event</span> ExceptionFilterEventHandler Filtering; |
1 |
<span class="lnum"> 4: </span> <span class="kwrd">public</span> <span class="kwrd">event</span> ErrorLoggedEventHandler Logged; |
1 |
<span class="lnum"> 5: </span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 7: </span> <span class="rem">/// Initializes the module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 8: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 9: </span> |
1 |
<span class="lnum"> 10: </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnInit(HttpApplication application) |
1 |
<span class="lnum"> 11: </span> { |
1 |
<span class="lnum"> 12: </span> <span class="kwrd">if</span> (application == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 13: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"application"</span>); |
1 |
<span class="lnum"> 14: </span> |
1 |
<span class="lnum"> 15: </span> application.Error += <span class="kwrd">new</span> EventHandler(OnError); |
1 |
<span class="lnum"> 16: </span> ErrorSignal.Get(application).Raised += <span class="kwrd">new</span> ErrorSignalEventHandler(OnErrorSignaled); |
1 |
<span class="lnum"> 17: </span> } |
1 |
<span class="lnum"> 18: </span> |
1 |
<span class="lnum"> 19: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 20: </span> <span class="rem">/// Gets the <see cref="ErrorLog"/> instance to which the module</span> |
1 |
<span class="lnum"> 21: </span> <span class="rem">/// will log exceptions.</span> |
1 |
<span class="lnum"> 22: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 23: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> ErrorLog GetErrorLog(HttpContext context) |
1 |
<span class="lnum"> 24: </span> { |
1 |
<span class="lnum"> 25: </span> <span class="kwrd">return</span> ErrorLog.GetDefault(context); |
1 |
<span class="lnum"> 26: </span> } |
1 |
<span class="lnum"> 27: </span> |
1 |
<span class="lnum"> 28: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 29: </span> <span class="rem">/// The handler called when an unhandled exception bubbles up to </span> |
1 |
<span class="lnum"> 30: </span> <span class="rem">/// the module.</span> |
1 |
<span class="lnum"> 31: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 32: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnError(<span class="kwrd">object</span> sender, EventArgs args) |
1 |
<span class="lnum"> 33: </span> { |
1 |
<span class="lnum"> 34: </span> HttpApplication application = (HttpApplication) sender; |
1 |
<span class="lnum"> 35: </span> LogException(application.Server.GetLastError(), application.Context); |
1 |
<span class="lnum"> 36: </span> } |
1 |
<span class="lnum"> 37: </span> |
1 |
<span class="lnum"> 38: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 39: </span> <span class="rem">/// The handler called when an exception is explicitly signaled.</span> |
1 |
<span class="lnum"> 40: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 41: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnErrorSignaled(<span class="kwrd">object</span> sender, ErrorSignalEventArgs args) |
1 |
<span class="lnum"> 42: </span> { |
1 |
<span class="lnum"> 43: </span> LogException(args.Exception, args.Context); |
1 |
<span class="lnum"> 44: </span> } |
1 |
<span class="lnum"> 45: </span> |
1 |
<span class="lnum"> 46: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 47: </span> <span class="rem">/// Logs an exception and its context to the error log.</span> |
1 |
<span class="lnum"> 48: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 49: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> LogException(Exception e, HttpContext context) |
1 |
<span class="lnum"> 50: </span> { |
1 |
<span class="lnum"> 51: </span> <span class="kwrd">if</span> (e == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 52: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"e"</span>); |
1 |
<span class="lnum"> 53: </span> |
1 |
<span class="lnum"> 54: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 55: </span> <span class="rem">// Fire an event to check if listeners want to filter out</span> |
1 |
<span class="lnum"> 56: </span> <span class="rem">// logging of the uncaught exception.</span> |
1 |
<span class="lnum"> 57: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 58: </span> |
1 |
<span class="lnum"> 59: </span> ExceptionFilterEventArgs args = <span class="kwrd">new</span> ExceptionFilterEventArgs(e, context); |
1 |
<span class="lnum"> 60: </span> OnFiltering(args); |
1 |
<span class="lnum"> 61: </span> |
1 |
<span class="lnum"> 62: </span> <span class="kwrd">if</span> (args.Dismissed) |
1 |
<span class="lnum"> 63: </span> <span class="kwrd">return</span>; |
1 |
<span class="lnum"> 64: </span> |
1 |
<span class="lnum"> 65: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 66: </span> <span class="rem">// Log away...</span> |
1 |
<span class="lnum"> 67: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 68: </span> |
1 |
<span class="lnum"> 69: </span> ErrorLogEntry entry = <span class="kwrd">null</span>; |
1 |
<span class="lnum"> 70: </span> |
1 |
<span class="lnum"> 71: </span> <span class="kwrd">try</span> |
1 |
<span class="lnum"> 72: </span> { |
1 |
<span class="lnum"> 73: </span> Error error = <span class="kwrd">new</span> Error(e, context); |
1 |
<span class="lnum"> 74: </span> ErrorLog log = GetErrorLog(context); |
1 |
<span class="lnum"> 75: </span> <span class="kwrd">string</span> id = log.Log(error); |
1 |
<span class="lnum"> 76: </span> entry = <span class="kwrd">new</span> ErrorLogEntry(log, id, error); |
1 |
<span class="lnum"> 77: </span> } |
1 |
<span class="lnum"> 78: </span> <span class="kwrd">catch</span> (Exception localException) |
1 |
<span class="lnum"> 79: </span> { |
1 |
<span class="lnum"> 80: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 81: </span> <span class="rem">// IMPORTANT! We swallow any exception raised during the </span> |
1 |
<span class="lnum"> 82: </span> <span class="rem">// logging and send them out to the trace . The idea </span> |
1 |
<span class="lnum"> 83: </span> <span class="rem">// here is that logging of exceptions by itself should not </span> |
1 |
<span class="lnum"> 84: </span> <span class="rem">// be critical to the overall operation of the application.</span> |
1 |
<span class="lnum"> 85: </span> <span class="rem">// The bad thing is that we catch ANY kind of exception, </span> |
1 |
<span class="lnum"> 86: </span> <span class="rem">// even system ones and potentially let them slip by.</span> |
1 |
<span class="lnum"> 87: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 88: </span> |
1 |
<span class="lnum"> 89: </span> Trace.WriteLine(localException); |
1 |
<span class="lnum"> 90: </span> } |
1 |
<span class="lnum"> 91: </span> |
1 |
<span class="lnum"> 92: </span> <span class="kwrd">if</span> (entry != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 93: </span> OnLogged(<span class="kwrd">new</span> ErrorLoggedEventArgs(entry)); |
1 |
<span class="lnum"> 94: </span> } |
1 |
<span class="lnum"> 95: </span> |
1 |
<span class="lnum"> 96: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 97: </span> <span class="rem">/// Raises the <see cref="Logged"/> event.</span> |
1 |
<span class="lnum"> 98: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 99: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnLogged(ErrorLoggedEventArgs args) |
1 |
<span class="lnum"> 100: </span> { |
1 |
<span class="lnum"> 101: </span> ErrorLoggedEventHandler handler = Logged; |
1 |
<span class="lnum"> 102: </span> |
1 |
<span class="lnum"> 103: </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 104: </span> handler(<span class="kwrd">this</span>, args); |
1 |
<span class="lnum"> 105: </span> } |
1 |
<span class="lnum"> 106: </span> |
1 |
<span class="lnum"> 107: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 108: </span> <span class="rem">/// Raises the <see cref="Filtering"/> event.</span> |
1 |
<span class="lnum"> 109: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 110: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnFiltering(ExceptionFilterEventArgs args) |
1 |
<span class="lnum"> 111: </span> { |
1 |
<span class="lnum"> 112: </span> ExceptionFilterEventHandler handler = Filtering; |
1 |
<span class="lnum"> 113: </span> |
1 |
<span class="lnum"> 114: </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 115: </span> handler(<span class="kwrd">this</span>, args); |
1 |
<span class="lnum"> 116: </span> } |
1 |
<span class="lnum"> 117: </span> |
1 |
<span class="lnum"> 118: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 119: </span> <span class="rem">/// Determines whether the module will be registered for discovery</span> |
1 |
<span class="lnum"> 120: </span> <span class="rem">/// in partial trust environments or not.</span> |
1 |
<span class="lnum"> 121: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 122: </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">bool</span> SupportDiscoverability |
1 |
<span class="lnum"> 123: </span> { |
1 |
<span class="lnum"> 124: </span> get { <span class="kwrd">return</span> <span class="kwrd">true</span>; } |
1 |
<span class="lnum"> 125: </span> } |
1 |
<span class="lnum"> 126: </span> } |
1 |
更多的功能等待您去挖掘,我们在实际开发中按需选择.希望这篇POST对您开发有帮助.<br /> |
转自:http://www.cnblogs.com/wintersun/archive/2011/11/12/2246513.html
View Detailsusing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string Path = Server.MapPath("1.txt"); string strings = "1232222"; FileAdd(Path, strings); } public static void FileAdd(string Path, string strings) { //实现一个System.IO.StreamWrite,使其以一种特定的编码向流中写入数据,AppendText数据追加到文件末尾 //这句也可以写成StreamWriter sw = new StreamWriter(Path, true, Encoding.GetEncoding("GB2312")); StreamWriter sw = File.AppendText(Path); //开始写入 sw.WriteLine(strings); //清理当前编码器的缓冲区,并将所有缓存数据写入基础流 sw.Flush(); sw.Close(); } } 转自:http://blog.sina.com.cn/s/blog_812ce0500100ywjb.html
View DetailsGlobal.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。下面让我们详细看一下如何在应用程序开发工作中使用这个文件。 概述Global.asax 位 于应用程序根目录下。虽然 Visual Studio .NET 会自动插入这个文件到所有的 ASP.NET 项目中,但是它实际上是一个可选文件。 删除它不会出问题——当然是在你没有使用它的情况下。.asax 文件扩展名指出它是一个应用程序文件,而不是一个使 用 aspx 的 ASP.NET 文件。Global.asax 文件被配置为任何(通过 URL 的)直接 HTTP 请求都被自动 拒绝,所以用户不能下载或查看其内容。ASP.NET 页面框架能够自动识别出对Global.asax 文件所做的任何更改。 在 Global.asax 被更改后ASP.NET 页面框架会重新启动应用程序,包括关闭所有的浏览器会话,去除所有状态信息,并重新启动应用程序 域。 编程Global.asax 文件继承自HttpApplication 类,它维护一个HttpApplication 对象池,并在需要时将对象池中的对象分配给应用程序。Global.asax 文件包含以下事件:Application_Init:在应用程序被实例化或第一次被调用时,该事件被触发。对于所有的HttpApplication 对象实例,它都会被调用。Application_Disposed:在应用程序被销毁之前触发。这是清除以前所用资源的理想位置。Application_Error:当应用程序中遇到一个未处理的异常时,该事件被触发。Application_Start:在HttpApplication 类的第一个实例被创建时,该事件被触发。它允许你创建可以由所有HttpApplication 实例访问的对象。Application_End:在HttpApplication 类的最后一个实例被销毁时,该事件被触发。在一个应用程序的生命周期内它只被触发一次。Application_BeginRequest:在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。Application_EndRequest:针对应用程序请求的最后一个事件。Application_PreRequestHandlerExecute:在 ASP.NET 页面框架开始执行诸如页面或 Web 服务之类的事件处理程序之前,该事件被触发。Application_PostRequestHandlerExecute:在 ASP.NET 页面框架结束执行一个事件处理程序时,该事件被触发。Applcation_PreSendRequestHeaders:在 ASP.NET 页面框架发送 HTTP 头给请求客户(浏览器)时,该事件被触发。Application_PreSendContent:在 ASP.NET 页面框架发送内容给请求客户(浏览器)时,该事件被触发。Application_AcquireRequestState:在 ASP.NET 页面框架得到与当前请求相关的当前状态(Session 状态)时,该事件被触发。Application_ReleaseRequestState:在 ASP.NET 页面框架执行完所有的事件处理程序时,该事件被触发。这将导致所有的状态模块保存它们当前的状态数据。Application_ResolveRequestCache:在 ASP.NET 页面框架完成一个授权请求时,该事件被触发。它允许缓存模块从缓存中为请求提供服务,从而绕过事件处理程序的执行。Application_UpdateRequestCache:在 ASP.NET 页面框架完成事件处理程序的执行时,该事件被触发,从而使缓存模块存储响应数据,以供响应后续的请求时使用。Application_AuthenticateRequest:在安全模块建立起当前用户的有效的身份时,该事件被触发。在这个时候,用户的凭据将会被验证。Application_AuthorizeRequest:当安全模块确认一个用户可以访问资源之后,该事件被触发。Session_Start:在一个新用户访问应用程序 Web 站点时,该事件被触发。 Session_End:在一个用户的会话超时、结束或他们离开应用程序 Web 站点时,该事件被触发。Application_Error 事件显示一个简单的消息用以说明发生的错误。 这个事件列表看起来好像多得吓人,但是在不同环境下这些事件可能会非常有用。使用这些事件的一个关键问题是知道它们被触发的顺序。Application_Init 和Application_Start 事件在应用程序第一次启动 时被触发一次。相似地,Application_Disposed 和 Application_End 事件在应用程序终止时被触发一次。此外,基于会 话的事件(Session_Start 和 Session_End)只在用户进入和离开站点时被使用。其余的事件则处理应用程序请求,这些事件被触发的 顺序是:Application_BeginRequestApplication_AuthenticateRequestApplication_AuthorizeRequestApplication_ResolveRequestCacheApplication_AcquireRequestStateApplication_PreRequestHandlerExecuteApplication_PreSendRequestHeadersApplication_PreSendRequestContent<<执行代码>>Application_PostRequestHandlerExecuteApplication_ReleaseRequestStateApplication_UpdateRequestCacheApplication_EndRequest 这些事件常被用于安全性方面。 转自:http://www.cnblogs.com/xiaopin/archive/2010/08/31/1813747.html
View Detailsweb.config是asp.net中保存配置信息(比如数据库连接字符串等)的重要文件。它是基于xml的文本文件方式放在Web应用程序的任何目录中,并且默认不随源文件编译到Dll中,而运行环境随时监视着它是否有改变,一但有变动,系统会自动重新加载里面的最新内容。 一、 配置文件保存位置 .net的默认配置文件保存在“windows目录\Microsoft.NET\Framework\对应.net版本\config”文件夹下面。不同的操作系统windows目录不一样,我们在命令行下输入“echo %windir%”查看windows目录所在的位置。 图:web.config所在的目录 Asp.net中有两个非常重要的配置文件,分别是machine.config和web.config,它们都位于config文件夹下面。这两个文件一般不需要我们手工是维护它,保持默认即可。但针对asp.net应用程序,它自身会有0个,1个或者多个web.config配置文件,多个配置文件会存在加载顺序问题。下节会介绍。 注意,传说中.net3.0和.net3.5只是在.net2.0的基础上扩充中,所以还是没用的.net2.0的配置文件。它们连config这个目录都没有。 二、 配置文件加载顺序 IIS在Asp.net网站启动时,会加载配置文件中的配置信息,然后缓存这些信息,不会每次要用都去读取配置文件,只是IIS会随时监视着这些文件的变化,一量有变化,它会重新去读取并缓存配置信息。 Asp.net网站运行时会按照以下方式加载配置文件中的节点信息: 1) 如果在当前运行页面所在的目录下有web.config文件,则查找是否存在所需要的节点,如果存在则返回结果,并停止下一步地查找。 2) 如里所在目录不存在web.config配置或者配置文件里没有所需要的节点,则查找它所在的上一级目录的配置文件中的节点,直到网站根目录。(问题:IIS6中的虚拟目录算不算根目录) 3) 如果网站根目录中都不存在web.config或者所需要的配置节点,转而到“windows目录\Microsoft.NET\Framework\对应.net版本\config\web.config”中去查找。 4) 如果第3条中还没找到,继续到“windows目录\Microsoft.NET\Framework\对应.net版本\config\machine.config”中去查找。 5) 如果还没找到,那就报错吧。 存在两个问题 1) IIS6中的虚拟目录算不算根目录。 2) 在系统运行时,在一个原本没有web.config的目录中手工加上一个web.config,会不会自动加载。 三、 配置文件节点介绍 Web.config文件是一个xml文本文件,它的根节点为<configuration>,该节点下包含常见的子节点有:<configSections>、<appSettings>、<connectionStrings>(保存数据库连接字符串)、<location>和<system.web>。下面针对各节点配置进行介绍。 1. <configSections> configSections 元素指定了配置节和处理程序声明。由于 ASP.NET 不对如何处理配置文件内的设置作任何假设,因此这非常必要。但 ASP.NET 会将配置数据的处理委托给配置节处理程序。配置结构信息如下: <configSections> <!--定义配置节处理程序与配置元素之间的关联。--> <section /> <!--定义配置节处理程序与配置节之间的关联。--> <sectionGroup /> <!--移除对继承的节和节组的引用。--> <remove /> <!--移除对继承的节和节组的所有引用,只允许由当前 section 和 sectionGroup 元素添加的节和节组。--> <clear/> </configSections> 每个 section 元素标识一个配置节或元素以及对该配置节或元素进行处理的关联 ConfigurationSection 派生类。可以在 sectionGroup 元素中对 section 元素进行逻辑分组,以对 section 元素进行组织并避免命名冲突。section 和 sectionGroup 元素包含在 configSections 元素中。 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素。 下面我们来示例写一个自定义配置信息,并完成它的SectionHandler,首先我们在<configuration>节点下添加configSections。 <configuration> <configSections> <sectionGroup name="mySectionGroup"> <section name="mySection" requirePermission="true" type="ConfigTest.SectionHandler.MySectionHandler,ConfigTest.SectionHandler" /> […]
View Details本文From hongshengpeng 解决ASP.NET 4.0 "A potentially dangerous Request.Form value was detected from the client". 错误在.net中,Request时出现有HTML或Javascript等字符串时,系统会认为是危险性值。立马报错。(在.aspx文件头中加入这句: <%@ Page validateRequest="false" %>,但还是出现相同错误) 或是修改web.config文件: <configuration> < system.web> <pages validateRequest="false" /> < /system.web> < /configuration> 但错误依旧。对.NET 4.0, 应该加上requestValidationMode="2.0" to the httpRuntime configuration section of the web.config :<httpRuntime requestValidationMode="2.0"/>(这句重要) 结构:<configuration> <system.web> <httpRuntime requestValidationMode="2.0">
View Details