一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

基于asp.net MyHttpModule url重写方法

时间:2011-11-21 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text;
namespace MyClass
{
public class MyHttpModule : IHttpModule
{
#region IHttpModule 成员
///


/// 释放所有资源
///

public void Dispose()
{
}
///
/// 初始化模块,并使其为处理请求做好准备
///

/// 一个 System.Web.HttpApplication,它提供对 ASP.NET 应用程序内所有应用程序对象的公用的方法、属性和事件的访问
public void Init(HttpApplication context)
{
context.AuthorizeRequest += new
EventHandler(this.BaseModuleRewriter_AuthorizeRequest);
}
///
/// 当安全模块已验证用户授权时发生
///

///
///
protected virtual void BaseModuleRewriter_AuthorizeRequest(
object sender, EventArgs e)
{
System.Web.HttpApplication app = (System.Web.HttpApplication)sender;
Rewrite(app.Request.Path, app);
}
///
/// 重写url
///

///requestedPath">url的虚拟路径
///
protected void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
List qeryString;
string virtualPath;
string inputFile = GetInputFile(app.Context, out virtualPath, out qeryString);//获取到真实的文件信息
if (System.IO.Path.GetExtension(inputFile) == ".aspx")//如果是aspx文件 那么则把保留重写的url
{
app.Context.RewritePath(requestedPath, string.Empty, string.Empty);//这里查询参数我没去处理了,也就是Request.QueryString的信息,如果取qeryString 然后去处理成一个字符串
return;
}
app.Context.RewritePath(virtualPath, string.Empty, app.Context.Request.QueryString.ToString());//其它文件则使用找到的路径
}
///
/// 获取url对应的绝对路径和虚拟路径及查询参数
///

///
///虚拟路径
///查询参数 如果为null请取HttpContext.Request.QueryString
///url对应的绝对路径
public static string GetInputFile(HttpContext context, out string virtualPath, out List qeryString)
{
string executionFilePath = context.Request.AppRelativeCurrentExecutionFilePath.Remove(0, 2);//获取当前对应的虚拟路径并干掉“~/”
string inputFile = context.Request.PhysicalPath;//获取当前url对于的绝对路径
virtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
qeryString = null;
List qeryList = new List();
if (!File.Exists(inputFile))//判断文件是否存在,也就是没有被重写的url获取使用绝对路径的资源等等
{
bool b = false;
string fileName;
string extension;
string applicationPath = context.Request.PhysicalApplicationPath;//获取网站的跟目录
var tempPath = GetFileInfo(inputFile, out fileName, out extension);
while (!b)//根据目录循环获取有效的文件目录
{
b = File.Exists(tempPath + "\" + extension);//判断文件是否存在
if (tempPath + "\" == applicationPath)//如果查找到根目录还没有查找到那么则不需要在查了
{
break;
}
if (!string.IsNullOrWhiteSpace(fileName))
{
qeryList.Add(fileName);//如果不存在那么这个就是参数 例如http://localhost:4688/WebForm1/2011/ (对应http://localhost:4688/WebForm1.aspx?xxx=2011)
}
tempPath = GetFileInfo(tempPath, out fileName, out extension);
}
if (b)//如果查找到了就把查找到的路径复制给输出或返回参数
{
inputFile = tempPath + "\" + extension;
virtualPath = "~/" + inputFile.Replace(applicationPath, null);
}
if (Path.GetExtension(extension) == ".aspx")//如果是asp.net那么则把list复制给输出参数 qeryString
{
qeryString = qeryList;
}
}
return inputFile;
}
///
/// 获取指定目录+文件是否有效
///

///目录
///
///
///
private static string GetFileInfo(string inputFile, out string fileName, out string extension)
{
var tempPath = Directory.GetParent(inputFile).FullName;//获取传进来目录的父目录
fileName = inputFile.Replace(tempPath + "\", null);//获取子目录名称
extension = Path.GetExtension(inputFile);//获取扩展名
if (string.IsNullOrWhiteSpace(extension))//如果扩展名为null那么则认为是aspx文件
{
extension = fileName + ".aspx";
}
else
{
extension = fileName + extension;
}
return tempPath;
}
#endregion
}
}

热门栏目