Can we access Session variable in Asp.Net from SIlverlight Application
Can we access Session variable in Asp.Net from 开发者_Python百科SIlverlight Application
There are two approaches to getting the value of Session variable.
The first is to include the value in the generated HTML of the ASPX page hosting the Silverlight application. Add the value in the InitParams
<Param>
tag of the silverlight object.
<param name="initParams" value="myValue=<%=Server.HTMLEncode(Session["myValue"].ToString())%>" />
Now in Silverlight code you can access this value:-
string myValue = Application.Current.Host.InitParams["myValue"];
The above is the most likely scenario. If though you need to also mutate the session value during execution of the Silverlight application and/or read a potentially changes value for the variable then things are tricker.
At this point many would probably advise the creation of some WCF to assist with this. Alternatively I might be inclined to create a .ashx file that simply accepted and/or returned some Xml that can assist with such very simple server side work.
Silverlight application is running on the client browser, so you can't access Session object from it.
Session is an object created by asp.net for every session it creates - it uses cookie or url to recognize the user session.
AnthonyWJones's answer above works well. I just had to make one small change in my situation. The initParams variables are actually key-value pairs and should be specified as such to access it properly from your Silverlight code:
<param name="initParams" value="myKey1=myValue1,myKey2=myValue2" />
Maybe this changed since the previous answer in 2011. This is as per .Net 4.5 and Silverlight 5.
精彩评论