Error: C# The name 'Json' does not exist in the Current context

Asked

Viewed 651 times

2

I have the following error in C# 'The name 'Json' does not exist in the Current context' in my application. I followed all the recommended steps by various websites, such as adding such references and such, but the error continued.

using System;
using System.Web.Mvc;
using System.IO;
using DevExpress.XtraRichEdit;

namespace PCMSO
{
  public partial class pcmso : System.Web.UI.Page
  {
    [HttpPost]
    public JsonResult SalvaTexto(){
        try
        {
            return Json(new { success = true, message = "Salvo!. "}, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = "Não foi possível salvar o documento. " + ex.Message }, JsonRequestBehavior.AllowGet);
        }
    }
  }
}
  • what is the version of ASP.net / Framework?

  • has code inside the Try?

1 answer

2


Segui todos os passos recomendados por diversos sites, como adicionar tais referências e tal.

Will be?

In your code example, there are only the following namespaces:

using System;
using System.Web.Mvc;
using System.IO;
using DevExpress.XtraRichEdit;

None of them have a type called Json. The first three are authored by Microsoft teams and are well known. The room has documentation here: https://documentation.devexpress.com/WindowsForms/DevExpress.XtraRichEdit.namespace

You need to include the namespace which contains the Json type, or else call the type by its full name (i.e.: System.Web.Helpers.Json, although by signature, it does not seem to be the case).


There’s another possibility here, more likely. Maybe you were thinking about Json method of the Controller class. In this case, for your code to work, the class in which you make your implementation should inherit from System.Web.Mvc.Controller. Not the case in the code you posted.

  • I forgot an excerpt from the code... I switched System.Web.UI.Page to Controller and the problem with Json was solved, but now appears 'Parser Error Message: 'PCMSO.pcmso' is not allowed here because it does not extend the class 'System.Web.UI.Page'. when I run the application.

  • @lucaswmolin The controller class is one thing, the page class is another completely different. They’re as different as an octopus is from a bicycle. And apparently you’re trying to use something of both. Unfortunately there is no way... You need to review what you are doing, because you are mixing incompatible things.

Browser other questions tagged

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