首页 > 解决方案 > ExceptionHandlerMiddleware|发生未处理的异常:对象引用未设置为对象的实例

问题描述

我在其他帖子中看到过这个问题,但我相信我的情况可能会有所不同。我的代码在 localhost 上运行良好,但在部署到 Web 服务器时出错。使用 ASP.NET Core 2.0 和 SQL Server 2012。我相信这与我的UserType班级(Model.LoggedInUserType)有关:

public string GetUserType(string LoginID)
{
     string user = ClaimsPrincipal.Current.Identity.Name;

     Employee employee = db.tblEmployee.FirstOrDefault(e => (e.FirstName + '.' + e.LastName) == user);
     string userType = employee.UserType;

     return userType;
}

用户类型视图模型.cs

public string LoggedInUserType = BaseInfoClass.UserType;

错误(来自日志):

2018-09-12 16:06:04.9272|2|INFO|Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|在 906.2122 毫秒内执行的操作 ApostilleDNCv2.Controllers.TransactionController.UpdateTransaction (ApostilleDNCv2) 2018-09-12 16:06:04.9272 |3|INFO|Microsoft.AspNetCore.Session.DistributedSession|会话开始;键:68a158b8-c8bb-9d16-6e66-8c1a3942f3be,Id:9c7a85af-15d4-b8c1-1286-c0ca515cb1e2 2018-09-12 16:06:04.9433||错误|Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware|发生了未处理的异常: 你调用的对象是空的。System.NullReferenceException:对象引用未设置为对象的实例。在 C:\TFS\Corporations\Development\Src\ApostilleDNCv2\ApostilleDNCv2\Views\Shared\Components\Apostille\default.cshtml 中的 AspNetCore._Views_Shared_Components_Apostille_default_cshtml.d__22.MoveNext():

查看(UpdateTransactionPost.cshtml)代码:

<div class="col-lg-offset-2">
        @await Component.InvokeAsync("Apostille", new { tid = Model.Trans.TransactionID })
</div>
<div class="col-lg-offset-2">
        @await Component.InvokeAsync("Certificate", new { tid = Model.Trans.TransactionID })
</div>

查看组件(Shared\Components\Apostille\default.cshtml - 行:42):

<th class="txtctr" style="@(Model.LoggedInUserType.ToString() != "ADM" ? "display:none" : "" )">
                    @Html.DisplayName("Delete")
</th>

样式参考的目的是使删除(和某些其他功能)仅对管理员可见/可用。

此网页显示 400 错误。其他不依赖 UserType 的页面显示正常。

我的问题:为什么这在本地工作,而不是从网络服务器?如何在不重写整个身份验证方案的情况下(希望)纠正这个问题?

标签: asp.net-core-mvc

解决方案


推荐阅读