我正在努力熟悉 OWIN,但有很多事情让我感到困惑。例如,在部分 startup.cs 类中,我通过以下方式注册上下文回调
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
有什么不同?为什么我们需要那个泛型方法?
我可以得到这样的上下文:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Get 和 GetUserManager 方法之间有什么区别?为什么我不能为 ApplicationUserManager 调用 context.Get?
最佳答案
Get<UserManager>
之间没有区别和 GetUserManager<UserManager>
这是两者的源代码...
/// <summary>
/// Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T Get<T>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<T>(GetKey(typeof (T)));
}
/// <summary>
/// Get the user manager from the context
/// </summary>
/// <typeparam name="TManager"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static TManager GetUserManager<TManager>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<TManager>();
}
关于asp.net - context.Get() 和它的通用版本有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585624/