WebClient Lost my Session
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;
}
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.
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,
succe开发者_StackOverflow中文版ss: LoadAjaxSucceeded,
async: true,
cache: false,
error: AjaxFailed
});
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 ?
You need to enable session (using WebMethodAttribute.EnableSession
), change this:
[WebMethod(), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
To:
[WebMethod(EnableSession = true), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
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.
精彩评论