简体   繁体   English

ASP.NET Core ViewComponent调用:InvalidCastException

[英]ASP.NET Core ViewComponent Invocation: InvalidCastException

Here is what I have 这就是我所拥有的

I have a view component as 我有一个视图组件

public class TestViewComponent : ViewComponent
{
     public async Task<IViewComponentResult> InvokeAsync(int param1, int param2)
     {
         return Content(param1.ToString() + param2);
     }
}

And a controller as 和控制器一样

public class HomeController : Controller
{
     private Dictionary<string, object> _dict = new Dictionary<string, object>();

     public IActionResult Index()
     {
         _dict = new Dictionary<string, object>() { ["param1"] = 100, ["param2"] = 200 };
         return ViewComponent("Test", _dict)
     }

     public IActionResult Index1()
     {
         var obj = new { param1 = 100, param2 = 200 };
         var objString = JsonConvert.SerializeObject(obj);
         _dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(objString);
         return ViewComponent("Test", _dict);
     }
 }

When I call the first Index action everything is good. 当我打电话给第一个Index动作时,一切都很好。 It works as expected but when I call the Index1 action I get an error with the following stack trace 它按预期工作,但当我调用Index1操作时,我得到一个错误,其中包含以下堆栈跟踪

System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.<InvokeAsyncCore>d__6.MoveNext()
--- 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 Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.<InvokeAsync>d__5.MoveNext()
--- 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 Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentHelper.<InvokeCoreAsync>d__12.MoveNext()
--- 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 Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewComponentResultExecutor.<ExecuteAsync>d__6.MoveNext()
--- 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 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultAsync>d__30.MoveNext()
--- 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 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResultFilterAsync>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()
--- 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 Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- 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 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__6.MoveNext()

It seems there is some issue after the Deserialization. 在反序列化之后似乎存在一些问题。 The exception is thrown even before the InvokeAsync method of the view component is called. 甚至在调用视图组件的InvokeAsync方法之前抛出异常。 I have been stuck on this for a while. 我被困在这一段时间了。 Any help will be really appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

The error message is pretty much telling what's happening here. 错误信息几乎可以告诉我们这里发生了什么。 The actual object s in the deserialized dictionary are Int64 s, which the arguments expected by the ViewComponent are Int32 (also called int in C#). 反序列化字典中的实际objectInt64 ,其中ViewComponent所期望的参数是Int32 (在C#中也称为int )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net Core 将 ViewComponent 渲染为变量 - Asp.Net Core render ViewComponent to variable ASP.NET Core使用ViewComponent跟踪侧栏的活动页面 - ASP.NET Core track active Page for sidebar using ViewComponent 在 ASP.NET CORE MVC 中使用 CRUD 操作的 ViewComponent - ViewComponent With CRUD Operations in ASP.NET CORE MVC 无法识别 ASP.NET Core ViewComponent 的 Invoke() 方法调用 - ASP.NET Core ViewComponent's Invoke() method call not recognized 如何在Asp.net核心中的ViewComponent之间共享ViewData - How share ViewData between ViewComponent in Asp.net core 使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent - ViewComponent not found in Asp.NET Core 2.2 using Razor Pages 如何将 ModelExpression 绑定到 ASP.NET Core 中的 ViewComponent? - How to bind a ModelExpression to a ViewComponent in ASP.NET Core? 如何在ViewComponent Asp.Net Core中获取当前登录用户 - How get current login user in ViewComponent Asp.Net Core ASP.NET CORE 存储过程错误:InvalidCastException - ASP.NET CORE stored procedure error : InvalidCastException ViewComponent 必须有一个名为“InvokeAsync”或“Invoke”的公共方法 - ASP.NET CORE (.NET 6) - ViewComponent must have one public method named 'InvokeAsync' or 'Invoke' - ASP.NET CORE (.NET 6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM