泛型處理常式 ashx 存取 Session

泛型處理常式 ashx 存取 Session

要在ashx寫入Session 必須做兩件事

1. using System.Web.SessionState;

2. 繼承IRequiresSessionState

 

泛型處理常式ashx  檔案


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace SystemLead.Project
{
    /// <summary>
    ///Query1 的摘要描述
    /// </summary>
    public class Query1 : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //寫入Session
            context.Session["SPRISEQ"] = context.Request.Form["SPRISEQ"];
            context.Session["TRSEQ"] = context.Request.Form["TRSEQ"];

            //讀取Session
            context.Response.Write(context.Session["SPRISEQ"].ToString() + context.Session["TRSEQ"].ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

使用JQuery AJAX呼叫  ashx 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../Scripts/jquery-1.10.0.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var SPRISEQ = "SPRISEQ";
            var TRSEQ = " TRSEQ";

            $.ajax({
                type: 'post',   //傳輸類型                
                url: "Query1.ashx",   //泛型路徑                      
                data: 'SPRISEQ=' + SPRISEQ + '&TRSEQ=' + TRSEQ, //傳入參數
                success: function (message) {                   //成功事件:回傳message參數
                    alert(message);
                },
                error: function () { alert('ajax failed'); }    //失敗事件
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>/div>
    </form>
</body>
</html>
Technorati 的標籤: ,