- ·上一篇文章:利用 ASP.NET 的内置功能抵御 Web 攻击
- ·下一篇文章:用 WebClient.UploadData 方法 上载文件数据
.text urlRewrite介绍
里我详细分析下。
它实现了IHttpHandlerFactory
(看注释就知道这个类是很重要的了)
HttpModule处理完后(这句话并不正确,在这里是这样的)进入HttpHandler Factory,根据HttpHandler的配置内容我们可以马上找到这个类
UrlReWriteHandlerFactory它是处理重写URL请求核心,在这里我详细分析下。
它实现了IHttpHandlerFactory
(看注释就知道这个类是很重要的了)
using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using Dottext.Framework;
using Dottext.Framework.Components;
using Dottext.Framework.Configuration;
namespace Dottext.Common.UrlManager
{
/**//// <summary>
/// Class responisble for figuring out which .Text page to load. By default will load an array of Dottext.UrlManager.HttpHanlder
/// from the blog.config file. This contains a list of Regex patterns to match the current request to. It also allows caching of the
/// Regex's and Types
/// </summary>
public class UrlReWriteHandlerFactory: IHttpHandlerFactory
{
public UrlReWriteHandlerFactory(){} //Nothing to do in the cnstr
//自定义虚方法从自定义配置节内容反序列化时构造Httphandler
protected virtual HttpHandler[] GetHttpHandlers(HttpContext context)
{
return HandlerConfiguration.Instance().HttpHandlers;
}
/**//// <summary>
/// Implementation of IHttpHandlerFactory. By default, it will load an array of HttpHanlder (Dottext.UrlManager.HttpHandler) from
/// the blog.config. This can be changed, by overrideing the GetHttpHandlers(HttpContext context) method.
/// </summary>
/// <param name="context">Current HttpContext</param>
/// <param name="requestType">Request Type (Passed along to other IHttpHandlerFactory's)</param>
/// <param name="url">The current requested url. (Passed along to other IHttpHandlerFactory's)</param>
/// <param name="path">The physical path of the current request. Is not gaurenteed to exist (Passed along to other IHttpHandlerFactory's)</param>
/// <returns>
/// Returns an Instance of IHttpHandler either by loading an instance of IHttpHandler or by returning an other
/// IHttpHandlerFactory.GetHanlder(HttpContext context, string requestType, string url, string path) method
/// </returns>
//实现接口IHttpHandlerFactory定义的方法
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string path)
{
//Get the Handlers to process. By defualt, we grab them from the blog.config
HttpHandler[] items = GetHttpHandlers(context);
//Dottext.Framework.Logger.LogManager.Log("path",Dottext.Framework.Util.Globals.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath));
//Do we have any?
if(items != null)
{
int count = items.Length;
for(int i = 0; i<count; i++)
{
//We should use our own cached Regex. This should limit the number of Regex's created
//and allows us to take advantage of RegexOptons.Compiled
//逐个匹配所配置节中定义的请求类型
if(items[i].IsMatch(Dottext.Framework.Util.Globals.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath)))
{
//注意这里是关键,注意返回的Httphandler实例
//throw new Exception();
switch(items[i].HandlerType)
{
case HandlerType.Page://默认是Page
return ProccessHandlerTypePage(items[i],context,requestType,url);
case HandlerType.Direct:
HandlerConfiguration.SetControls(context,items[i].BlogControls);
return (IHttpHandler)items[i].Instance();
case HandlerType.Factory:
//Pass a long the request to a custom IHttpHandlerFactory
return ((IHttpHandlerFactory)items[i].Instance()).GetHandler(context,requestType,url,path);
default:
throw new Exception("Invalid HandlerType: Unknown");
}
}
}
}
//If we do not find the page, just let ASP.NET take over
return PageHandlerFactory.GetHandler(context,requestType,url, path);
}
private IHttpHandler ProccessHandlerTypePage(HttpHandler item, HttpContext context, string requestType, string url)
{
string pagepath = item.FullPageLocation;
if(pagepath == null)
{
pagepath = HandlerConfiguration.Instance().FullPageLocation;
}
HandlerConfiguration.SetControls(context,item.BlogControls);
IHttpHandler myhandler=PageParser.GetCompiledPageInstance(url,pagepath,context);
它实现了IHttpHandlerFactory
(看注释就知道这个类是很重要的了)
HttpModule处理完后(这句话并不正确,在这里是这样的)进入HttpHandler Factory,根据HttpHandler的配置内容我们可以马上找到这个类
UrlReWriteHandlerFactory它是处理重写URL请求核心,在这里我详细分析下。
它实现了IHttpHandlerFactory
(看注释就知道这个类是很重要的了)
using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using Dottext.Framework;
using Dottext.Framework.Components;
using Dottext.Framework.Configuration;
namespace Dottext.Common.UrlManager
{
/**//// <summary>
/// Class responisble for figuring out which .Text page to load. By default will load an array of Dottext.UrlManager.HttpHanlder
/// from the blog.config file. This contains a list of Regex patterns to match the current request to. It also allows caching of the
/// Regex's and Types
/// </summary>
public class UrlReWriteHandlerFactory: IHttpHandlerFactory
{
public UrlReWriteHandlerFactory(){} //Nothing to do in the cnstr
//自定义虚方法从自定义配置节内容反序列化时构造Httphandler
protected virtual HttpHandler[] GetHttpHandlers(HttpContext context)
{
return HandlerConfiguration.Instance().HttpHandlers;
}
/**//// <summary>
/// Implementation of IHttpHandlerFactory. By default, it will load an array of HttpHanlder (Dottext.UrlManager.HttpHandler) from
/// the blog.config. This can be changed, by overrideing the GetHttpHandlers(HttpContext context) method.
/// </summary>
/// <param name="context">Current HttpContext</param>
/// <param name="requestType">Request Type (Passed along to other IHttpHandlerFactory's)</param>
/// <param name="url">The current requested url. (Passed along to other IHttpHandlerFactory's)</param>
/// <param name="path">The physical path of the current request. Is not gaurenteed to exist (Passed along to other IHttpHandlerFactory's)</param>
/// <returns>
/// Returns an Instance of IHttpHandler either by loading an instance of IHttpHandler or by returning an other
/// IHttpHandlerFactory.GetHanlder(HttpContext context, string requestType, string url, string path) method
/// </returns>
//实现接口IHttpHandlerFactory定义的方法
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string path)
{
//Get the Handlers to process. By defualt, we grab them from the blog.config
HttpHandler[] items = GetHttpHandlers(context);
//Dottext.Framework.Logger.LogManager.Log("path",Dottext.Framework.Util.Globals.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath));
//Do we have any?
if(items != null)
{
int count = items.Length;
for(int i = 0; i<count; i++)
{
//We should use our own cached Regex. This should limit the number of Regex's created
//and allows us to take advantage of RegexOptons.Compiled
//逐个匹配所配置节中定义的请求类型
if(items[i].IsMatch(Dottext.Framework.Util.Globals.RemoveAppFromPath(context.Request.Path,context.Request.ApplicationPath)))
{
//注意这里是关键,注意返回的Httphandler实例
//throw new Exception();
switch(items[i].HandlerType)
{
case HandlerType.Page://默认是Page
return ProccessHandlerTypePage(items[i],context,requestType,url);
case HandlerType.Direct:
HandlerConfiguration.SetControls(context,items[i].BlogControls);
return (IHttpHandler)items[i].Instance();
case HandlerType.Factory:
//Pass a long the request to a custom IHttpHandlerFactory
return ((IHttpHandlerFactory)items[i].Instance()).GetHandler(context,requestType,url,path);
default:
throw new Exception("Invalid HandlerType: Unknown");
}
}
}
}
//If we do not find the page, just let ASP.NET take over
return PageHandlerFactory.GetHandler(context,requestType,url, path);
}
private IHttpHandler ProccessHandlerTypePage(HttpHandler item, HttpContext context, string requestType, string url)
{
string pagepath = item.FullPageLocation;
if(pagepath == null)
{
pagepath = HandlerConfiguration.Instance().FullPageLocation;
}
HandlerConfiguration.SetControls(context,item.BlogControls);
IHttpHandler myhandler=PageParser.GetCompiledPageInstance(url,pagepath,context);
