2012-07-15 89 views
-1

我想要访问在我的项目的Global.asax部分中设置的全局变量。在我的DAL中包含名称空间并访问变量visual studio抱怨时。下面是代码如何访问asp.net中的全局变量mvc

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RegisterGlobalFilters(GlobalFilters.Filters); 
    RegisterRoutes(RouteTable.Routes); 
    Application["NHSessionFactory"] = CreateSessionFactory(); 
} 

这里是我的DAL逻辑

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

namespace Data.DAL 
{ 
    public class DB 
    { 
     private ISessionFactory session; 
     public DB() 
     { 
      ISessionFactory session = (ISessionFactory) Application["NHSessionFactory"]; //problems here 
     } 

     public ISessionFactory GetSession() 
     { 
      return session; 
     } 
    } 
} 

错误的屏幕截图: enter image description here

我将如何访问这是在Global.asax中设置变量?

+0

它是如何投诉?应用程序的结构是什么(DAL代码相对于应用程序在哪里) – Murph 2012-07-15 20:39:35

+0

你得到了什么错误信息? CreateSessionFactory在哪里定义?不可能返回null? – nikeaa 2012-07-15 20:41:13

回答

0

是的,你可以使用应用程序变量的方式相同asp.net

Example: 

System.Web.HttpContext.Current.Application.Lock(); 
System.Web.HttpContext.Current.Application["NHSessionFactory"] = "Something"; 
System.Web.HttpContext.Current.Application.UnLock();