【发布时间】:2018-04-30 21:50:38
【问题描述】:
目前在创建管理员用户后我的控制器出现问题。 这是用于创建管理员用户的代码:
protected override void Initialize(RequestContext requestContext)
{
using (var ac = new ApplicationDbContext())
{
var userManager = new ApplicationUserManager(new
UserStore<ApplicationUser>(ac));
var roleManager = new RoleManager<IdentityRole>(new
RoleStore<IdentityRole>(ac));
var user = new ApplicationUser { UserName = "hello@gmail.com",
Email = "hello@gmail.com" };
userManager.Create(user, "Hello1234!");
roleManager.Create(new IdentityRole("admin"));
user = userManager.FindByNameAsync(user.UserName).Result;
userManager.AddToRole(user.Id, "admin");
}
}
因错误而更新
当我导航到产品控制器时,我在浏览器中收到此错误:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Controller.PossiblyLoadTempData() +11
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +39
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.2633.0
【问题讨论】:
-
需要调用base.Initialize()来初始化Controller基类。
标签: c# asp.net-mvc