asp.net mvc:Error1:值不能为null

本文关键字:不能 null Error1 net mvc asp | 更新日期: 2023-09-27 18:29:34

我有一个注册页面。它在我的本地计算机中工作,但在我部署到生产之后。一直在获取以下错误消息

            Server Error in '/' Application.
            Error1: Value cannot be null.
            Parameter name: view
            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.Exception: Error1: Value cannot be null.
            Parameter name: view
            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: 

            [Exception: Error1: Value cannot be null.
            Parameter name: view]
               Smoothie.Web.Controllers.AccountController.Signup(UserRegisterViewModel user, String returnUrl) +662
               lambda_method(Closure , ControllerBase , Object[] ) +149
               System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
               System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +181
               System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
               System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +56
               System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +256
               System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +22
               System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +190
               System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +311
               System.Web.Mvc.Controller.ExecuteCore() +105
               System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +88
               System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
               System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
               System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
               System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
               System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
               System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +31
               System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
               System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23
               System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59
               System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
               System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9690172
               System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是我的代码,我花了一整天的时间调试它,仍然没有运气。

    //[ChildActionOnly]
    public virtual ActionResult Signup(string returnUrl)
    {
       return PartialView(MVC.Account.Views._Register, new UserRegisterViewModel());
        //return PartialView("_Register", new UserRegisterViewModel());
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Signup(UserRegisterViewModel user, string returnUrl)
    {
        try
        {
            if (ModelState.IsValid)
            {
                var newUser = _mappingService.Map<UserRegisterViewModel, User>(user);
                if (newUser == null)
                {
                    throw new Exception("newUser cannot be null");
                }
                var confirmation = _userService.AddUser(newUser, AccountType.Smoothie);
                if (confirmation.WasSuccessful)
                {
                    var userData = _mappingService.Map<User, UserDataDto>(confirmation.Value);
                    if (userData == null)
                    {
                        throw new Exception("userData cannot be null");
                    }
                    _authenticationService.SetAuthCookie(Response, userData.DisplayName, false, userData);
                    Utilities.SendEmail(ConfigurationManager.AppSettings["EmailReply"],
                                        new List<string> {userData.Email}, "Welcome to putastrawinit.com",
                                        GetWelcomeEmail());
                    var redirectUrl = "/home";
                    if (returnUrl != null && Url.IsLocalUrl(returnUrl))
                    {
                        redirectUrl = returnUrl;
                    }

                    return Json(new {Success = true, RedirectUrl = redirectUrl});
                    //return RedirectToAction("Index", "Home", null);

                }
                ModelState.AddModelError("", confirmation.Message);
            }
        }
        catch (Exception e)
        {
            throw new Exception("Error1: " + e.Message);
        }
        var errors = from value in ModelState.Values
                     from error in value.Errors
                     select new { error.ErrorMessage };
        //throw new Exception("Errors: " + errors.ToString());
        return Json(new { Success = false, ErrorMessages = errors });
    }

注册查看页面。

            @model Smoothie.Domain.ViewModels.UserRegisterViewModel
            @using (Ajax.BeginForm("Signup", "Account", new AjaxOptions { OnSuccess = "onSignupSuccess", OnFailure = "onSignupFailure" }))
            {
                @Html.AntiForgeryToken()
                @Html.Hidden("returnUrl", null, new { value = @Request.QueryString["returnUrl"] })    
                <div class="signupForm">
                    <div>@Html.ValidationSummary("", new { @id = "signupSummary" })</div>
                    <div class="row">
                        <label for="Email">
                            Display Name:</label>
                        @Html.EditorFor(m => m.DisplayName)
                    </div>
                    <div class="row">
                        <label for="Email">
                            Email Address:</label>
                         @Html.TextBoxFor(m => m.Email, new {@class = "text-box", autocomplete = "off" })
                    </div>
                    <div class="row">
                        <label for="Password">
                            Password:</label>
                        @Html.TextBox("Password", null, new { @class = "registerPassword text-box", type = "password", autocomplete = "off" })
                    </div>
                    <div class="row">
                        <label>
                            &nbsp;</label>
                        <div class="registerChecker">
                        </div>
                    </div>
                    <div class="row">
                        <label>
                            &nbsp;</label>
                        <input type="image" src="@Links.Content.images.btn_sign_up_png" style="width: 91px; height: 33px;" value="Create" />
                    </div>
                </div>
            }

这就是ViewModel。

            using System.ComponentModel.DataAnnotations;
            namespace Smoothie.Domain.ViewModels
            {
                public class UserRegisterViewModel
                {
                    [Required(ErrorMessage = "Email is required")]
                    [StringLength(255, ErrorMessage = "Email must be 50 characters or fewer")]
                    [RegularExpression(@"'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*", ErrorMessage = "Your Email address is invalid")]
                    public string Email { get; set; }
                    [Required(ErrorMessage = "Display name is required")]
                    [StringLength(25, MinimumLength = 2, ErrorMessage = "Display name must be between 2 and 25 characters")]
                    public string DisplayName { get; set; }
                    [DataType(DataType.Password)]
                    [Required(ErrorMessage = "Password is required")]
                    [StringLength(25, MinimumLength = 8, ErrorMessage = "Password must be between 8 and 25 characters")]
                    public string Password { get; set; }
                }
            }

这是我从代码中删除try-catch之后的新堆栈跟踪。

        Value cannot be null.
        Parameter name: view
        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.ArgumentNullException: Value cannot be null.
        Parameter name: view
        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: 

        [ArgumentNullException: Value cannot be null.
        Parameter name: view]
           System.Web.Mvc.ViewContext..ctor(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer) +205
           Smoothie.Web.Controllers.AccountController.GetWelcomeEmail() +178
           Smoothie.Web.Controllers.AccountController.Signup(UserRegisterViewModel user) +288
           lambda_method(Closure , ControllerBase , Object[] ) +106
           System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
           System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +181
           System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
           System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +56
           System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +256
           System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +22
           System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +190
           System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +311
           System.Web.Mvc.Controller.ExecuteCore() +105
           System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +88
           System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
           System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
           System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
           System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
           System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
           System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +31
           System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
           System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23
           System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59
           System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
           System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9690172
           System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

asp.net mvc:Error1:值不能为null

根据最新的调用堆栈,GetWelcomeEmail()中似乎缺少视图,这导致view在ViewContexts构造函数中为null。