2017-01-14 27 views
1

は、私が最初にIndexを呼び出すと、私はASP.NETコアViewComponent呼び出し:ここでInvalidCastExceptionが

私は

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); 
    } 
} 

として

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

とコントローラとしてビューコンポーネントを持ってきたものですアクションはすべて良いです。期待通りに動作しますが、私は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() 

でエラーが発生します。ビューコンポーネントのInvokeAsyncメソッドが呼び出される前に例外がスローされます。 私はしばらくこのことに固執しています。どんな助けでも本当に感謝します。おかげさまで

答えて

0

ここでエラーメッセージが表示されています。逆シリアル化された辞書の実際のobjectInt64であり、ViewComponentが期待する引数はInt32(C#ではintとも呼ばれます)です。

+0

デシリアライズメソッドの動作を変更して、デフォルトで数値をInt64に変更せず、代わりにint32を使用することはできますか? –

+0

'DeserializeObject >(...)'の 'object'を' int'に変更しようとしましたか? – Meligy

関連する問題