URL重写不支持相对路径该怎么办?

URL重写已经很普遍了,但基本上大部分的URL重写都不支持页面的相对路径,所有如果想在已经开发好的项目中添加还是有压力的,第二就是例如微软的那个URL重写是根据正则表达式来处理的,那样是很好,但也有不足之处,就是不方便定位到某个页面只能有哪些参数。

  我觉得要解决的问题有一下几个:

  1、解决如图片js等不能使用相对路径的文件

  2、解决某个页面能有几个参数和哪些参数是可选的

  下面就是解决掉这些问题了

  添加处理程序MyHttpModule,下面是我的一个简单的处理程序(我只是做了一个简单的,并没有考虑性能,而且我是写死的一个url重写就是重写成没有扩展名的)

以下是代码片段:
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text;

namespace MyClass
{
public class MyHttpModule : IHttpModule
{
#region IHttpModule 成员
///<summary>
/// 释放所有资源
///</summary>
public void Dispose()
{
}
///<summary>
/// 初始化模块,并使其为处理请求做好准备
///</summary>
///<param name="context"> 一个 System.Web.HttpApplication,它提供对 ASP.NET 应用程序内所有应用程序对象的公用的方法、属性和事件的访问</param>
public void Init(HttpApplication context)
{
context.AuthorizeRequest += new
EventHandler(this.BaseModuleRewriter_AuthorizeRequest);
}
///<summary>
/// 当安全模块已验证用户授权时发生
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected virtual void BaseModuleRewriter_AuthorizeRequest(
object sender, EventArgs e)
{
System.Web.HttpApplication app = (System.Web.HttpApplication)sender;
Rewrite(app.Request.Path, app);
}
///<summary>
/// 重写url
///</summary>
///<param name="requestedPath">url的虚拟路径</param>
///<param name="app"></param>
protected void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
List<string> 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());//其它文件则使用找到的路径
}
///<summary>
/// 获取url对应的绝对路径和虚拟路径及查询参数
///</summary>
///<param name="context"></param>
///<param name="virtualPath">虚拟路径</param>
///<param name="qeryString">查询参数 如果为null请取HttpContext.Request.QueryString</param>
///<returns>url对应的绝对路径</returns>
public static string GetInputFile(HttpContext context, out string virtualPath, out List<string> qeryString)
{
string executionFilePath = context.Request.AppRelativeCurrentExecutionFilePath.Remove(0, 2);//获取当前对应的虚拟路径并干掉“~/”
string inputFile = context.Request.PhysicalPath;//获取当前url对于的绝对路径
virtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
qeryString = null;
List<string> qeryList = new List<string>();
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;
}
///<summary>
/// 获取指定目录+文件是否有效
///</summary>
///<param name="inputFile">目录</param>
///<param name="fileName"></param>
///<param name="extension"></param>
///<returns></returns>
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
}
}

转载于:https://www.cnblogs.com/webearly/archive/2012/02/04/2338106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值