zoukankan
html css js c++ java
利用IHttpModule实现URL地址转发功能
using
System;
using
System.Web;
using
System.Text.RegularExpressions;
namespace
WebControlLibrary1
{
/**/
///
<summary>
///
BaseModuleRewriter 的摘要说明。
///
</summary>
public
abstract
class
BaseModuleRewriter:IHttpModule
{
public
BaseModuleRewriter()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
IHttpModule 成员
#region
IHttpModule 成员
public
virtual
void
Init(HttpApplication app)
{
app.AuthorizeRequest
+=
new
EventHandler(app_AuthorizeRequest);
}
public
virtual
void
Dispose()
{
//
TODO: 添加 BaseModuleRewriter.Dispose 实现
}
#endregion
protected
virtual
void
app_AuthorizeRequest(
object
sender, EventArgs e)
{
HttpApplication app
=
(HttpApplication)sender;
this
.Rewrite(app.Request.Path,app);
}
protected
abstract
void
Rewrite(
string
requestedPath, HttpApplication app);
}
public
class
ModulRewriter:BaseModuleRewriter
{
protected
override
void
Rewrite(
string
requestedPath, HttpApplication app)
{
string
strPath
=
requestedPath;
string
strFileName
=
strPath.Substring(strPath.LastIndexOf(
"
/
"
)
+
1
);
string
strReg
=
@"
^\d+
"
;
Regex reg
=
new
Regex(strReg,RegexOptions.IgnoreCase);
if
(reg.IsMatch(strFileName))
{
string
strTruePath
=
strPath.Remove(strPath.LastIndexOf(
"
/
"
)
+
1
,strFileName.Length);
strTruePath
=
strTruePath
+
"
go.aspx?id=
"
+
reg.Match(strFileName).Value;
HttpContext.Current.RewritePath(strTruePath);
//
app.Server.Execute(strTruePath);
}
else
{
//
app.Server.Execute(strPath);
HttpContext.Current.RewritePath(strPath);
}
}
}
}
其实网上已经有很多人都实现了,但我现在自己实现了一下,以加深印象。在web.config中写入:
<httpModules>
<add type="WebControlLibrary1.ModulRewriter, WebControlLibrary1" name="ModuleRewriter" />
</httpModules>
即可。
查看全文
相关阅读:
HTML撑起浮动子元素得父元素高度
H5弃用标签和属性
HTML常用转义字符
php微信公众号开发入门
常见正则表达式总结
解决上下两个相邻图片之间存在默认间距的问题
移动端真机调试的两种方法
H5使用小结
CF 11D
Codeforces Round #639 (Div. 2) C Hilbert's Hotel (数学)
原文地址:https://www.cnblogs.com/sxlfybb/p/585138.html
最新文章
使用js获取当前页面的url网址信息。
如何用js获取日期(转载)
常见css兼容问题
Javascript Object、Function对象
HTML插入Flash的全兼容完美解决方案-SWFObject
overflow:hidden真的失效了吗?
网页右下角弹出窗体
jquery技巧总结
六、Django学习:设置博客列表和博客详情
五:Django学习:开始搭建个人博客网站
热门文章
四、Django学习:定义后台与修改模型
三、Django学习:使用模板显示内容
二、Django学习:创建App模板
一、Django学习:浏览器显示helloworld
python执行js代码时编码报错问题
scrapy的一些容易忽视的点(模拟登陆,传递item等)
爬虫抓取表格数据思路(含合并单元格)
python中if和elif的区别
js特效—省市级联
HTML动态操作表格
Copyright © 2011-2022 走看看