Reputation: 2317
I'd like to have specific static data (site menu DTO in particular) shared across all application requests. In the old system.web.dll
days, that would be adding data in Application_Start
into HttpContent.Current.Application[]
dictionary. I'm sure very similar can be achieved with Owin / OwinContext but cannot find the easy way how to add it / access it. Can anyone help?
Upvotes: 2
Views: 1343
Reputation: 54628
The Microsoft.AspNet.Identity.Owin
library contains the class OwinContextExtensions
which has the following methods:
public static T Get<T>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<T>(OwinContextExtensions.GetKey(typeof(T)));
}
public static T Set<T>(this IOwinContext context, T value)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Set<T>(OwinContextExtensions.GetKey(typeof(T)), value);
}
I'm pretty sure you can use these to set and get values stored in the OwinContext
. Notice that the key name of the object stored in the context is the type, so for collections you should create a concrete type for the unique name:
public MyDictionary : Dictionary<string, int>
{
}
var myDic = new MyDictionary();
var context = HttpContext.GetOwinContext();
context.Set(myDic);
var myDic2 = context.Get<MyDictionary>();
Upvotes: 2