Question

New to SPA MVC4, trying to pass a session variable to LinqToEntitiesDataController from the account controller to identify the user by:

                      using (DALEntities db = new DALEntities())
                        {
                            string PHN = (from p in db.Patients
                                          where p.UserName == model.UserName
                                          select p.PHN).First();
                            Session["P"] = PHN;
                        }

In the LinqToEntitiesDataController I am trying to use the variable as follows:

public partial class DALController : LinqToEntitiesDataController<MyVDC.Models.DALEntities>
    {
        public IQueryable<MyVDC.Models.TestModel> GetTestModel()
        {
            **string phn = (string)Session["P"];**
            return ObjectContext.TestModels.Where(b => b.PHN == phn).OrderBy(b => b.ID);

        }
}

I get this Error:

The name 'Session' does not exist in the current context

Is this the only way, or is there a better method to use the session variables with this controller.

I also tried to use in the account controller:

 HttpContext.Current.Session["P"] = PHN;

But I get this error:

'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found 

Thanks in advance.

Was it helpful?

Solution

public class SessionHttpControllerRouteHandler : HttpControllerRouteHandler
{
    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        return new SessionHttpControllerHandler(requestContext.RouteData);
    }
}

public class SessionHttpControllerHandler : HttpControllerHandler, IRequiresSessionState
{
    public SessionHttpControllerHandler(RouteData routeData) : base(routeData) { }
}

Create session support HttpControllerHandler, and route entry this class in Global.asax.

    routes.Add("SessionApis",
        new HttpWebRoute(
            url: "api/{controller}/{id}",
            defaults: new RouteValueDictionary(new {id = RouteParameter.Optional}),
            routeHandler: new SessionHttpControllerRouteHandler()
    ));

But, I think ApiController is good to use by sessionless. so scalable and REST principle.

LinqToEntitiesDataController route add at Custom AreaRegistration class( DALRouteRegistration class?). Change scaffolding default area register code.

from

    RouteTable.Routes.MapHttpRoute(
        "DAL", // Route name
        "api/DAL/{action}", // URL with parameters
        new { controller = "DAL" } // Parameter defaults
    );

to

    RouteTable.Routes.Add("SessionDALApis",
        new HttpWebRoute(
            url: "api/DAL/{action}",
            defaults: new RouteValueDictionary(new { controller = "DAL" }),
            routeHandler: new SessionHttpControllerRouteHandler()
    ));

How about this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top