C# 在服务器上发布后运行,但在本地计算机上正常工作时,在mvc 5中加载视图时出现异常

C# 在服务器上发布后运行,但在本地计算机上正常工作时,在mvc 5中加载视图时出现异常,c#,asp.net-mvc,entity-framework,exception,C#,Asp.net Mvc,Entity Framework,Exception,发布后在服务器上运行时获取异常,但该异常正在本地系统上运行(无论是否发布)。我在里面呆了一天,请帮忙工作。它在本地开发机器上运行良好,但在服务器中发布和部署后引发异常 我尝试了构造函数更改、数据库对象更改和“使用”块等 这是堆栈跟踪 [NullReferenceException: Object reference not set to an instance of an object.] RAMSWeb.Controllers.GTLMTVariableProcessController

发布后在服务器上运行时获取异常,但该异常正在本地系统上运行(无论是否发布)。我在里面呆了一天,请帮忙工作。它在本地开发机器上运行良好,但在服务器中发布和部署后引发异常

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

这是堆栈跟踪

[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; }
        }
}

异常告诉您模型中的一个属性为null。我认为数据库连接有问题。是否检查了连接字符串?

是,我已检查,但问题仍然存在。发布后,它正在开发计算机上工作,但服务器部署上的问题也可能是权限问题,请检查数据库安全->用户中是否添加了服务器帐户以及同一用户是否存在存储过程权限。