[英]UNITY MVC CONTAINER CATCH NULL EXCEPTION IN ACCOUNT CONTROLLER WITH AUTHORIZE ATTRIBUTE
I have a question.我有个问题。 I'm new to UnityContainer
.我是UnityContainer
。 I use Unity for DI.我使用 Unity 进行 DI。
My ASP.NET MVC web app has an AccountController
(which was auto-generated by ASP.NET Identity) with the [Authorize]
attribute.我的 ASP.NET MVC Web 应用程序有一个带有[Authorize]
属性的AccountController
(由 ASP.NET Identity 自动生成)。
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
[InjectionConstructor]
public AccountController() { }
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
private set { _userManager = value; }
}
[AllowAnonymous]
public ActionResult Login(string returnUrl) { ViewBag.returnUrl = returnUrl; return View(); }
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model)
{
//My code
return RedictToAction("Index","Home");
}
//Some Methods
}
View my complete code: My simple AccountController was auto-generated by VS - Identity查看我的完整代码:我的简单 AccountController 是由 VS-Identity 自动生成的
I try to inject constructor for AccountController
.我尝试为AccountController
注入构造函数。
My code is in App_Start > UnityConfig.cs
:我的代码在App_Start > UnityConfig.cs
:
public static class UnityConfig
{
public static IUnityContainer Container;
public static void RegisterComponents()
{
var container = new UnityContainer();
//register all my components with the container here
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
container.RegisterType<DbContext, ApplicationDbContext>(new PerRequestLifetimeManager());
container.RegisterType<UserManager<ApplicationUser>>();
container.RegisterType<ApplicationUserManager>();
container.RegisterType<AccountController>(new InjectionConstructor());
DependencyResolver.SetResolver(new Unity.AspNet.Mvc.UnityDependencyResolver(container));
}
}
When I start to run the app, and call a method on the AccountController
, I ge tthis error message:当我开始运行应用程序并调用AccountController
上的方法时,我收到此错误消息:
Value cannot be null.值不能为空。 Parameter name: container参数名称:容器
Please click below link to view the error message:请点击以下链接查看错误信息:
The Exception was thrown.异常被抛出。 Url's localhost:xxxxx/Account/Login Url 的 localhost:xxxxx/Account/Login
I found that when I removed [Authorize]
and [ValidateAntiForgeryToken]
attributes, everything worked well.我发现当我删除[Authorize]
和[ValidateAntiForgeryToken]
属性时,一切正常。
But when I added [Authorize]
or [ValidateAntiForgeryToken]
attributes, the exception was thrown.但是当我添加[Authorize]
或[ValidateAntiForgeryToken]
属性时,抛出了异常。
Please help me to fix it.请帮我修复它。
I really appreciate it!对此,我真的非常感激!
I found my answer.我找到了我的答案。 In Application_Start
在Application_Start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.RegisterComponents(); //<--- Incorrect
}
And I adjusted it我调整了它
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents(); //<========
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.