2012-06-14 23 views
0

編輯:讀取服務器變量已在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); 

     } 

    } 


} 
+0

您正在使用集成管道? – AnthonyWJones

+0

是的,在應用程序池中,我已將管理管道模式設置爲Integrated –

回答

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> 

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


    } 


} 

培訓相關網頁。config

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="PlatoModule" type="PlatoModules.PlatoModule" /> 
     </modules> 
... 
    </system.webserver> 
0
<% 
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 
%> 
+1

http://www.w3schools.com/asp/coll_servervariables.asp不正確 – Frank

+0

我可以通過它列出變量及其值;然而,我無法弄清楚如何在http模塊中添加我自己的服務器變量,然後從經典的asp代碼中讀取。 –

+1

我懷疑你可以擴展現有的服務器變量可用於傳統的ASP。總體目標是什麼?如果它在asp/aspx頁面之間共享信息,那麼最好在全局配置文件或db中完成。這樣你的應用可以擴展。我不明白你的http模塊在做什麼。 – Frank

相關問題