How to get the response in JSON format

jagdish patil asked on January 4, 2020 11:50

I have installed the license version of kentico in my machine. Now I am calling the API using angular. I have set the authorization, content type and required headers through angular still I am getting the response in HTML format.

The url I am calling is: http://localhost:52659/Kentico12_4_DancingGoatMvc/en-US/Articles?format=json

Please tell me the actual format of api to get the response in JSON format.

Recent Answers


Trevor Fayas answered on January 5, 2020 05:30

You would return a JsonResult action, MVC doesn't magically return JSON for you just because you put "?format=json" in it, at this point it's up to you to route and return the proper results.

Something along the lines of:

public ActionResult Articles(string format)  
    {  
                var articles = GetArticles();  
        if(!string.IsNullOrEmpty(format) && format == "json") {
        return Json(articles, JsonRequestBehavior.AllowGet);  
        } else {
        // return view?
        return View(articles);
        }
    } 
1 votesVote for this answer Mark as a Correct answer

jagdish patil answered on January 6, 2020 06:46 (last edited on January 6, 2020 06:47)

Hi @Trevor, Thank you for your reply but I am using Angular to call the API so your solution will not work. I need the exact endpoint through which I can get the response in JSON format.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 6, 2020 08:43

HAve you considered using the REST Service to get the data in desired format and using the REST API Calls?

1 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on January 6, 2020 10:09

Jagdish, Trevor is right, MVC controller by default returns HTML and there is no specific URL format to make it return json.

There are 3 options available for you:

  • adjust MVC controller/method to return json
  • implement Web API endpoint
  • use Kentico out of the box REST service like Juraj suggested
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.