Click here to Skip to main content
Rate this: bad
good
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 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());     
}
 
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 16:02pm
WKim356
Edited 15-May-11 17:11pm
v3

1 solution

Rate this: bad
good
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.
 

 
HTH
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Jun 2011
Copyright © CodeProject, 1999-2014
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