URL重写及干掉ASP.NET试图状态的实现方法
一、解决无法使用相对路径的文件问题
二、解决页面参数问题
以下是我们的处理程序的代码示例:
```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
string virtualPath;
string inputFile = GetInputFile(app.Context, out virtualPath, out qeryString);
}
public static string GetInputFile(HttpContext context, out string virtualPath, out List
// 获取当前对应的虚拟路径和查询参数...
}
}
```
代码重构
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
string virtualPath = ""; // 虚拟路径
string inputFile = MyHttpModule.GetInputFile(context, out virtualPath, out qeryString); // 获取输入文件路径和查询字符串
object[] obj = new object[] { }; // 参数数组
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)
{
// 异常处理逻辑(此处应添加适当的异常处理逻辑)
}
网络安全培训
- URL重写及干掉ASP.NET试图状态的实现方法
- Extjs4.0 ComboBox如何实现三级联动
- js改变透明度实现轮播图的算法
- JavaScript实现简洁的俄罗斯方块完整实例
- asp的RegExp对象正则表达式功能用法[比较全]
- 如何检测JavaScript的各种类型
- SQLserver中字符串查找功能patindex和charindex的区别
- php-7.3.6 编译安装过程
- bootstrap输入框组使用方法
- 超详细的javascript数组方法汇总
- AngularJS 使用$sce控制代码安全检查
- 微信小程序之拖拽排序(代码分享)
- 详解webpack2+node+react+babel实现热加载(hmr)
- JavaScript数据结构学习之数组、栈与队列
- swoole和websocket简单聊天室开发
- 利用vue.js把静态json绑定bootstrap的table方法