2017-02-20 142 views
1

這是我網站上的錯誤。在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

不能直接施放objectlong。您應該使用Convert.ToInt64()甚至蒙上對象值:

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

PS:你爲什麼不宣佈 'OBJ' 爲long(並相應地更改名字:)):

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

編輯更新錯誤的庫調用,如@Jonno所述

+1

沒有'Convert.ToLong()' - 你可能是指'Convert.ToInt64()' - https://msdn.microsoft.com/en-us/library/0c5w856c(v=vs.110)。 aspx – Jonno

+0

你是對的......現在真的太晚了;)thx。更新 –

+0

非常感謝!那完全是它。我有一些其他代碼依賴於名稱,所以(Convert.ToInt64(obj));是答案!再次感謝! – zberg007

0

只要obj是對int的引用,您可以嘗試顯式轉換爲int ri鑄造long前GHT:.GetTenantCoordinates((long)(int)obj)

我想,但是你可以聲明objintlong