The name "Json" does not exist in the current context

Asked

Viewed 123 times

-4

Good afternoon, I have the following part of my class that would serve to feed the objects obtained through a json online.

public JsonResult GetJson()
    {
        string res;
        WebClient client = new WebClient();

        // Download string
        string value = client.DownloadString(http://localhost/info.json);

        // Write values
        res = value;
        dynamic dyn = JsonConvert.DeserializeObject(res);
        var lstInstagramObjects = new List<GitHubModel>();

        foreach (var obj in dyn.data)
        {
            lstInstagramObjects.Add(new GitHubModel()
            {
                Players_Total = int.Parse(obj.players_total.ToString()),
                Players_Online = int.Parse(obj.players_online.ToString()),
            });
        }

        return Json(lstInstagramObjects, JsonRequestBehavior.AllowGet);
    }

    public class GitHubModel
    {
        public List<GitHubObjects> data { get; set; }
        public dynamic ApiTunel { get; internal set; }
        public dynamic Players_Total { get; internal set; }
        public dynamic Players_Online { get; internal set; }
    }
    public class GitHubObjects
    {
        public string api_tunel { get; set; }
        public string nome_server { get; set; }
        public string nome_launcher { get; set; }
        public int server_status { get; set; }
        public int players_online { get; set; }
        public int players_total { get; set; }
        public string discord { get; set; }
        public string teamspeak { get; set; }
        public string server_ip { get; set; }
        public int server_port { get; set; }
        public int version { get; set; }
        public string update_link_zip { get; set; }
        public string tokovoip_link { get; set; }
    }
}

However I get the following visual error when I try to compile: inserir a descrição da imagem aqui

I ran the Stack and broke my head thinking what could be, but without success, PS: I have no experience with C# and I’m trying to give a familiar with the language.

  • This is ASP.NET Core?

  • I’m riding in C#

  • C# is the language. It seems that you are using a web framework (such as ASP.NET or ASP.NET Core) and it is probably from this framework that such a framework comes JsonResult or the method Json that you are trying to use. You need to know if you are using ASP.NET and which version you are using, without this it is impossible to help you.

  • Excuse the lack of information is that I’m really parachuting to try to learn C# even when I created the project what I remember was a project in the look that was like . NET Framework, how can I know which ASP.NET I am using? I only found . NET Framework 4.7.2

  • @Brendownf in Visual Studio, go to "Solution Explorer", right-click the desired project, click "Properties" and see the information in the "Application" tab (I don’t remember the menu items in English)

1 answer

0

For those who have problems like mine, the solution after turning the internet for me was, define the class where my function was as a controller, I had forgotten to define it.

public class Json: Controller
{
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.