3

I want to access asp.net(C#) session value in angularjs file, what should be the best way to access session value.

What i tried, but couldn't able to get it.

 $session.get(key) 

3 Answers 3

3

ASP.NET is backend and Angular runs in the browser. ;-)

You have several options:

  1. write the session into a cookie (if not already done by ASP.NET) and read the value via the angular-cookies library

  2. write a RESTful service to get some session information value

  3. render the value into a hidden field and read it via jquery

Sign up to request clarification or add additional context in comments.

5 Comments

first of all thanks, i can not use hiddenfield because i have many sessions in my application and i will need most of them to use in my angularjs page. is there any way to access session in angular like we are accessing in javascript and jQuery like <%=Session["UserID"]%>
you can do that if the 'HTML' file will be rendered on the server side. Angular will download the HTML templates only once. so the value should never change within the same session
can you provide me an example how can i do it using Cookie?
awaiting for your response.
sorry, I was working - You can find here the documentation of angular-cookies. You can use $cookies.get(key) or $cookies.getObject(key), that depends on what information you wanna put into the cookie. Personally I'd use a web service and get the information with a GET request and JSON format
1

You need to do this through a server side controller, the session is stored on the server, while angular will run on the client. So you can make a Session controller to receive a key and to return in JSOn format the value from the Session.

2 Comments

how can use session controller?
there isn't anything part of asp.net, you should make it yourself, but it should be a simple controller which will get a key and then return the object from the session serialized in json
0

Expanding on Michael's 2nd option "write a RESTful service to get some session information value"

One possible way to do this is

  1. Create and set your session object as usual
  2. Redirect the user to your html page with a query param "google.com?sm=1"
  3. Look for the "sm" query param in angularjs/javascript and make an api call that returns your session object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.