编辑:
问题的简单版本: 我想在.NET httpmodule中创建服务器变量,并在我的经典ASP代码中读取它们。
这可能吗?还是错误的做法?
EndEdit中:
所以我接管了一个经典的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页面中读取它们时,它们似乎没有出现......
CLASSIC 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);
}
}
}
答案 0 :(得分:0)
<%
for each x in Request.ServerVariables
response.write(x & "<br />")
next
%>
用于列出Classic ASP中所有服务器变量的代码示例已损坏。 它只给出了所有可用变量的列表,但不是它们的值。
试试这个:
<%
for each x in Request.ServerVariables
response.write(x & " = " & Request.ServerVariables(x) & "<br />")
next
%>
答案 1 :(得分:0)
解决方案......
暂时 -
我无法修改服务器变量并让它们被请求上的经典asp代码“拾取”;但是我可以在标题中添加此代码,因此我将使用标题。如果这是一个糟糕的做事方式,请在评论中告诉我!谢谢你帮我弗兰克!
所以这是代码:
CLASSIC ASP:
<%
for each x in Request.ServerVariables
Response.Write("<h2>"& x & ":" & Request.ServerVariables(x) & "</h2>")
next
Response.Write("<h2> DAVE: " & Request.ServerVariables("HTTP_DAVE") & "</h2>")
Response.Flush()
%>
<h2>hello!</h2>
</html>
HTTPMODULE:
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()
{
}
// Your BeginRequest event handler.
private void BeginRequest(Object source, EventArgs e)
{
Debugger.Launch();
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
context.Response.Write(
"<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");
context.Request.Headers.Add("DAVE", "DAVOLIO");
context.Response.Flush();
}
catch (Exception ex)
{
context.Response.Write(
ex.Message);
}
}
private void EndRequest(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
var content = @"<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>";
context.Response.Write(content);
context.Response.Flush();
}
}
}
Relevent web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="PlatoModule" type="PlatoModules.PlatoModule" />
</modules>
...
</system.webserver>