【问题标题】:WebClient Lost my SessionWebClient 丢失了我的会话
【发布时间】:2011-05-31 00:57:40
【问题描述】:

我有问题先看看我的web服务方法:

[WebMethod(EnableSession = true), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetPageContent(string VirtualPath)
{
    WebClient client = new WebClient();
    string content=string.Empty;
    client.Encoding = System.Text.Encoding.UTF8;
    try
    {
        if (VirtualPath.IndexOf("______") > 0)
            content = client.DownloadString(HttpContext.Current.Request.UrlReferrer.AbsoluteUri.Replace("Main.aspx", VirtualPath.Replace("__", ".")));
        else content = client.DownloadString(HttpContext.Current.Request.UrlReferrer.AbsoluteUri.Replace("Main.aspx", VirtualPath));
    }
    catch { content = "Not Found"; }
    return content;
}

您可以看到我的 Web 服务方法从它的 localhost 读取和缓冲页面它可以工作,我使用它向我的网站添加一些 ajax 功能它工作正常,但我的问题是 Client.DownloadString(..) 丢失了所有会话因为我的会话都是空的,与此页面相关以获得更多描述。在我想从我的网络服务加载的页面的页面加载中,我设置了会话:

HttpContext.Current.Session[E_ShopData.Constants.SessionKey_ItemList] = result;

但是当我点击页面中的一个按钮时,这个会话是空的,我的意思是它不能转移会话。

我该如何解决这个问题? 我的 Web 服务由一些 jquery 代码处理,如下所示:

$.ajax({ type: "Post",
    url: "Services/NewE_ShopServices.asmx" + "/" + "GetPageContent",
    data: "{" + "VirtualPath" + ":" + mp + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: hideBlocker,
    success: LoadAjaxSucceeded,
    async: true,
    cache: false,
    error: AjaxFailed
});

Web 客户端导致会话丢失和 .js 文件无法运行我如何使用其他东西代替具有相同实际应用程序的 Web 客户端?

【问题讨论】:

    标签: c# asp.net jquery web-services session


    【解决方案1】:

    您需要启用会话(使用WebMethodAttribute.EnableSession),更改此:

    [WebMethod(), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    

    收件人:

    [WebMethod(EnableSession = true), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    

    编辑:抱歉,没有看到您正在使用 WebClient。 这不起作用,因为 WebClient 运行服务器端,因此使用与用户不同的会话。

    【讨论】:

    • 我认为它与网络服务无关,因为我已经这样做了但它不起作用
    • 当我在通过 WebClient.DownloadString 下载的页面的 pageLoad 事件中访问会话时,我看不到会话中的任何值。
    猜你喜欢
    • 2014-08-23
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2013-08-10
    • 2012-05-09
    相关资源
    最近更新 更多