无效的Cast异常 - 强制转换无效

时间:2017-02-20 23:52:49

标签: c#

这是我网站上的错误。在Visual Studio中没有错误,它编译得很好。当我在本地运行网站并单击按钮以在Chrome或任何其他浏览器中显示区域时,会发生这种情况。我完全被这个难过,我正在寻找任何帮助?非常感谢。

System.InvalidCastException: Specified cast is not valid.
at MySite.Web.Areas.Mpa.Controllers.ZonesController.d__7.MoveNext() in C:\MySite\Development\routing_branch\src\MySite.Web\Areas\Mpa\Controllers\ZonesController.cs:line 125
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f()

这是我调用的方法,显示第125行:

93 public async Task<PartialViewResult> MapViewModal()
94  {
95      int? impersonatorTenantId;
96      int value;
97      object obj;
98      IZoneAppService zoneAppService = this._zoneAppService;
99      if (this.AbpSession.ImpersonatorTenantId.HasValue)
100     {
101         impersonatorTenantId = this.AbpSession.ImpersonatorTenantId;
102         value = impersonatorTenantId.Value;
103     }
104     else
105     {
106         impersonatorTenantId = this.AbpSession.TenantId;
107         value = impersonatorTenantId.Value;
108     }
109     List<ZoneListDto> zonesByTenantId = await zoneAppService.GetZonesByTenantId(value, true);
110     if (zonesByTenantId == null || zonesByTenantId != null && zonesByTenantId.Count == 0)
111     {
112         zonesByTenantId = new List<ZoneListDto>();
113     }
114     ITenantSettingsAppService tenantSettingsAppService = this._tenantsettingsAppService;
115     if (this.AbpSession.ImpersonatorTenantId.HasValue)
116     {
117         impersonatorTenantId = this.AbpSession.ImpersonatorTenantId;
118         obj = impersonatorTenantId.Value;
119     }
120     else
121     {
122         impersonatorTenantId = this.AbpSession.TenantId;
123         obj = impersonatorTenantId.Value;
124     }
125     string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates((long)obj);
126     ZonesMapView zonesMapView = new ZonesMapView()
127     {
128         Zones = zonesByTenantId,
129         TenantCoordinates = tenantCoordinates
130     };
131     return this.PartialView("_MapViewModal", zonesMapView);
132 }

我的桌面布局是这样的:

dbo.MySiteZones
Columns:
Id (PK, bigint, not null)
TenantId (int, not null)
Name (nvarchar(255), not null)
Caption (nvarchar(600), null)
IsActive (bit, not null)
IsDeleted (bit, not null)
DeleterUserId (bigint, null)
DeletionTime (datetime, null)
LastModificationTime (datetime, null)
LastModifierUserId (bigint, null)
CreationTime (datetime, not null)
CreatorUserId (bigint, null)
PolygonCoordinates (nvarchar(max), null)
HexColor (nvarchar(12), null)
PolygonCoordinatesReversed (nvarchar(max), null)

2 个答案:

答案 0 :(得分:2)

您无法将object直接投射到long。你应该使用Convert.ToInt64()甚至可以投射一个对象值:

string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(Convert.ToInt64(obj));

P.S:为什么不将'obj'声明为long(并相应更改名称:) :):

long obj;
// ... 
string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(obj);

编辑更新@Jonno

提到的错误的库调用

答案 1 :(得分:0)

只要obj是对int的引用,您就可以在转换为int之前尝试显式转换为long.GetTenantCoordinates((long)(int)obj)

我认为您可以将obj声明为intlong