2 Replies - 3199 Views - Last Post: 17 May 2012 - 10:54 PM Rate Topic: -----

#1 waqas865  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-May 12

How to get Session ID in Controller in MVC 3.0?

Posted 16 May 2012 - 11:31 PM

Hello To All,
I have a little problem in getting session id in controller.Session id has not a constant value. its an object having username, password ,firstname but i want to get only username value and store it into a variable.
e.g var test = session["onGoingUser"]

Here , onGoingUser is an object having three values and i want only one value which should be store in test.So how could this possible.Or any other solution except this.

Urgently needed.

Is This A Good Question/Topic? 0
  • +

Replies To: How to get Session ID in Controller in MVC 3.0?

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 234
  • View blog
  • Posts: 1,315
  • Joined: 07-April 08

Re: How to get Session ID in Controller in MVC 3.0?

Posted 17 May 2012 - 06:41 AM

Basically Session is just an object container. when you cast it like you are to var test you're creating an object named test. You will need to tell the compiler to treat this object AS something What is your back end language? are you using VB.NET or C#?

in C# you would do something like this:

var test = (Session["onGoingUser"] as <object>);

if(test != null)
{
    var value = test.<value>;
}



This basically says Cast your session object as the object you specify. This is safe because if it can't cast it then you will receive a null back instead of an exception.
Was This Post Helpful? 1
  • +
  • -

#3 waqas865  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-May 12

Re: How to get Session ID in Controller in MVC 3.0?

Posted 17 May 2012 - 10:54 PM

Sorry for late reply brother..backend language is C# ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1