А именно, Переменные сеанса. У меня есть .ashx в моем проекте MVC ASP.NET, который вытягивает некоторые данные изображения для отображения пользователю, и я должен смочь получить доступ к объекту, который я хранил на сессии. От контроллеров я могу вытянуть объектный штраф, но на моей ashx странице, контексте. Сеанс является пустым. Какие-либо мысли?Спасибо!
Вот пример того, что я пытаюсь сделать... контекст. Сессия всегда возвращает пустой указатель.
private byte[] getIconData(string icon)
{
//returns the icon file
HttpContext context = HttpContext.Current;
byte[] buffer = null;
//get icon data
if ( context.Session["tokens"] != null)
{
//do some stuff to get icon data
}
}
Ok, so what I ended up having to do.... in my ashx file, I added in the IReadOnlySessionState interface and it will access the session state just fine. So it looks something like this...
public class getIcon : IHttpHandler, IReadOnlySessionState
You must import the System.Web assembly in your code and then you can do something like this:
HttpContext context = HttpContext.Current;
return (User)context.Session["User"];
Editing:
Dude, I did some tests here and it works for me, try something like this:
Create a helper class to encapsulate you getting session variables stuff, it must import the System.Web assembly:
public class TextService
{
public static string Message {
get
{
HttpContext context = HttpContext.Current;
return (string)context.Session["msg"];
}
set
{
HttpContext context = HttpContext.Current;
context.Session["msg"] = value;
}
}
}
Then in your controller you should do something like:
TextService.Message = "testing the whole thing";
return Redirect("/home/testing.myapp");
And in your other classes you can call the helper class:
return TextService.Message;
Give it a try.