Click here to Skip to main content
12,401,060 members (61,979 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET MVC
I'm new to Mvc.
Sorry to my english. ^^
I have some question about 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());     

I want to use _db variable to my Testcontroller.

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

[Edited]Code is blocked in "pre" tags[/Edited]
Posted 15-May-11 15:02pm
Updated 15-May-11 16:11pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Jun 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100