在ASP.NET MVC3控制器的构造函数中使用会话 [英] Using session in asp.net mvc3 Controller's Contructor

查看:67
本文介绍了在ASP.NET MVC3控制器的构造函数中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mvc的新手.
对不起,我的英语. ^^
我对控制器中的asp.net MVC会话有一些疑问.
我想做的场景操作如下:.
1.首先,我的开发环境是entityframework和mvc3.
2.当某人登录时,每个人都有不同的数据库.因此,每个都有连接不同的数据库.
3.因此,每个人都有自己的会话值,该值是数据库连接字符串.到目前为止一切都很好.
4.我有简单的数据库存储库,并且在每个存储库的构造函数中都可以更改数据库连接.
5.在调用Repository类的控制器上,我需要会话值.但是据我所知,Controller的构造无法保留会话值.对吧?
6.我要您的好建议.提前谢谢.
代码示例如下:

I''m new to Mvc.
Sorry to my english. ^^
I have some question about asp.net MVC session in the controller.
The Scenario things that I want to do is like follows..
1. First of all, My development circumstance is entityframework and mvc3.
2. When Someone logged in each one has different database. So, Each has connect different database.
3. So, Each person has his own session value which is database connection string. So far so good.
4. I have simple database Repository and at the each repository''s constructor can change database connection.
5. At controller which calls Repository class, I need session value. But As I know Controller''s construction can''t keep session value. right?
6. I want your good advice. Thanks in advance.
Code samples are below:

public class MasterRepository 
{
    DBEntities _db;

    public MasterRepository(string con)
    {
        _db = new DBEntities(con);
    }

}
public class TestController : Controller
{

    private string con;

    MasterRepository _db;

    public TestController()
    {
    _db = new MasterRepository(Session["conn"].ToString()); //  Session is null I want to solve this Part...
    }

    public ActionResult Index()
    {

        string con = Session["conn"].ToString(); // Session is assigned.

        return View();
    }

}




我只想将会话值用于控制器,所以我想将数据库连接保留在控制器的构造器中.

该点未设置为控制器的构造器.




I just want to use my session value to my controller so I want to keep my database connection in a controller''s contructor.

The point is not set to an controller''s contructor.

MasterRepository _db;    
public TestController()    
{    
   //  Session is null I want to solve this Part...
   _db = new MasterRepository(Session["conn"].ToString());     
}



我想对我的Testcontroller使用_db变量.

如果无法将会话值设置为控制器的构造函数,还有另一种方法吗?

[已编辑]代码在前置"标记中被阻止[/已编辑]



I want to use _db variable to my Testcontroller.

If impossible set session value to controller''s constructor, Is there another way?

Code is blocked in "pre" tags[/Edited]

推荐答案

它为null,因为在ASP.NET下运行时在MVC框架中,未按预期构造控制器类时设置HttpContext(因此也未设置HttpContext.Session),但稍后由ControllerBuilder类设置(注入").如果您想更好地了解生命周期,可以下拉ASP.NET MVC框架(源可用),或参考:


HTH
It is null because when running under the ASP.NET MVC framework, the HttpContext (and therefore HttpContext.Session) is not set when the controller class is contructed as you might expect, but it set ("injected") later by the ControllerBuilder class. If you want a better understanding of the lifecycle you can either pull down the ASP.NET MVC framework (the source is available), or refer to: this page

If you need to access the Session then one way would be to override the "OnActionExecuting" method and access it there, as it will be available by that time.



HTH


这篇关于在ASP.NET MVC3控制器的构造函数中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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