Web客户端失去了我的会话 [英] WebClient Lost my Session

查看:150
本文介绍了Web客户端失去了我的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,首先看一下我的Web服务方法:

I have a problem first of all look at my web service method :

[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服务方法读取并从它的本地主机它的作品,我用它来一些Ajax功能添加到我的网站,它工作正常缓冲页面,但我的问题是Client.DownloadString(..)失去了所有的会议因为我的课程均为空这是与此相关的页面,了解更多的说明。在这我想从我的web服务加载页面的页面加载我设置会话:

As you can see my web service method read and buffer page from it's localhost it works and I use it to add some ajax functionality to my web site it works fine but my problem is Client.DownloadString(..) lost all session because my sessions are all null which are related to this page for more description. at page-load in the page which I want to load from my web service I set session :

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

但是当我点击页面中的一个按钮,本次会议为空我的意思是不能转移的会话。

but when I click a button in the page this session is null I mean it can't transfer session.

我该如何解决这个问题?
我的网络服务是由一些jQuery code像以下处理:

How I can solve this problem ? my web service is handled by some jquery code like following :

$.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客户端使用相同的实际应用?

Web Client causes to lose session and .js files can not run how I can use other thing instead of web client with the same practical application ?

推荐答案

您需要启用会话(使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx\"相对=nofollow> WebMethodAttribute.EnableSession ),更改此:

You need to enable session (using WebMethodAttribute.EnableSession), change this:

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

要:

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


修改:对不起,没看到你正在使用Web客户端。
这将无法工作,因为Web客户端运行服务器端的,因此从用户使用不同的会话。


Edit: Sorry, didn't see you are using WebClient. This won't work because WebClient runs server side and therefore uses different session from the users.

这篇关于Web客户端失去了我的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆