0

我有一個簡單的項目,使用Microsoft MVC的身份驗證模板。在開發機器和IIS Express上進行調試時,我可以創建一個新用戶,我可以使用其默認控制器登錄,但是當我嘗試在IIS上發佈它時,我在註冊過程中捕獲到空引用異常。這裏是堆棧跟蹤:用戶註冊錯誤用戶註冊使用異步控制器的用戶

[NullReferenceException: Object reference not set to an instance of an object.] 
     TrabahoNow.Web.Controllers.<Register>d__8.MoveNext() +224 
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
     System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84 
     System.Web.Mvc.Async.<>c__DisplayClass37. <BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17 
     System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 
     System.Web.Mvc.Async.AsyncInvocationWithFilters. <InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 
     System.Web.Mvc.Async.<>c__DisplayClass46. <InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225 
     System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10 
     System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 
     System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 
     System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 
     System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 
     System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54 
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 
     System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54 
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 
     System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36 
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54 
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651796 
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

我不知道是什麼原因導致它在因特網上運行。這裏是我的控制器操作:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser() 
      { 
       UserName = model.Email, 
       FirstName = model.FirstName.ToUpper(), 
       LastName = model.LastName.ToUpper(), 
       MiddleName = model.MiddleName.ToUpper(), 
       Email = model.Email, 
       PhoneNumber = model.Phone, 
       DesiredPosition = model.DesiredPosition, 
       BirthDate = DateTime.Now, 


      }; 

      var result = await UserManager.CreateAsync(user, model.Password); 

      if (result.Succeeded) 
      { 
        await SignInAsync(user, isPersistent: false); 
        return RedirectToAction("Index", "Home");      

      } 
      else 
      { 
       AddErrors(result); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

這在IIS服務器中失敗。有人能幫我把這個弄出來嗎?

這裏是我的設置..

.NET 4.5.1

EF 6.0

MVC 5

+1

一個模型字段爲空(可能是名稱)和'ToUpper()'拋出異常。在調試器中運行它,並檢查你的模型。 –

+0

錯誤來當我在這條線實例化類ApplicationUser: VAR用戶=新ApplicationUser() { 用戶名= model.Email, 姓= model.FirstName.ToUpper(), 名字= model.LastName。 ToUpper的(), 中間名= model.MiddleName.ToUpper(), 電子郵件= model.Email, ******中國= model.Phone, DesiredPosition = model.DesiredPosition, 出生日期= DateTime.Now, }; 它調用控制器的dispose方法 –

+0

如果我只能接受你的評論作爲答案@BrendanGreen請將其發佈爲答案 –

回答

0

既然你有例外,確認錯誤發生在你構建你的類的實例時,你的問題是hig很可能是model.FirstName,model.LastNamemodel.MiddleName爲空。

致電ToUpper()將是您異常的根本原因。

無論是檢查空:

FirstName = string.IsNullOrEmpty(model.FirstName) ? null : model.FirstName 

或者,如果C#6提供給您,使用空傳播運營商:

FirstName = model.FirstName?.ToUpper(); 
+0

它對一個可爲空的middleName你是絕對正確的。謝謝。現在,c#方法更加優雅,但對我來說無濟於事。再次感謝。 –