Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 业务类无法引用HTTPContext_C# - Fatal编程技术网

C# 业务类无法引用HTTPContext

C# 业务类无法引用HTTPContext,c#,C#,我在解决方案中设置了一个业务层作为另一个C#项目,我需要调用HttpContext,但无论我如何尝试,我都无法让它正确引用 我尝试让Visual Studio获取所需的引用,但运气不佳,我还尝试手动放置对System.Web的引用,但这些似乎不起作用 我还注意到,会话也没有找到 下面是我使用的代码片段 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System

我在解决方案中设置了一个业务层作为另一个C#项目,我需要调用HttpContext,但无论我如何尝试,我都无法让它正确引用

我尝试让Visual Studio获取所需的引用,但运气不佳,我还尝试手动放置对System.Web的引用,但这些似乎不起作用

我还注意到,会话也没有找到

下面是我使用的代码片段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BussinessLayer
{
    class UserPermissions
    {
        public void redirectToLogin()
        {

            if (Session["user"] == null)
            {
                HttpContext.
                if (HttpContext.Current.Request.Cookies["userName"] == null)
                {
                    HttpContext.Current.Response.Redirect("/login.aspx");
                }

            }
        }
    }
}

有人知道找不到什么HttpContext或会话吗?

这与定位.NET 4客户端配置文件有关。您可以通过将项目重新定位到完整的.NET Framework 4来解决此问题。

这与定位.NET 4客户端配置文件有关。您可以通过将项目重新定位到完整的.NET Framework 4来解决此问题。

您需要参考GAC中的System.Web.dll(我的VS2010没有在“.NET References”中显示它,因此我必须手动添加它)


另外,如果您要分离业务层,则明智的做法是使其更加独立、松散耦合。

您需要参考GAC中的System.Web.dll(我的VS2010没有在“.NET References”中显示它,因此我必须手动添加它)


另外,如果您要分离业务层,则明智的做法是使其更独立、更松散地耦合。

您确实不需要从业务层引用
HttpContext
。如果您这样做了,那么它就不再是业务层了。它是表示层的一部分,与表示引擎具有硬编码的依赖关系


当表示层调用
HttpContext
时,需要从
HttpContext
中获取的任何值都应提供给业务层对象。

您确实不需要从业务层引用
HttpContext
。如果您这样做了,那么它就不再是业务层了。它是表示层的一部分,与表示引擎具有硬编码的依赖关系


当表示层调用时,应该将
HttpContext
中需要的任何值提供给业务层对象。

HttpContext.Current不适合业务层,您可以使用

公共静态类HttpContextHelper{
私有静态对象lockObj=新对象();
私有静态HttpContextBase mockHttpContext;
/// 
///使用抽象访问HttpContext。
/// 
公共静态HttpContextBase当前{
得到{
锁(lockObj){
if(mockHttpContext==null&&HttpContext.Current!=null){
返回新的HttpContextWrapper(HttpContext.Current);
}
}
返回mockHttpContext;
}
设置{
锁(lockObj){
mockHttpContext=值;
}
}
}
}

HttpContext.Current不适合业务层,您可以使用

公共静态类HttpContextHelper{
私有静态对象lockObj=新对象();
私有静态HttpContextBase mockHttpContext;
/// 
///使用抽象访问HttpContext。
/// 
公共静态HttpContextBase当前{
得到{
锁(lockObj){
if(mockHttpContext==null&&HttpContext.Current!=null){
返回新的HttpContextWrapper(HttpContext.Current);
}
}
返回mockHttpContext;
}
设置{
锁(lockObj){
mockHttpContext=值;
}
}
}
}
当你说“引用”时,你的意思是你得到了一个编译器错误吗?或者HttpContext.Current返回为null?当你说“reference”时,你的意思是你得到了一个编译器错误吗?或者HttpContext.Current返回为null?
    public static class HttpContextHelper {
    private static object lockObj = new object();
    private static HttpContextBase mockHttpContext;

    /// <summary>
    /// Access the HttpContext using the Abstractions.
    /// </summary>
    public static HttpContextBase Current {
        get {
            lock (lockObj) {
                if (mockHttpContext == null && HttpContext.Current != null) {
                    return new HttpContextWrapper(HttpContext.Current);
                }
            }

            return mockHttpContext;
        }

        set {
            lock (lockObj) {
                mockHttpContext = value;
            }
        }
    }
}