How to add a request wise variable in httpcontext - asp.net-mvc

In HttpContext(Or some thing like this) I need to add a temperory variable from a controller which need to available through out the request processing(Request wise variable). But the HttpContext.Current.Request is readonly. If i'm adding in Items its not getting outside. How can i achieve this
Thanks & Regards
Binesh Nambiar C

You are looking for HttpContext.Items, which is a dictionary that can be used to store items for the duration of the current request. It goes out of scope at the end of the request.
// Set
HttpContext.Items["Customer"] = customer;
// Get
var customer = HttpContext.Items["Customer"];

Related

Store object in the session

I would like to store some object in the session. I know, that there are at least two ways to do it:
in the service define scope = "session", and then define property def myObject
use httpSession: session['myObject'] = myObject
What is more useful approach to store object in the session?
Update: What the benefites of using of every method? Can I invalidate session if I would use scope = 'session'?
Update 2 If I would like to use object of another class in the service with session = "session", I have an exception about bean with scope session.
Based on the information you have provided storing an object in the session itself would be the simpler approach. Just be sure whatever you do store there is as small as possible.
session['someKey'] = new MyObject()
The documentation has some more information about using the session.
Unless you have a requirement for using an actual instance of an object you may find it easier to simply store a map in the session instead.
session['someKey'] = [mapKey1: 'value', meaningKey: 42]
...
println session['someKey'].meaningKey // 42

Getting a unique object from a indexedDB in Dart

I'm trying to get information from an indexedDB, and I want to get just the object from one store by key, and not all the objects from the database. In some examples of javascript and IndexedDB they use the method get() to get the value depending of the key. In DART there is not such method but there is getObject().
How do I get the value of the object when using getObject(key)?
Thanks a lot in advance!
Actually it has getObject(key) method, take a look here https://api.dartlang.org/apidocs/channels/stable/#dart-dom-indexed_db.ObjectStore#id_getObject
The other way for fetching data is Cursor. Here is a good tutorial https://www.dartlang.org/docs/tutorials/indexeddb/#getting-data
So all needed to do was to create a callback. This is how I got the objects value:
var id = "example";
var trans = _db.transaction('myDB', 'readwrite');
var store = trans.objectStore(_TODOS_STORE);
// Get the value from one object ! and render it
var request = store.getObject(id).then((val){functionFor(val);});
Futures need from callbacks to be able to use their values.

Using what to create cookies

Wherever I look this is the correct way to add a Cookie:
HttpCookie Session = new HttpCookie("Session");
Session.Value = someguid;
Session.Expires = somedatetime;
Response.Cookies.Add(Session);
And wherever there is no explanation as to how to get Response into the current context. So I get this error:
The name 'Response' does not exist in the current context
What do I need to do to get the above code to work?
You may want to use System.Web.HttpContext.Current.Response if you sure that this code will be executed inside web application.
But I have life-proven practice (and Law of Demeter says the same) to pass Response as a parameter to a method where I want to use it.

Session deletion in Symfony 1.4

How do I delete all session variables at once if they are not in Array?
PS I set them this way:
$this->getUser()->setAttribute('PayPalTransaction.hash', $request->getParameter('hash'));
Regards,
Roman
The sfUser class (which you get with $this->getUser()), keeps all it's attributes in a sfNamespacedParameterHolder. So the setAttribute() function on sfUser if merely a proxy to the sfNamespacedParameterHolder::setAttribute(). You can get the reference to this holder with sfUser::getAttributeHolder().
The sfNamespacedParameterHolder also has a function clear(), which clears all attributes.
So to clear all attributes, use:
$this->getUser()->getAttributeHolder()->clear().
(Please note that you will still be authenticated (e.g. logged in) when you clear the attribute holder).
Another way if you want to remove just one session variable not all of them is to use the following code
$this->getUser()->getAttributeHolder()->remove('att_name');
Again this will only remove one not all ... to clear all use the previous code by Grad
To remove all attributes of a namespace :
$this->getUser()->getAttributeHolder()->removeNamespace('yournamespace');

Update only the changed values on Entity object

how can i automatically update my entity objects changed values and save them to db.
I hava an Action like that
public ActionResult Update()
{
User userToUpdate = new User();
TryUpdateModel<User>(userToUpdate,ValueProvider);
BaseRepository.Context.AttachTo("User",userToUpdate);
BaseRepository.Context.SaveChanges();
return Json("");
}
ValuProvider : has the items that come
from the client as post data.
The problem on this code is the code update all the values but i want to update only the changed values.
How can i find the changed values on my entity object.
You should check out the ObjectContext.ApplyPropertyChanges Method
it is suppose to do what your asking for...
msdn
Two options:
On the View you could know the values that were changed by using Javascript and then you could pass that information to your controller.
You could simply compare the previous values (which you already have since you populated a view) and check each value before updating the DB.
I prefer last option, since at this point you could also check for data validation.
This is really a problem for your data access code, not anything to do with your controller. Pick an ORM that handles this for you and forget about the problem.

Resources