用网上下载的UrlRewriteModule URL重写组件发现有时在打开页面的时候会出错(错误一时没有记下来·!!),但是再刷新页面的时候又好了,于是想自己写一个URL重写的,以得以前看视 频的时候说是要URL重写的话可在Module中写,上网查了一下相关的资料,写了一个简单的Module来进行URL重写,记录如下,以备后用:
源文件下载
/Files/niunan/Module.zip
<
httpModules
>
<!-- 测试 -->
< add name ="ModuleTest" type ="ModouleTest" />
<!-- 测试结束 -->
</ httpModules >
ModouleTest.cs
<!-- 测试 -->
< add name ="ModuleTest" type ="ModouleTest" />
<!-- 测试结束 -->
</ httpModules >
/*
* 作者: 牛腩
* 创建时间: 2009-9-2 9:59:30
* Email: 164423073@qq.com
* 说明: URL重写Module
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
/// URL重写Module
/// </summary>
public class ModouleTest : IHttpModule
{
public ModouleTest()
{
}
#region IHttpModule 成员
void IHttpModule.Dispose()
{
throw new NotImplementedException();
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
#endregion
private void Application_BeginRequest( object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
HttpServerUtility server = application.Server;
string url = request.Url.ToString();
/*
* 只要输入的地址中有niunantest/的都会跳转并把/后头的字符传过去,如:
* http://localhost :3212/ModuleTest/niunantest/43242
* http://localhost :3212/ModuleTest/niunantest/abd3345
* http://localhost :3212/ModuleTest/niunantest/牛腩
*/
Regex reg = new Regex( @" niunantest/(\w+) " );
if (reg.IsMatch(url))
{
Match m = reg.Match(url);
string value = m.Groups[ 1 ].Value;
// response.Write("匹配的值:" + value + "<br>");
server.Transfer( " ~/default.aspx?str= " + value); // 使用response.Redirect会让地址栏中的地址改变
}
}
}
个人理解,其实module就相当于jsp中的过滤器(如果有的话),每个页面在呈现之前必须得先经过module,那么我们就可以在module中判断 当前的URL地址,如果符合某个格式的就可以跳转到对应的页面,在这里用到了server.Transfer跳转是为了不让地址栏中的地址改变。
* 作者: 牛腩
* 创建时间: 2009-9-2 9:59:30
* Email: 164423073@qq.com
* 说明: URL重写Module
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
/// URL重写Module
/// </summary>
public class ModouleTest : IHttpModule
{
public ModouleTest()
{
}
#region IHttpModule 成员
void IHttpModule.Dispose()
{
throw new NotImplementedException();
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
#endregion
private void Application_BeginRequest( object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
HttpServerUtility server = application.Server;
string url = request.Url.ToString();
/*
* 只要输入的地址中有niunantest/的都会跳转并把/后头的字符传过去,如:
* http://localhost :3212/ModuleTest/niunantest/43242
* http://localhost :3212/ModuleTest/niunantest/abd3345
* http://localhost :3212/ModuleTest/niunantest/牛腩
*/
Regex reg = new Regex( @" niunantest/(\w+) " );
if (reg.IsMatch(url))
{
Match m = reg.Match(url);
string value = m.Groups[ 1 ].Value;
// response.Write("匹配的值:" + value + "<br>");
server.Transfer( " ~/default.aspx?str= " + value); // 使用response.Redirect会让地址栏中的地址改变
}
}
}
源文件下载
/Files/niunan/Module.zip