怎么让aspx页面的Session和WebService的Session公用
就是在应用程序一个aspx页面上定义一个Session,然后让该页面访问WebService,让WebService和页面使用一个Session把刚才定义的东西取出来,我是这么做的
Default.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Net;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Session["Message"] = "Message"; localhost.SessionService ss = new localhost.SessionService(); ss.CookieContainer = new CookieContainer(); Cookie cookie = new Cookie("ASP.NET_SessionId", Session.SessionID); cookie.Domain = "localhost"; cookie.HttpOnly = true; cookie.Path = "/"; ss.CookieContainer.Add(cookie); string message=ss.GetSession(); int i = 0; i++; }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;/// <summary>///SessionService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService]public class SessionService : System.Web.Services.WebService { public SessionService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod(EnableSession=true)] public string GetSession() { if (Session["Message"] != null) return Session["Message"].ToString(); else return "NoSession"; } }