编辑:
问题的简单版本:我想在 .NET httpmodule 中创建服务器变量并在我的经典 ASP 代码中读取它们。
这可能吗?还是错误的方法?
结束编辑:
所以我接手了一个经典的asp应用程序,作者写了一个ISASPI Filter dll,他用它来为他的经典asp应用程序设置服务器变量。我在 IIS 论坛上读到自定义 ISAPI 过滤器是个坏主意,如果我要继续使用它,我应该使用 http 模块。
所以我把这个方法从互联网上拉下来,让我在我的 httpmodule 中设置服务器变量,这似乎适用于将项目添加到服务器变量集合中......但是我无法从我的经典 asp 代码中读取它。
我有错误的方法吗?
BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
var context = app.Context;
MethodInfo addStatic = null;
MethodInfo makeReadOnly = null;
MethodInfo makeReadWrite = null;
Type type = app.Request.ServerVariables.GetType();
MethodInfo[] methods = type.GetMethods(temp);
foreach(var method in methods)
{
switch( method.Name)
{
case "MakeReadWrite":
makeReadWrite = method;
break;
case "MakeReadOnly":
makeReadOnly = method;
break;
case "AddStatic":
addStatic = method;
break;
}
}
makeReadWrite.Invoke(context.Request.ServerVariables, null);
string[] values = { "DaveVar", "hehehe" };
addStatic.Invoke(context.Request.ServerVariables, values);
makeReadOnly.Invoke(context.Request.ServerVariables, null);
这似乎正确设置了它们;但是,当我尝试从我的经典 asp 页面中读取它们时,它们并没有出现......
经典ASP:
<html>
<%
for each x in Request.ServerVariables
response.write("<h2>"& x & "</h2>")
next
%>
<h2>hello!</h2>
</html>
它们确实出现的 ASP.NET ASPX 页面:
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
foreach (var x in Request.ServerVariables)
{
%>
<div>
<%= x.ToString() %>
</div>
<%
}
%>
</div>
</form>
</body>
</html>
完整的http模块:
namespace PlatoModules
{
public class PlatoModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (s, e) => BeginRequest(s, e);
context.EndRequest += (s, e) => EndRequest(s, e);
}
public String ModuleName
{
get { return "test"; }
}
public void Dispose()
{
}
private void BeginRequest(Object source,
EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
System.Diagnostics.Debugger.Launch();
// Create HttpApplication and HttpContext objects to access
// request and response properties.
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
/* if (fileExtension.Equals(".aspx"))
{
context.Response.Write("<h1><font color=red>" +
"HelloWorldModule: Beginning of Request" +
"</font></h1><hr>");
}*/
BlackMagicSetServerVariables(application);
if (fileExtension.Equals(".asp"))
{
string content = @"<h1><font color=red>" +
"BeginReq" +
@"</font></h1><br>";
context.Response.Write(content);
context.Response.Flush();
}
}
catch (Exception ex)
{
context.Response.Write(@"<h1><font color=red>" +
"error" + ex.Message +
@"</font></h1><br>");
}
}
private void EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write(@"<br><h1><font color=red>" +
@"Enter endreq </font></h1>");
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".asp"))
{
context.Response.Write(@"<br><h1><font color=red>" +
@"EndReq </font></h1>");
}
context.Response.Flush();
}
void BlackMagicSetServerVariables(HttpApplication app)
{
BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
var context = app.Context;
MethodInfo addStatic = null;
MethodInfo makeReadOnly = null;
MethodInfo makeReadWrite = null;
Type type = app.Request.ServerVariables.GetType();
MethodInfo[] methods = type.GetMethods(temp);
foreach(var method in methods)
{
switch( method.Name)
{
case "MakeReadWrite":
makeReadWrite = method;
break;
case "MakeReadOnly":
makeReadOnly = method;
break;
case "AddStatic":
addStatic = method;
break;
}
}
makeReadWrite.Invoke(context.Request.ServerVariables, null);
string[] values = { "DaveVar", "hehehe" };
addStatic.Invoke(context.Request.ServerVariables, values);
makeReadOnly.Invoke(context.Request.ServerVariables, null);
}
}
}