我正在将现有的Asp Mvc项目转换为.Net Core,现在的问题是此get方法
public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } }在Mvc中,我们可以使用
来获取OwinContextHttpContext.GetOwinContext().GetUserManager()
但是在不存在.Net Core GetOwinContext的情况下,当我比较两个类的HttpContext的定义时
在Mvc中,HttpContextBase是类型,但是在Core中,HttpContext与HttpContextBase在同一命名空间中没有扩展方法,这就是为什么找不到此扩展方法的原因,请让我知道如何在Core中做到这一点? >
解决方案
没有其他选择. ASP.NET Core几乎所有内容都使用依赖项注入.如果需要UserManager<TUser>,则可以将您正在使用的任何类的构造器(控制器等)注入到构造器中:
public class MyController : Controller { private readonly UserManager<ApplicationUser> _userManager; public MyController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } ... }I am converting an existing Asp Mvc project to .Net Core now the issue is this get method
public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } }In Mvc we could fetch OwinContext using
HttpContext.GetOwinContext().GetUserManager()
but in case of .Net Core GetOwinContext not present , When I compare definition of HttpContext of both classes
HttpContextBase is type in case of Mvc but in case of Core there is HttpContext which has no extension method in same namespace as HttpContextBase that's why this extension method was not found ,Please let me know how to do this in Core?
解决方案There is no alternate. ASP.NET Core uses dependency injection for pretty much everything. If you need UserManager<TUser>, you inject into the constructor of whatever class you're working with (controller, etc.):
public class MyController : Controller { private readonly UserManager<ApplicationUser> _userManager; public MyController(UserManager<ApplicationUser> userManager) { _userManager = userManager; } ... }
更多推荐
在ASP.NET Core中的GetUserManager的替代方法
发布评论