asp.net - 读取已在 ASP.NET httpmodule 中设置的经典 ASP 中的服务器

编辑:

问题的简单版本: 我想在 .NET http 模块中创建服务器变量并在我的经典 ASP 代码中读取它们。

这可能吗?还是错误的做法?

结束:

所以我接管了一个经典的 asp 应用程序,作者编写了一个 ISASPI Filter dll,他用它来为他的经典 asp 应用程序设置服务器变量。我在 IIS 论坛上读到,自定义 ISAPI 过滤器不是一个好主意,如果我要推进它,我应该使用 http 模块。

所以我从互联网上撤下了这个方法,它让我可以在我的 http 模块中设置服务器变量,这似乎可以将项目添加到服务器变量集合中......但是我无法从我的经典 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);

        }

    }


}

最佳答案

<%
for each x in Request.ServerVariables
  response.write(x & "<br />")
next
%>

用于在经典 ASP 中列出所有服务器变量的代码示例已损坏。 它只为您提供所有可用变量的列表,而不是它们的值。

试试这个:

<%
for each x in Request.ServerVariables
  response.write(x & " = " & Request.ServerVariables(x) & "<br />")
next
%>

关于asp.net - 读取已在 ASP.NET httpmodule 中设置的经典 ASP 中的服务器变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11032529/

相关文章:

regex - 如何匹配同一字符串中的多组正则表达式模式?

regex - 如何使用正则表达式查找字符串中出现的相同后续字符?

php - 你如何使用 PHP 检查文件是否在某个目录中?

.net - String.GetHashCode() 的复杂性

r - 如何处理每次有多个点的时间序列(在 R 中)?

jquery-ui - jQuery UI 日期选择器。显示一种格式,保存其他格式

svn - Hudson - SVN 错误 : org. tmatesoft.svn.core.SV

rest - 使用 Twitter 的 REST API 从收藏夹获取 faved_at 日期?

.net - 如何为当前用户注册一个类型库?

c# - "Unable to find an entry point named"在 c# 中使用