如何访问HttpContext的ASP.NET MVC中的控制器之外吗? [英] How to access HttpContext outside of controllers in ASP.NET MVC?

查看:124
本文介绍了如何访问HttpContext的ASP.NET MVC中的控制器之外吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体而言,会话变量。我有一个ashx的在是拉一些图像数据显示给用户我的ASP.NET MVC项目,我需要能够访问,我已经存储在会话中的对象。从控制器我可以拉细的对象,但在我的ashx页面上,context.Session为空。有什么想法吗?谢谢!

下面是什么,我试图做... context.Session总是返回空的例子。

 专用字节[] getIconData(字符串图标)
    {
        //返回图标文件
        HttpContext的背景下= HttpContext.Current;        字节[]缓冲= NULL;        //获取图标数据
        如果(context.Session [令牌]!= NULL)
        {
            //做一些东西让图标数据
        }
    }


解决方案

好了,我最后不得不做的....我的ashx文件,我在IReadOnlySessionState接口添加它只会访问会话状态精细。因此,它看起来像这样...

 公共类调用getIcon:IHttpHandler的,IReadOnlySessionState

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
        }
    }

解决方案

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

这篇关于如何访问HttpContext的ASP.NET MVC中的控制器之外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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