asp.net-core-mvc - 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 的页面显示正常。
我的问题:为什么这在本地工作,而不是从网络服务器?如何在不重写整个身份验证方案的情况下(希望)纠正这个问题?
解决方案
推荐阅读
- python - 如何增加元组中的项目
- amazon-web-services - 在 EC2 实例上传输基本 yii2 应用程序
- java - 如何使用eclipse与MySQL数据库连接java
- angularjs - Google 地图在 Ionic v1 中无法正常工作
- raspberry-pi3 - 如何更改 2x16 LCD 显示器的背光颜色
- python - Python对Pandas数据框进行排序会产生随机结果
- sql - 如何通过 SQL 中的某些键删除以区分
- swift - 呈现后无法关闭视图控制器
- typescript - 是否可以在 TypeScript 中的元组解构期间创建一些变量?
- jaxb - 具有多命名空间的 JAXB 解组