URL重写及干掉ASP.NET试图状态的实现方法

网络安全 2025-04-24 20:25www.168986.cn网络安全知识

一、解决无法使用相对路径的文件问题

二、解决页面参数问题

以下是我们的处理程序的代码示例:

```csharp

public class MyHttpModule : IHttpModule {

public void Dispose() { }

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);

}

protected void Rewrite(string requestedPath, System.Web.HttpApplication app) {

List qeryString;

string virtualPath;

string inputFile = GetInputFile(app.Context, out virtualPath, out qeryString);

}

public static string GetInputFile(HttpContext context, out string virtualPath, out List qeryString) {

// 获取当前对应的虚拟路径和查询参数...

}

}

```

代码重构

1. 获取文件信息

```csharp

///

/// 获取指定目录、文件名和扩展名是否有效

///

/// 目录

/// 文件名

/// 扩展名

///

private static string GetFileInfo(string inputFile, out string fileName, out string extension)

{

var tempPath = Directory.GetParent(inputFile).FullName; // 获取传进来目录的父目录

fileName = Path.GetFileNameWithoutExtension(inputFile); // 获取子目录名称(不含扩展名)

extension = Path.GetExtension(inputFile); // 获取扩展名

if (string.IsNullOrWhiteSpace(extension)) // 如果扩展名为空或null,认为是aspx文件

{

extension = $"{fileName}.aspx"; // 假设是aspx文件并设置扩展名

}

else

{

extension = $"{fileName}{extension}"; // 否则使用完整的文件名和扩展名组合

}

return tempPath; // 返回父目录路径

}

```

2. MyHttpHandlerFactory类实现

```csharp

using System;

using System.IO;

using System.Reflection;

using System.Web;

using System.Web.UI;

using System.Collections.Generic;

using System.Linq;

namespace MyClass

{

public class MyHttpHandlerFactory : IHttpHandlerFactory

{

region IHttpHandlerFactory 成员实现

public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)

{

List qeryString = new List(); // 查询字符串列表

string virtualPath = ""; // 虚拟路径

string inputFile = MyHttpModule.GetInputFile(context, out virtualPath, out qeryString); // 获取输入文件路径和查询字符串

object[] obj = new object[] { }; // 参数数组

Dictionary parametersDictionary = new Dictionary(); // 参数字典

var receiveMembers = System.Webpilation.BuildManager.GetCompiledType(virtualPath).GetMember("ReceiveParameters"); // 获取ReceiveParameters成员信息

MethodInfo receiveParametersMethod = null; // 用于存储找到的ReceiveParameters方法信息

bool hasParameters = false; // 是否含有查询参数

if (qeryString != null && qeryString.Any()) // 如果存在查询字符串则处理 { foreach (var memberInfo in receiveMembers) //遍历所有ReceiveParameters成员 { if (memberInfo.MemberType == MemberTypes.Method) // 如果是方法 { MethodInfo methodInfo = memberInfo as MethodInfo; if (methodInfo != null) { var parameters = methodInfo.GetParameters(); // 获取方法参数信息 int optionalCount = parameters.Count(p => p.IsOptional); // 计算可选参数数量 if (qeryString.Count == parameters.Length || qeryString.Count == parameters.Length - optionalCount) // 如果查询参数数量与ReceiveParameters方法参数数量相匹配 { receiveParametersMethod = methodInfo; hasParameters = true; obj = new object[parameters.Length]; // 创建参数数组 for (int i = 0; i < parameters.Length; i++) { var parameterName = parameters[i].Name; if (i < qeryString.Count) { obj[i] = qeryString[i]; } parametersDictionary[parameterName] = obj[i].ToString(); } break; } } } } } if (receiveParametersMethod == null) // 如果未找到匹配的ReceiveParameters方法 { virtualPath = context.Request.Path; inputFile = context.Request.PhysicalPath; } var tempPageInstance = System.Web.UI.PageParser.GetCompiledPageInstance(virtualPath, inputFile, context); if (receiveParametersMethod != null) { var page = tempPageInstance as System.Web.UI.Page; page?.Init += new EventHandler(page_Init); // 添加事件处理器 page?.InvokeMethodFromCompiledCode("ReceiveParameters", obj); } return tempPageInstance; }

让我们从`ReleaseHandler`方法开始。

```csharp

///

/// 使工厂能够重用现有的处理程序实例

///

/// 要重用的 System.Web.IHttpHandler 对象

public void ReleaseHandler(IHttpHandler handler)

{

// 具体的释放逻辑

// 此处应添加代码以实际释放或管理处理程序实例

}

```

接下来是`ReceiveParameters`方法的改进:

```csharp

///

/// 根据提供的参数自动运行方法并传递参数值

///

/// 参数名称

public void ReceiveParameters(string name)

{

var temp = Request; // 暂存Request对象,尽管此处可能不需要这个变量

Page page = this.Page; // 获取当前页面实例(假设此方法是在页面类中定义的)

string value; // 参数值,需要实际从Request中获取而不是未定义的变量

try

{

// 从Request中获取参数值

value = page.Request.Form[name]; // 从表单中获取参数值

if (!string.IsNullOrEmpty(value)) // 确保值存在且有意义

{

// 根据参数名称查找对应的控件并执行相应操作

System.Web.UI.Control control = page.FindControl(name); // 根据名称查找控件实例

if (control != null) // 如果找到对应的控件实例,执行后续操作

{

} catch (Exception ex)

{

// 异常处理逻辑(此处应添加适当的异常处理逻辑)

}

上一篇:Extjs4.0 ComboBox如何实现三级联动 下一篇:没有了

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by