Error : The given key was not present in the dictionary in C# Asp.Net Core

Dung Do Tien Apr 08 2021 270

Hi, I created a project using Asp.Net Core to display some articles. I have created an enum to define category as below:

public enum ArticlesType
{
    Normal = 1, 
    CarCompare = 2, 
    Tips = 3, 
    NewCar = 4, 
    Gallery = 5,
    Stories = 6,
    Promos = 7, 
    Cluster = 8
}

And below is my action controller to display list articles:

[HttpGet]
public IHttpActionResult Get(short articletype = (short)ArticlesType.Normal, short displaytype = 0)
{
    //......
    string titlePage = articletype > 0 ? ArticlesHelper.ArticlesTypeName[articletype] : ""; 
   //.......
}

but when localhost:1987/article/8/0 running app and call to action Get I got an error: The given key was not present in the dictionary.

{"Message":"An error has occurred.","ExceptionMessage":"The given key was not present in the dictionary.","ExceptionType":"System.Collections.Generic.KeyNotFoundException","StackTrace":"   
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n   at DVG.APA.CMSApi.Controllers.ArticlesController.Get(Int16 articletype, Int16 displaytype)\r\n   
at lambda_method(Closure , Object , Object[] )\r\n   
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters)\r\n   
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- 
End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- 
End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   
at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"}

How can I resolve it?

Have 1 answer(s) found.
  • P

    Palakorn Sangketchon Apr 08 2021

    Oh, This error throws when you get an item from a list but that item not existed in the list. So in ArticlesHelper.ArticlesTypeName list has to content ArticlesType equal 8.

    For example:

    Dictionary<String, String> test = new Dictionary<String,String>();
    test.Add("Key1","Value1");
    string error = test["Key2"]; // this line will throw error he given key was not present in the dictionary 

    I hope it useful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close