Business Class无法引用HTTPContext

时间:2011-06-11 10:50:39

标签: c#

我在我的解决方案中将业务层设置为另一个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或Session?

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

你需要引用可以在GAC中找到的System.Web.dll(我的VS2010没有在'.NET References'中显示它,所以我必须手动添加它)。

另外,如果你要分离业务层,那么它可能更明智,更独立,更松散耦合。

答案 3 :(得分:1)

你真的,老实说,需要从您的业务层引用HttpContext。如果这样做,那么它就不再是业务层。它是表示层的一部分,具有对表示引擎的硬编码依赖性。

当表示层调用时,应将HttpContext所需的值提供给业务层对象。