在数据访问层中使用Httpcontext [英] Use Httpcontext in data Access Layer

查看:56
本文介绍了在数据访问层中使用Httpcontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在dataAceess层中使用HttpContext,但无法从HttpContext创建对象

I treid to use HttpContext in dataAceess layer but i cant creat a object from HttpContext

 HttpContext httpContext = HttpContext.Current;

我创建了一个Web应用程序和一个libery项目,我想在这样的libery项目中使用HttpContext:

I creat a web application and a libery project and i want to use HttpContext in libery project like this :

public static Context GetContextPerRequest()
   {

       HttpContext httpContext = HttpContext.Current;
       if (httpContext == null)
       {
           return new Context();
       }
       else
       {
           int contextId = Thread.CurrentContext.ContextID;
           int hashCode = httpContext.GetHashCode();
           string key = string.Concat(hashCode, contextId);

           Context context = httpContext.Items[key] as Context;
           if (context == null)
           {
               context = new Context();
               httpContext.Items[key] = context;
           }

           return context;
       }
   }

我使用.net 4.

推荐答案

我以这种方式解决了我的问题:

I solve my problem in this way :

  1. 添加对system.web的引用
  2. 使用system.web;在我的数据访问层中
  3. 通过方法构造器中的GetContextPerRequest()创建对象

这篇关于在数据访问层中使用Httpcontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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