[英]Invalid Cast Exception - cast is not valid
This is the error on my website. 这是我网站上的错误。 No error in Visual Studio, it compiles just fine.
在Visual Studio中没有错误,它可以正常编译。 This happens when I run the website locally and click the button to show the zones in Chrome or any other browser.
当我在本地运行网站并单击按钮以在Chrome或任何其他浏览器中显示区域时,会发生这种情况。 I am completely stumped by this and I am looking for any help??
我对此完全感到困惑,我在寻找任何帮助吗? Greatly appreciated.
不胜感激。
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()
This is the method I am calling which shows line 125: 这是我正在调用的方法,它显示第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 }
My table layout is like this: 我的表布局是这样的:
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)
You cannot cast an object
directly to long
. 您不能将
object
直接投射为long
。 You should use Convert.ToInt64()
which even casts an object value: 您应该使用
Convert.ToInt64()
甚至转换一个对象值:
string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(Convert.ToInt64(obj));
PS: Why don't you declare 'obj' as long
(and change the name accordingly :) ): PS:为什么不声明'obj'
long
(并相应地更改名称:)):
long obj;
// ...
string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(obj);
EDIT Update wrong library call as mentioned by @Jonno 编辑更新错误的库调用,如@Jonno所述
As long as obj
is a reference to int
you can try explicit cast to int
right before casting to long
: .GetTenantCoordinates((long)(int)obj)
只要
obj
是对int
的引用,您就可以在强制转换为long
之前尝试将显式强制转换为int
: .GetTenantCoordinates((long)(int)obj)
I think however you can declare obj
as int
or long
. 我认为您可以将
obj
声明为int
或long
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.