23

Specifically, Session variables. I have an .ashx in my ASP.NET MVC project that is pulling some image data to display to a user, and I need to be able to access an object that I've stored in the session. From controllers I can pull the object fine, but in my ashx page, the context.Session is null. Any thoughts? Thanks!

Here is an example of what I'm trying to do... context.Session is always returning null.

 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 } } 
5
  • 1
    duplicate: stackoverflow.com/questions/1240732/… Commented Dec 16, 2009 at 23:36
  • Not really... my problem is that I can't access my session variables stored from my MVC controller. When I try to access the session outside of a controller, it just returns null. Commented Dec 17, 2009 at 5:54
  • isn't your code in a IHttpHandler? if so, it's the same problem and solution. Commented Dec 17, 2009 at 13:27
  • I apologize, you were correct Mauricio. I added IReadOnlySessionState to my class and it can read the session data just fine now. Commented Dec 17, 2009 at 17:14
  • Um, I did accept an answer... the provided response from Alaor didn't work, so I posted what did work for me and accepted that as the answer. And I have accepted answers on every question of mine where there was an answer. Commented Jan 9, 2012 at 20:23

4 Answers 4

25

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It's still saying that the Session is null, despite being able to access several different session variables in a controller.
You have created the session variable? Can you show some code here?
Where do you set the Session["tokens"] variable?
I set the Session["tokens"] variable inside of one of my MVC controllers. I have the problem when I try to read it from anything outside of that controller.... in this case specifically it's an .ashx file.
6

To save anyone the digging, for .net core 2.1+:

Add the following to public void ConfigureServices(...) in your Startup.cs:

services.AddHttpContextAccessor(); 

Use by injecting into your service/etc:

public MyService(IHttpContextAccessor httpContextAccessor) { //... } 

Thanks to: https://adamstorr.azurewebsites.net/blog/are-you-registering-ihttpcontextaccessor-correctly

1 Comment

Can also check from custom components section in learn.microsoft.com/en-us/aspnet/core/fundamentals/…
2

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 

Comments

1

In .Net core the best way to access HTTPContext outside the controller is to use IHttpContextAccessor. Using DI we can access for example User/HttpContext objects like _httpContextAccessor.HttpContext.User and _httpContextAccessor.HttpContext.HttpContext. For detailed answer please refer this link. Thanks!

2 Comments

Question was asked in 2009. IHttpContextAccessor did not exist back then, and is a asp.net core thing.
@mxmissile I understand the question was asked back in 2009 but recently I was facing the same issue, this stack overflow link was one of the topmost suggestion given. Hence I thought it would be helpful here to mention above solution which resolved my issue. I will change my answer little bit which will explicitly mention this solution is specific to .Net core. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.