is there any alternative for session - c#

is there any other way to save our data
in session when we save data dos not mater what is the type of it
save:
Session["instauser"] = _instaApi;
use:
var IInstaApi = Session["instauser"] as InstaApi
i know for save in db or file we should serialized the data but i cant.
because the InstaApi contain
HttpClient , HttpClientHandeler ,...
Is there any thing like session?
but keep data in db or file, not memory like session.
Update
That is private instagram api we create instant of it and login with it(no mater how!) and then if we could save that object (instants of InstaApi ) then we can use it for ever no need to login again

If its purpose is for use within controller and view of MVC, other alternatives are ViewBag, ViewData and TempData.
ViewBag and ViewData life is only for one request, while TempData can be persisted.

Take a look at APC_STORE, unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests.
http://php.net/manual/en/function.apc-store.php

Related

what is the scope of Viewbag in mvc3 and how do we maintain state in MVC

what is the scope of Viewbag in mvc3 is it only available on the page we are rendering through my action method.
How do we maintain information across the page in MVC. Suppose i Create new employee and and when I move to next page I want that employee information.
How do we maintain state in MVC.
the view bag is part of the httpcontext. it's primarily set in the controller action and read in the view, but it can be accessed from just about anywhere in the mvc framework within an http request/response.
the web does not have state, like you would in a rich client app. To maintain values from page to page (or more appropriately, request to request) you can use cookies, session, query string, the request body (think post/put requests).
same as #2.
ViewBag is a dynamic expression and it is available for all the pages. The data in ViewBag is the thing that changes as per we have assigned. If we are assigning ViewBag.items=itemlist; for a View, then it will be constant for that View. We can put as many data into ViewBag per page as we want like , for a single page, we can have
ViewBag.items=itemlist;
ViewBag.table=usertable;
You can maintain the information across the pages in MVC by passing the data as a parameter to the method which rendres the View on which we want to maintain the information as below:
public ActionResult CreateEmployee(EmployeeModel emp)
{
//Add Employee to db
ViewBag.employee=emp;
RedirectToAction("MethodToCall","Controller");
}
Thus the next page can have the employee information contained in the ViewBag
Alternatively, you can use ViewData as well.
3] State can be maintained using sessions, cookies etc

pass C# object between aspx pages

I am creating an ASPX application that collects data from the user can creates a file based on this data. Different types of data are collected based on the type of user. Also, I have to check the database to see if there is data already in the database. I am looking for an easy way to keep track of the data while traversing between all the ASPX pages that are collecting data. I know i can pass the data in the URL and if i don't find an alternative method then i will use that.
My question is, is it possible put the data into a C# object and somehow pass the instance of that object between ASPX pages?
The simplest is to use the Session bag.
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
When retrieving an object from session state, cast it to the appropriate type.
ArrayList stockPicks = (ArrayList)Session["StockPicks"];
Here's a more exhaustive article on Session state on MSDN: http://msdn.microsoft.com/en-us/library/ms178581.aspx (from which the brief code examples are shamelessly copied)
Assuming the data is not prohibitively big, I'd put the data in Session and retrieve it from page to page.
You can use Session object for this.

ViewBag Usage-ASP.NET MVC3

In my controller say ControllerFirst I am setting a ViewBag property by the below line.
ViewBag.PreviousPage="ThisIsComingFromControllerFirst";
return RedirectToAction("ControllerSecond", "Home");
Now from here:
public ActionResult ControllerSecond()
{
return View();
}
I am trying to use Viewbag in the ControllerSecond by the following
View: ControllerSecond.cshtml
#if(ViewBag.PreviouPage == "SomeValue")
{
//do this
}
But ViewBag.PreviousPage value is null.
Please let me know why its null, what could I do to get the value in my view from the ControllerFirst.
I have done this one using Session, but We don't want to sessions..
Any other options?
To answer your first question, ViewBag is a more convenient form of ViewData that uses dynamic objects rather than generic ones. As with ViewData, ViewBag only exists for the life of a single request, thus it is only available between the ActionMethod and it's view. It is not available between different ActionMethods.
Think of it like an intercom system in a home. You can send messages to other parts of the home, but you can't send a message to a neighbors home.
The only other options you have are:
Use Session
Use TempData (which also uses session)
Use a Cookie
Use a querystring parameter
Use an intermediate table in your database
Post to ActionMethod
ViewBag (and ViewData) are objects for accessing extra data (i.e., outside the data model), between the controller and view.
If your data have to persist between two subsequent requests you can use TempData.
However, TempData is by default stored in the session.
So, if you don't want to use sessions, you could use cookies and somehow duplicate a bit of what session does for you, as MikeSW suggested.
When to use ViewBag, ViewData, or TempData in ASP.NET MVC 3 applications
Use TempData instead of ViewBag
You can use a cookie and somehow duplicate a bit of what session does for you.

Global Variable inside of Controller MVC

So, I have this variable that I push into the controller via POST from a form in my view.
I then push the variable into viewdata so it's available to the next view which is fine. But that view has no need of a form so I'm unable to push that same variable into the next controller. In short, it's cumbersome to push pieces of information back and forth from controller to view and reverse, so I'm looking for a way to keep a global variable alive inside a controller so that it's accessible by all action results... The general breakdown of my program is this...
-User types a "name"
-I send "name" to controller.
-I push 'name' into viewstate (query entity framework to get a list of stuff 'name'
has access to) and return that list into the view.
-In that view I can access the 'name' since it was in view state.
-Clicking on a link inside the page takes me to another controller where I need
to get access to 'name' WITHOUT passing view Routing or POST.
Obviously the easiest way would be to declare 'name' globally and then it's always available but for the life of me I can't figure out how.
Have you considered storing it in the Session?
This will allow you to easily access it, either from your controller or views, and avoids the need for global variables.
Storing:
[HttpPost]
public ActionResult YourPostMethod(string name)
{
Session["Name"] = "yourName";
}
Access: *
Make Sure to check that it exists prior to grabbing it:
var whatsMyName = (Session["Name"] != null) ? Session["Name"] : "";
Scalability Consideration
It's worth mentioning that MVC applications are designed to mimic the web and are stateless. Introducing Session variables changes this, so be aware that it can introduce issues regarding scalability, etc.
Each user that stores data within the Session will take up resources at the server level. Depending on the number of users and what you are storing within the Session, you could potentially run out of memory if those values become too large.
Why not use the session object, which is an associative array which lives while the client is connected?
$_SESSION['name'] = "zzzz"; // store session data
name = $_SESSION['name']; //retrieve data
You can use this for each user till their session is active.. hope this helps

Where and which data to save into session on an ASP.NET MVC 2 application?

I am having some trouble saving the state of my current view.
Currenly I have several selectlist calling their own Action method on the controller that returns the Index view with the filtered model based on the values of the selectlist.
I have also written a little FileResult action that creates a csv file based on the current model. But I am only covering one selectlist right now as I only save the value of selectList1 into the session and access it with Session["SelectListValue1"]
What are the best practices in this situation?
Should I redo the entire (each action for each SelectList) part?
Should I save each SelectLists value into the session and check if it's null?
Or should I just save the Lambda Expression into the session and modify it during every call?
Well, generally in MVC we don't directly save to Session, it's not considered a best practice b/c of impact to your app's performance. Generally, it's a best practice to make each request as stateless as possible.
Each form should follow the POST-Request-GET pattern where possible, so you're not going to do what you did in WebForms as a rule (where you keep posting back to the same form/action).
So you should consider what the state is that you're trying to capture represents. THe list of possible values is one thing, drawn possibly from a database and stored as a list or enumerable in the cache perhaps (in some scenarios; could look it up every time in others). The value that's selected probably represents a property on osme other object, though, so you should use that as your means of getting out the selected value.
If it's something that's not a part of a persistent object, then you can either just read the post values each time and set the viewstate again (probably the best practice) or, if you need to persist that value across a redirect, then use the TempData bag (which works much like session; in fact uses session under the hood) but values get garbage collected after one the next request, so you don't have to worry as much about the memory bloat.
It doesn't sounds like you need to be using the session at all. Can't you pass the values of your select lists via the query string or in a form?

Categories