ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current context

C#asp.netJsonasp.net CoreVisual Studio-2015

C# Problem Overview


In my ASP.NET Core (.NET Framework) project, I'm getting above error on my following Controller Action method. What I may be missing? Or, are there any work arounds?:

public class ClientController : Controller
{
    public ActionResult CountryLookup()
    {
        var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };
        
        return Json(countries, JsonRequestBehavior.AllowGet);
    }
}

UPDATE:

Please note folowing comments from @NateBarbettini below:

  1. JsonRequestBehavior has been deprecated in ASP.NET Core 1.0.
  2. In accepted response from @Miguel below, the return type of action method does not specifically need to be of type JsonResult. ActionResult or IActionResult works too.

C# Solutions


Solution 1 - C#

Returning Json-formatted data:

public class ClientController : Controller
{
    public JsonResult CountryLookup()
    {
         var countries = new List<SearchTypeAheadEntity>
         {
             new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
             new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
         };

         return Json(countries);
    }
}

Solution 2 - C#

In Code it's replace To JsonRequestBehavior.AllowGet with new Newtonsoft.Json.JsonSerializerSettings()

It's Work same as JsonRequestBehavior.AllowGet

public class ClientController : Controller
{
  public ActionResult CountryLookup()
  {
    var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };

    return Json(countries, new Newtonsoft.Json.JsonSerializerSettings());
  }
}

Solution 3 - C#

Some times you need to return a message back in JSON, simply use the JSON result as below, no need for jsonrequestbehavior any more, below simple code to use:

public ActionResult DeleteSelected([FromBody]List<string> ids)
{
    try
    {
        if (ids != null && ids.Count > 0)
        {
            foreach (var id in ids)
            {
                bool done = new tblCodesVM().Delete(Convert.ToInt32(id));
                
            }
            return Json(new { success = true, responseText = "Deleted Scussefully" });

        }
        return Json(new { success = false, responseText = "Nothing Selected" });
    }
    catch (Exception dex)
    {
        
        return Json(new { success = false, responseText = dex.Message });
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnamView Question on Stackoverflow
Solution 1 - C#Miguel Torres CView Answer on Stackoverflow
Solution 2 - C#Ravi MakwanaView Answer on Stackoverflow
Solution 3 - C#AliView Answer on Stackoverflow