views:

120

answers:

3

I have 2 projects in my solution.

  1. MVC Web application
  2. Class library

    • The MVC Web application references the class library.
    • The class library contains a class that extends the default ASP.Net Controller.

I'm putting a variable in session in the application's Global.asax.

protected void Session_Start(object sender, EventArgs args)
{
   HttpContext.Current.Session["DomainName"] = Request.Url.Host;
}

In the class library I'm trying to get the value from the HttpContext.Session, but HttpContext.Session keeps coming up null.

public class MyController : System.Web.Mvc.Controller
{
    public MyController () : base()
    {
        //HttpContext.Session is always null at this point
        ViewData["DomainName"] = HttpContext.Session["DomainName"];
    } 
}

HttpContext.Current.Session doesn't seem to be an option in controllers. Any ideas?

+1  A: 

If you're just trying to add ViewData from the session, try doing it in the OnActionExecuting method. This is where I typically add ViewData I want for every View.

Bit Destroyer
Yep, thanks for your feedback man!
Ozzie Perez
A: 

You just use Session by itself (it's a property of Controller), but that just maps to Controller.HttpContext.Session (in other words, what you're already using), so it won't solve your problem, which must be elsewhere.

I'm not sure why you're putting this in the Session, though, as you can read Request.Url.Host directly during the Action.

Craig Stuntz
Thanks Craig, yea, I realized I can simply reference the Session directly in this case. I was used to referencing the context first while in a separate project when doing WebForms. :) Yeah, I know I can read Request.Url.Host directly, but it was more of a principal kind of a question of how to do it period. Thanks again for your feedback.
Ozzie Perez
+2  A: 

Two issues -- the HttpContext property in the Controller class is the current session. Unfortunately, it's not available in the constructor of the controller. Obviously because it's not passed in the constructor, it has to be set via the property afterwards. You might consider adding a property to hold the domain name and referencing the session from it -- that way it would be available for use when needed.

 protected string DomainName
 {
      get { return this.HttpContext.Session["DomainName"] as string; }
 }

The set it in ViewData in your actions or in OnActionExecuting/OnActionExecuted.

 protected override void OnActionExecuted( ActionExecutedContext context )
 {
      ViewData["DomainName"] = this.HttpContext.Session["DomainName"];
      // or ViewData["DomainName"] = this.DomainName;  // if you used the property
 }
tvanfosson
Thanks for the detailed breakdown! We're rolling now... :)
Ozzie Perez
Public override void OnActionExecuted( ActionExecutedContext context ) gives an error "cannot change access modifiers when overriding 'protected' inherited member", but using public void OnActionExecuting(ActionExecutedContext context) worked fine. Thanks fellas.
Ozzie Perez
PS. I used it with your property suggestion. :)
Ozzie Perez
@Ozzie -- fixed the visibility.
tvanfosson
@tvanfosson, thanks
Ozzie Perez