asp.net與asp的session共用 及 asp的請求攔截

來源:互聯網
上載者:User

asp.net 與 asp 的session是無法直接共用的(底層的處理dll也不一樣),要想互連session,只能用變通的辦法:

一、asp.net -> asp 的session傳遞

a) 建一個類似SessionHanler.asp的頁面,代碼如下:

<!-- #include virtual="inc/Func.asp" --><%    Dim returnUrl    Session("user") = Request("user")    Set returnUrl = Request("returnUrl")    if returnUrl="" then        returnUrl = "home.asp"    end if            '記日誌    AppendFile "/log/log.txt" , Now() & ",user:"&Session("user") &",returnUrl:" & returnUrl    Response.Redirect(returnUrl)%>

 大概功能,就是接收參數,然後按需要產生Session,最後重新導向到真正的功能頁面,這樣其它頁面訪問Session時就有值了

b) 建一個asp.net頁面,把需要傳遞的值,以參數形式提交到 SessionHanler.asp  (POST或GET方式都行),參考代碼:

using System;namespace ASP_ASPX_TEST{    public partial class index : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (IsPostBack)            {                string returnUrl = "home.asp";                string sessoinHandlerUrl = "SessionHandler.asp";                string postData = "user=" + this.txtUser.Text + "&returnUrl=" + returnUrl;                Response.Redirect(sessoinHandlerUrl + "?" + postData);            }        }    }}

 二、asp -> asp.net 的session傳遞

反過來做即可,原理完全相同。

三、攔截asp請求

對於現有的asp項目,在不修改其asp原始碼的前提下,如果需要對http請求加以攔截(例如:把攔截到的請求參數做些處理,再轉寄到其它子系統。同時不影響原來asp項目的正常運行),有二種做法:

a) 自行開發ISAPI 篩選器 ,然後在IIS裡,把自己開發的dll加入ISAPI 篩選器

這個方法比較繁瑣,技術難度也相對較高,在今天.net的時代,不推薦大家使用,有興趣的可以參考一個開源項目: http://filterdotnet.codeplex.com/

以及 一些ISAPI的開發文章,比如

ISAPI開發介紹 http://blog.csdn.net/mycoolx/article/details/6913048

ISAPI tutorial for Delphi developers http://delphi.about.com/library/bluc/text/uc060901c.htm

delphi IIS ISAPI http://www.cnblogs.com/cisky/archive/2011/01/05/1926249.html

delphi IIS ISAPI  http://siyebocai.blog.163.com/blog/static/103316426200810297512408/

用Delphi編寫 IIS ISAPI 程式 http://download.csdn.net/detail/wwwvvingnet/2229146

在Delphi中用IIS或PWS調試ISAPI程式 http://bbs.csdn.net/topics/7979

b) 利用asp.net的HttpModule (環境:IIS7 /Asp.Net 4.0上 測試通過)

前提:Asp項目所用的應用程式集區必須採用"整合"模式

先建一個HttpModule

using System;using System.IO;using System.Web;namespace ASP_ASPX_TEST{    public class MyHttpModule : IHttpModule    {        string logFileName;        public MyHttpModule()        {            logFileName = HttpContext.Current.Server.MapPath("~/log/request.log");        }        public void Dispose()        {        }        public void Init(HttpApplication context)        {            context.BeginRequest += BeginRequestHandle;            context.EndRequest += EndRequestHandle;        }        private void BeginRequestHandle(Object source, EventArgs e)        {            HttpApplication application = (HttpApplication)source;            HttpContext context = application.Context;            HttpRequest request = context.Request;            File.AppendAllText(logFileName,Environment.NewLine + request.Url + Environment.NewLine + "BeginRequest => " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);            foreach (var item in request.QueryString.AllKeys)            {                File.AppendAllText(logFileName, string.Format("  QueryString => {0}:{1}", item, request.QueryString[item]) + Environment.NewLine);            }            foreach (var item in request.Form.AllKeys)            {                File.AppendAllText(logFileName, string.Format("  Form => {0}:{1}", item, request.Form[item]) + Environment.NewLine);            }            context.Response.Write("BeginRequestHandle<br/>");        }        private void EndRequestHandle(Object source, EventArgs e)        {            HttpApplication application = (HttpApplication)source;            HttpContext context = application.Context;            HttpRequest request = context.Request;            File.AppendAllText(logFileName, Environment.NewLine + request.Url + Environment.NewLine + "EndRequest => " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);            foreach (var item in request.QueryString.AllKeys)            {                File.AppendAllText(logFileName, string.Format("  QueryString => {0}:{1}", item, request.QueryString[item]) + Environment.NewLine);            }            foreach (var item in request.Form.AllKeys)            {                File.AppendAllText(logFileName, string.Format("  Form => {0}:{1}", item, request.Form[item]) + Environment.NewLine);            }            context.Response.Write("EndRequestHandle<br/>");        }    }}

 這裡只是示範代碼,我把所有請求的QueryString和Form參數都記錄了下來。

web.config中修改配置

<?xml version="1.0" encoding="UTF-8"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web>  <system.webServer>    <modules runAllManagedModulesForAllRequests="true">      <add name="MyHttpModule" type="ASP_ASPX_TEST.MyHttpModule,ASP_ASPX_TEST" />    </modules>  </system.webServer></configuration>

 註:IIS7及以上版本,自訂的HttpModule,必須加到 system.webServer節點下,否則只能攔截asp.net的請求,對asp無效

最後贈送一個asp調試的小技巧(自從asp.net出來以後,很多人估計象我一樣,已經很久不碰asp,這些小技巧差不多忘記光了,貼在這裡備份一下)

IE瀏覽器裡先去掉 友好錯誤的勾選

IIS設定裡,允許發送詳細錯誤到用戶端

這樣,asp代碼出錯時,就會顯示詳細資料了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.