編輯:讀取服務器變量已在ASP.NET的HttpModule設置
問題的簡單版本: 我要創建在.NET的HttpModule服務器變量,並在我的經典ASP代碼閱讀。
這可能嗎?或者錯誤的方法?
EndEdit中:
所以我已經接管了經典的ASP應用程序,筆者寫了一個ISASPI器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);
}
}
}
您正在使用集成管道? – AnthonyWJones
是的,在應用程序池中,我已將管理管道模式設置爲Integrated –