【问题标题】:Getting an exception while loading view in mvc 5 when run after publish on server but working fine on local machine在服务器上发布后运行但在本地计算机上工作正常时在 mvc 5 中加载视图时出现异常
【发布时间】:2020-02-14 00:02:48
【问题描述】:

发布后在服务器上运行时出现异常,但它在本地系统上运行,无论是否发布。我坚持了一天,请帮忙解决。它在本地开发机器上运行良好,但在服务器中发布和部署后抛出异常

我尝试了构造函数更改、使用“使用”块更改数据库对象等

这是堆栈跟踪

[NullReferenceException: Object reference not set to an instance of an object.]
   RAMSWeb.Controllers.GTLMTVariableProcessController..ctor() +101

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +139
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +105
   System.Activator.CreateInstance(Type type) +12
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +75

[InvalidOperationException: An error occurred when trying to create a controller of type 'RAMSWeb.Controllers.GTLMTVariableProcessController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +242
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +103
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +263
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1020
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

控制器



  [HttpGet]
        public ActionResult CallEmployeesGT()
        {
            return View(new GTLMTCallEmployesVM());
        }


型号:

public class GTLMTCallEmployesVM
{
        private ApplicationDbContext db;

        System.Security.Principal.IPrincipal user = HttpContext.Current.User;

        public GTLMTCallEmployesVM()
        {
            db = new ApplicationDbContext();

        } 

        [Required]
        [Display(Name = "Month")]
        public string TransactionMonth { get; set; }

        [Required]
        [Display(Name = "Year")]
        public string TransactionYear { get; set; }


        [Required]
        [Display(Name = "To Date")]
        public DateTime SalaryToDate { get; set; }


        [Required]
        [Display(Name = "From Date")]
        public DateTime SalaryFromDate { get; set; }


        [Required]
        [Display(Name = "Distributor")]
        public string BranchCode { get; set; }



        private SelectList _branchSelectListGT { get; set; }
        public SelectList BranchSelectListGT
        {
            get
            {
                if (_branchSelectListGT != null) return _branchSelectListGT;
                else
                {
                    if (user.IsInRole(constant.Roles.SuperAdmin) || user.IsInRole(constant.Roles.Admin))
                    {
                        return new SelectList(db.Branches.Where(x => x.DistributorType == SalaryModuleHelper.DistributorType.GT && x.DisableDate == null).ToList().Select(y => new BranchSelectListVM { BranchCode = y.BranchCode, BranchName = y.Name + "(" + y.BranchCode + ") " + y.DistributorType }).ToList(), "BranchCode", "BranchName");
                    }
                    else
                    {
                        var loggedInUserBranches = constant.GlobalHelper.GetBranchIds(user.Identity.GetUserId());

                        return new SelectList(db.Branches.Where(x => loggedInUserBranches.Any(y => y.Trim() == x.BranchCode.Trim()) && x.DistributorType == SalaryModuleHelper.DistributorType.GT && x.DisableDate == null).Select(x => new BranchSelectListVM { BranchCode = x.BranchCode, BranchName = x.Name + " (" + x.BranchCode + ") "+ x.DistributorType }).ToList(), "BranchCode", "BranchName");

                    }
                }
            }
            set { _branchSelectListGT = value; }
        }
}

【问题讨论】:

    标签: c# asp.net-mvc entity-framework exception


    【解决方案1】:

    恕我直言,异常告诉您模型中的属性之一为空。我认为数据库连接有问题。你检查你的连接字符串了吗?

    【讨论】:

    • 是的,我已经检查过了,但问题仍然存在。发布后它正在开发机器上工作,但服务器部署有问题
    • 也可能是权限问题,请检查服务器帐户是否已添加到您的数据库安全性-> 同一用户存在用户和存储过程权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2018-10-22
    相关资源
    最近更新 更多