UrlRewriter 实现伪静态
一.摘要
为了实现URL的友好和提高SEO ,在开发中经常需要重写Url,有时为了Url的友好在传递参数的时候也会使用Session来传递。
二.前言
当客户端请求一个*.aspx页面文件时,http请求会被inetinfo.exe进程截获,根据我们的IIS映射配置,会把请求给指定的DLL进行处理,ASP.NET 页面会被 ASPNET_ISAPI.DLL 而ASPNET_ISAPI.DLL则会通过一个http pipeline的管道,将这个Http请求发送给ASPNET_WP.EXE进程。这时候ASP.NET Framework 会通过HTTP Rutime来处理这个Http请求,处理完毕后将结果返回给客户端。
完整的ASP.NET HTTP请求
HttpRequest-->inetinfo.exe->
ASPNET_ISAPI.DLL-->HttpPipeline-->ASPNET_WP.EXE-->
HttpRuntime-->HttpApplicationFactory-->HttpApplication-->HttpModule-->HttpHandlerFactory-->HttpHandler-->HttpHandler.ProcessRequest()
三 ASP.NET的HttpModule
ASP.NET系统中默认的HttpModule,系统自身实现了一个IhttpModule的接口,如果需要替代可以定义自己的HttpModule对象。
默认的HTTPModule:
DefaultAuthenticationModule 确保上下文中存在 Authentication 对象。
FileAuthorizationModule 验证远程用户是否具有访问所请求文件的 NT 权限。
FormsAuthenticationModule 启用 ASP.NET 应用程序以使用 Forms 身份验证。
PassportAuthenticationModule 提供环绕 PassportAuthentication 服务的包装。
SessionStateModule 为应用程序提供会话状态服务。
UrlAuthorizationModule 提供基于 URL 的授权服务以允许或拒绝对指定资源的访问。
WindowsAuthenticationModule 启用 ASP.NET 应用程序以使用 Windows/IIS 身份验证。
系统默认的HttpModule是在文件machine.config中配置的,和我们开发时使用到的web.config的关系是:是在ASP.NET FRAMEWORK启动处理一个Http Request的时候,它会依次加载machine.config和请求页面所在目录的web.config文件,如果在machine中配置了一个自己的HttpModule,你仍然可以在所在页面的web.config文件中remove掉这个映射关系。
深入HttpModule
一个Http请求在被ASP.NET Framework捕获之后会依次交给HttpModule以及HttpHandler来处理。HttpModule与Http之间不是完全独立的,实际上,http请求在HttpModule传递的过程中会在某个事件内将控制权转交给HttpHandler的,而真正的处理在HttpHandler中执行完成后,HttpHandler会再次将控制权交还给HttpModule
上面的代码中的HttpModule的Init()中的参数是HttpApplication类型,它具有许多事件,包括BeginRequest,EndRequest,AuthentiacteRequest 等等。
四 使用自定义HTTPModule实现Url重写
添加配置
<httpModules>
<add name="UrlReWriter" type="UrlReWriter"/>
</httpModules>
实现自己的HttpModule
public class UrlReWriter:IHttpModule
{
public UrlReWriter()
{
//
//
//
}
#region IHttpModule
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path);
Regex regex=new Regex("UserInfo(\\d+).aspx",RegexOptions.Compiled);
Match match=regex.Match(file);
if(match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = "UserInfo.aspx?UserId=" + userId;
context.RewritePath(rewritePath);
}
}
}
Net实现Url重写,伪静态,IIS7直接支持重写模块
获得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true%20
下载完毕后,导入工程,我这里没有对该工程做任何修改,保留了原来的重写方。
配置和使用
1、.net程序里面需要引用文件Intelligencia.UrlRewriter
2、Web.config里面进行配置,大致如下:
<configuration>
<configSections>
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
<rewriter>
<!--pages-->
<rewrite url="^~/Pages/(\w+).html" to="~/Pages/$1.aspx" processing="stop"/>
</rewriter>
<system.web>
<httpModules>
<add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter"/>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
</configuration>
3、IIS配置,需要配置两个地方
选择站点->属性->主目录->配置->这里需要配置两个地方
1、应用程序扩展->添加->
可执行文件:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
扩展名.html
确定文件是否存在,前面勾去掉
2、通配符应用程序映射里面插入,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
确定文件是否存在,前面勾去掉