I have a value which I want to be vaild during a single request. I am not using Session, as this would make the value global for the entire navigation session.
So I have put thie value in a static field of a class. Great, but then I discovered that such fields are even more global, that is, they stay set for the entire application! This means that there could be random interaction among navigation sessions.
So the question is: is there a safe place I can put a global variable, which will be
Thanks Palantir
EDIT I'll elaborate. I have a piece of code in my master page, which I need to hide on certain conditions, of which I am aware in the controller only. I thought about setting a static variable in the controller, which then would be queried by the master page, but now I see there could be a better way...
ASP.NET Global Variables Example Use global variables in ASP.NET websites by specifying static fields. Global variables are useful in ASP.NET. They don't need to be copied and only one is needed in the website application.
Global: Static fields and properties in ASP.NET are always global. Another copy is never created. You will not have duplication. Performance: Static fields are efficient in normal situations.
A problem with the Application object in ASP.NET is that it requires casting to read its objects. This introduces boxing and unboxing. Static fields allow you to use generics. Is this recommended? Yes, and not just by the writer of this article.
Use HttpContext.Items
- a per-request cache store. Check out this article on 4guysfromrolla for more details.
It should work fine in ASP.NET MVC. You may wish to derive your master page from a base class (either via code-behind or using the Inherits directive) and have a protected method on the base class that inspects HttpContext.Items and returns, e.g. true/false depending whether you want to display the conditional code.
TempData
lasts until the next request as already noted.
But there are also two other dictionaries scoped to the single request.
Controller
,ViewPage
}.ViewData
Context.Items
To communicate from controller to (master) page ViewData
is probably the better choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With