1

編集:

質問の簡単なバージョン: .NET httpmodule でサーバー変数を作成し、従来の ASP コードでそれらを読み取りたいと考えています。

これは可能ですか?または間違ったアプローチ?

終了編集:

そこで、私は従来の 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);

        }

    }


}
4

2 に答える 2

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

ClassicASPですべてのサーバー変数を一覧表示するためのコードサンプルが壊れています。使用可能なすべての変数のリストのみが表示され、それらの値は表示されません。

これを試して:

<%
for each x in Request.ServerVariables
  response.write(x & " = " & Request.ServerVariables(x) & "<br />")
next
%>
于 2012-06-18T09:26:14.877 に答える
0

解決 ...

とりあえず -

サーバー変数を変更することはできず、リクエスト時に従来の ASP コードによってそれらを「取得」することはできません。ただし、このコードをヘッダーに追加できたので、ヘッダーを使用します。これがひどい方法である場合は、コメントでお知らせください。フランクを助けてくれてありがとう!

ここにコードがあります:

クラシック 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>

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()
        {
        }

        // 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();
        }


    }


}

関連する web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"> 
        <add name="PlatoModule" type="PlatoModules.PlatoModule" /> 
        </modules>
...
    </system.webserver>
于 2012-06-19T14:02:09.873 に答える