TheBeerHouse 系列二:web.config的魅力

这个跟以前~~介绍过的 Web设置 类似,只是这里的Web设置更好点- -先看下web.config片断

None.gif < configuration  xmlns ="http://schemas.microsoft.com/.NetConfiguration/v2.0" >
None.gif    
< configSections >
None.gif        
< section  name ="theBeerHouse"  type ="MB.TheBeerHouse.TheBeerHouseSection, __code" />
None.gif    
</ configSections >
None.gif  
None.gif   
< theBeerHouse  defaultConnectionStringName ="LocalSqlServer" >
None.gif      
< contactForm  mailTo ="thebeerhouse@wrox.com" />
None.gif       
<!-- 默认显示商品的数量 -->
None.gif       
< articles  pageSize ="10"   />
None.gif       
< polls  archiveIsPublic ="true"  votingLockByIP ="false"   />
None.gif      
< newsletters  fromEmail ="thebeerhouse@wrox.com"  fromDisplayName ="TheBeerHouse"   />
None.gif      
< forums  threadsPageSize ="8"  hotThreadPosts ="10"   bronzePosterPosts ="10"  silverPosterPosts ="20"  goldPosterPosts ="50"   />
None.gif      
< store  sandboxMode ="true"  businessEmail ="thebeerhouse@wrox.com"   />
None.gif   
</ theBeerHouse >
None.gif    
开始- -分析首先是 <section name="theBeerHouse" type="MB.TheBeerHouse.TheBeerHouseSection, __code"/> Web设置 讲过了~~就是对应一个类.


 <theBeerHouse defaultConnectionStringName="LocalSqlServer">.....</theBeerHouse>这个是跟上面name对应的节点 Web设置也有详细说明~~不废话~~首先看看类图然后再分析下哪里不同咯
ThenB2.gif
说明下:凡是有对应类的属性都是嵌套节点哦 14.gif
让我来看看TheBeerHouseSection类的定义- -注意看~~有注释懒得打了~~
ContractedBlock.gif ExpandedBlockStart.gif TheBeerHouseSection
ExpandedBlockStart.gifContractedBlock.gif  /**//// <summary>
InBlock.gif    
/// 配置节点管理类
ExpandedBlockEnd.gif    
/// </summary>

None.gif   public class TheBeerHouseSection : ConfigurationSection
ExpandedBlockStart.gifContractedBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 默认的连接对象
ExpandedSubBlockEnd.gif       
/// </summary>

InBlock.gif      [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalSqlServer")]
InBlock.gif      
public string DefaultConnectionStringName
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (string)base["defaultConnectionStringName"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gifbase["connectionStdefaultConnectionStringNameringName"= value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// 默认的换冲对象
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      [ConfigurationProperty("defaultCacheDuration", DefaultValue = "600")]
InBlock.gif      
public int DefaultCacheDuration
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (int)base["defaultCacheDuration"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gifbase["defaultCacheDuration"= value; }
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// 接点-主要配置电子邮件
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      [ConfigurationProperty("contactForm", IsRequired=true)]
InBlock.gif      
public ContactFormElement ContactForm
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (ContactFormElement) base["contactForm"]; }
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 节点主要配置显示上商品的页面默认,显示几个商品
ExpandedSubBlockEnd.gif       
/// </summary>

InBlock.gif      [ConfigurationProperty("articles", IsRequired = true)]
InBlock.gif      
public ArticlesElement Articles
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (ArticlesElement)base["articles"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"polls", IsRequired = true)]
InBlock.gif      
public PollsElement Polls
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (PollsElement)base["polls"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"newsletters", IsRequired = true)]
InBlock.gif      
public NewslettersElement Newsletters
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (NewslettersElement)base["newsletters"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"forums", IsRequired = true)]
InBlock.gif      
public ForumsElement Forums
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (ForumsElement)base["forums"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"store", IsRequired = true)]
InBlock.gif      
public StoreElement Store
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (StoreElement)base["store"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 网战配置程序
ExpandedSubBlockEnd.gif       
/// </summary>

InBlock.gif       [ConfigurationProperty("pageProvider", IsRequired = true)]
InBlock.gif       
public PageElement PageProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif          
get dot.gifreturn (PageElement)base["pageProvider"]; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif       
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif   }
其他的部分,只写一个~~不占用版面了~~~
ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
InBlock.gif    
/// 配置电子邮件
ExpandedBlockEnd.gif    
/// </summary>

None.gif     public   class  ContactFormElement : ConfigurationElement
ExpandedBlockStart.gifContractedBlock.gif   
dot.gif {
InBlock.gif      [ConfigurationProperty(
"mailSubject", DefaultValue="Mail from TheBeerHouse: {0}")]
InBlock.gif      
public string MailSubject
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (string)base["mailSubject"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gifbase["mailSubject"= value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"mailTo", IsRequired=true)]
InBlock.gif      
public string MailTo
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (string)base["mailTo"];  }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gifbase["mailTo"= value;  }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      [ConfigurationProperty(
"mailCC")]
InBlock.gif      
public string MailCC
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn (string)base["mailCC"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gifbase["mailCC"= value; }
ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif   }

Ok--这就是主要的节点结构.让我们继续往下走:
None.gif < authentication  mode ="Forms" >
None.gif            
< forms  cookieless ="AutoDetect"  loginUrl ="~/AccessDenied.aspx"  name ="TBHFORMAUTH" />
None.gif        
</ authentication >

上面这段代码有意思,他的作用是结合权限验证的如果没有经过权限验证则定位到某某页,其实我更倾向于下面这样写利用HttpHandler来自动捕捉

None.gif
None.gif
public   class  Bt : IHttpHandlerFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//其实就是HttpHandler{
InBlock.gif
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
InBlock.gif    
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
string sendToUrl = url;                         //地址栏里面的地址
InBlock.gif
            System.Collections.Generic.List<string> UrlSqlit = new System.Collections.Generic.List<string>();
InBlock.gif            UrlSqlit.AddRange(url.Split(
'/'));
InBlock.gif
InBlock.gif            
string filePath = pathTranslated;
InBlock.gif
InBlock.gif            
string sendToURLString;
InBlock.gif
InBlock.gif
InBlock.gif            
if(UrlSqlit[UrlSqlit.Count-1]=="Admin.aspx")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
if(Page.User.Identity.IsAuthenticated&&Roles.IsUserInRole(Page.User.Identity,"Admin")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                  sendToURLString
= "~/Admin.aspx";  //真正要访问的页面
ExpandedSubBlockEnd.gif
                }
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                        
InBlock.gif                sendToURLString
="~/AccessDenied.aspx";
InBlock.gif            
ExpandedSubBlockEnd.gif                 }

InBlock.gif            
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
string queryString ="";                  //参数。比如?id=123
InBlock.gif
InBlock.gif     
InBlock.gif
InBlock.gif            
//这句最重要了。转向了。
InBlock.gif
            context.RewritePath(sendToURLString, String.Empty, queryString);
InBlock.gif
InBlock.gif            
//这个还没有弄明白:)---返回新的编译实例
InBlock.gif
            return PageParser.GetCompiledPageInstance(url, filePath, context);
ExpandedSubBlockEnd.gif        }

InBlock.gif    
//---------------------------------使工厂可以重用现有的处理程序实例。
InBlock.gif
    public virtual void ReleaseHandler(IHttpHandler handler)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
这是配置~文件
None.gif >
None.gif        
< httpHandlers > <!-- verb是处理的请求类型*是所有,GET,POST, PATH=处理的类型,typehttpHandLers -->
None.gif            
< add  verb ="*"  path ="*.aspx"  type ="Bt" />
None.gif        
</ httpHandlers >

---跑题了我们继续-说说Web事件~`首先看个类
ThenB3.gif
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Web.Management;
None.gif
None.gif
namespace  MB.TheBeerHouse
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public abstract class WebCustomEvent : WebBaseEvent 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="message">引发的事件的说明。</param>
InBlock.gif       
/// <param name="eventSource">引发事件的对象。</param>
ExpandedSubBlockEnd.gif       
/// <param name="eventCode">与该事件关联的代码。实现自定义事件时,事件代码必须大于WebExtendedBase。</param>

InBlock.gif      public WebCustomEvent(string message, object eventSource, int eventCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif         : 
base(message, eventSource, eventCode) dot.gif{ }
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public class RecordDeletedEvent : WebCustomEvent 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
private const int eventCode = WebEventCodes.WebExtendedBase + 10;//自定义WebEventCodes.WebExtendedBase事件必须大于
InBlock.gif
      private const string message = "The {0} with ID = {1} was deleted by user {2}.";//这里做的好动静结合太极拳的味道
InBlock.gif

InBlock.gif       
public RecordDeletedEvent(string entity, int id, object eventSource)//HttpContext.Current.User.Identity.Name获取当前用户的名称
InBlock.gif
         : base(string.Format(message, entity, id, HttpContext.Current.User.Identity.Name), eventSource, eventCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{ }
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}


--接下来就是配置~~都有注释的慢慢看~~
None.gif    < healthMonitoring  heartbeatInterval ="10800"   > //针对运行状况监视配置应用程序,必选的 TimeSpan 属性。
None.gif
None.gif       //heartbeatInterval指定时间间隔,即引发 WebHeartbeatEvent 事件的频率(以秒为单位)。 默认值为 "00:00:00",它表示不引发 WebHeartbeatEvent 事件。
None.gif 
None.gif         
< providers >
None.gif            
< remove  name ="SqlWebEventProvider"   />
None.gif            
< add  name ="SqlWebEventProvider"  connectionStringName ="LocalSqlServer" //buffer定义提供程序的缓冲功能。
None.gif               buffer
="false"  bufferMode ="Notification"  maxEventDetailsLength ="1073741823" //bufferMode定义提供程序的缓冲功能。
None.gif               type
="System.Web.Management.SqlWebEventProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"   />            
None.gif         
</ providers >
None.gif         
< eventMappings > //可选的元素。将事件友好名称映射到相关的事件类型。
None.gif
None.gif            
< add  name ="TBH Events"  type ="MB.TheBeerHouse.WebCustomEvent, MB.TheBeerHouse.CustomEvents"   />             
None.gif         
</ eventMappings >
None.gif         
< rules >
None.gif            
< clear  /> //就是可以触发检测的程序配置--SqlWebEventProvider这个是值得记录到Sql中--记录Window到日志中--EventLogWebEventProvider 
None.gif            
< add  name ="TBH Events"  eventName ="TBH Events"  provider ="SqlWebEventProvider"  profile ="Critical"   />             
None.gif            
< add  name ="All Errors"  eventName ="All Errors"  provider ="SqlWebEventProvider"  profile ="Critical"   />
None.gif            
< add  name ="Failure Audits"  eventName ="Failure Audits"  provider ="SqlWebEventProvider"  profile ="Critical"   />
None.gif            
< add  name ="Heartbeats"  eventName ="Heartbeats"  provider ="SqlWebEventProvider"  profile ="Critical"   />
None.gif         
</ rules >
None.gif      
</ healthMonitoring >   

//--------------。与内置的 ASP.NET 运行状况监视事件不同的是,自定义事件必须显式引发。
要引发自定义事件必须

 RecordDeletedEvent swre =new  RecordDeletedEvent ("Heartbeats", this, myCode);//--参数1是eventName
                // --运行这个引发自定义事件
  swre.Raise();

转载于:https://www.cnblogs.com/ajaxren/archive/2007/05/12/743854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值