How to return a Json from a .Net Core Web API?











up vote
0
down vote

favorite












This is a basic question. I am new to ASP.Net Core so I created a .Net Core Web API project using the template in Visual Studio 2017 and I would like to know how to return a Json string from the Get() function.



The Get() function provided.



    [HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}


I would like to know how to change so it returns a Json string of int variable like the following.



    // GET: api/MOER
[HttpGet]
public <<some return type>> Get()
{
_MOER = 32;

return <<return a Json result/string of _MOER>>;
}


I am have seen the Nuget package Newtonsoft.Json where you serialize/deserialize but I am not sure if its applicable any more with .Net Core.



I have also seen examples where they use JsonResult but when I try to use this approach, the compiler doesn't know what Json() is.



    [HttpGet]
public JsonResult Get()
{
_MOER = 32;

return Json(_MOER);
}


Thank you for your help!










share|improve this question






















  • It seems that you're using the [ApiController], why not simply return an IActionResult type?
    – itminus
    Nov 20 at 1:40

















up vote
0
down vote

favorite












This is a basic question. I am new to ASP.Net Core so I created a .Net Core Web API project using the template in Visual Studio 2017 and I would like to know how to return a Json string from the Get() function.



The Get() function provided.



    [HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}


I would like to know how to change so it returns a Json string of int variable like the following.



    // GET: api/MOER
[HttpGet]
public <<some return type>> Get()
{
_MOER = 32;

return <<return a Json result/string of _MOER>>;
}


I am have seen the Nuget package Newtonsoft.Json where you serialize/deserialize but I am not sure if its applicable any more with .Net Core.



I have also seen examples where they use JsonResult but when I try to use this approach, the compiler doesn't know what Json() is.



    [HttpGet]
public JsonResult Get()
{
_MOER = 32;

return Json(_MOER);
}


Thank you for your help!










share|improve this question






















  • It seems that you're using the [ApiController], why not simply return an IActionResult type?
    – itminus
    Nov 20 at 1:40















up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is a basic question. I am new to ASP.Net Core so I created a .Net Core Web API project using the template in Visual Studio 2017 and I would like to know how to return a Json string from the Get() function.



The Get() function provided.



    [HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}


I would like to know how to change so it returns a Json string of int variable like the following.



    // GET: api/MOER
[HttpGet]
public <<some return type>> Get()
{
_MOER = 32;

return <<return a Json result/string of _MOER>>;
}


I am have seen the Nuget package Newtonsoft.Json where you serialize/deserialize but I am not sure if its applicable any more with .Net Core.



I have also seen examples where they use JsonResult but when I try to use this approach, the compiler doesn't know what Json() is.



    [HttpGet]
public JsonResult Get()
{
_MOER = 32;

return Json(_MOER);
}


Thank you for your help!










share|improve this question













This is a basic question. I am new to ASP.Net Core so I created a .Net Core Web API project using the template in Visual Studio 2017 and I would like to know how to return a Json string from the Get() function.



The Get() function provided.



    [HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}


I would like to know how to change so it returns a Json string of int variable like the following.



    // GET: api/MOER
[HttpGet]
public <<some return type>> Get()
{
_MOER = 32;

return <<return a Json result/string of _MOER>>;
}


I am have seen the Nuget package Newtonsoft.Json where you serialize/deserialize but I am not sure if its applicable any more with .Net Core.



I have also seen examples where they use JsonResult but when I try to use this approach, the compiler doesn't know what Json() is.



    [HttpGet]
public JsonResult Get()
{
_MOER = 32;

return Json(_MOER);
}


Thank you for your help!







c# json.net asp.net-core-webapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:44









PKonstant

11019




11019












  • It seems that you're using the [ApiController], why not simply return an IActionResult type?
    – itminus
    Nov 20 at 1:40




















  • It seems that you're using the [ApiController], why not simply return an IActionResult type?
    – itminus
    Nov 20 at 1:40


















It seems that you're using the [ApiController], why not simply return an IActionResult type?
– itminus
Nov 20 at 1:40






It seems that you're using the [ApiController], why not simply return an IActionResult type?
– itminus
Nov 20 at 1:40














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Add this attribute to your controller class:



[Produces("application/json")]


So it becomes:



[Produces("application/json")]
public class YourController: Controller {

[HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}
}


That should be enough, otherwise I believe the default is XML (unless the client explicitly asks for JSON using the Accept HTTP header).






share|improve this answer





















  • Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
    – PKonstant
    Nov 19 at 22:59










  • Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
    – HaukurHaf
    Nov 19 at 23:06










  • Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
    – PKonstant
    Nov 19 at 23:12












  • It will probably yield the same result as the built in json serialization ....
    – HaukurHaf
    Nov 19 at 23:18










  • oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
    – PKonstant
    Nov 19 at 23:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383719%2fhow-to-return-a-json-from-a-net-core-web-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Add this attribute to your controller class:



[Produces("application/json")]


So it becomes:



[Produces("application/json")]
public class YourController: Controller {

[HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}
}


That should be enough, otherwise I believe the default is XML (unless the client explicitly asks for JSON using the Accept HTTP header).






share|improve this answer





















  • Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
    – PKonstant
    Nov 19 at 22:59










  • Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
    – HaukurHaf
    Nov 19 at 23:06










  • Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
    – PKonstant
    Nov 19 at 23:12












  • It will probably yield the same result as the built in json serialization ....
    – HaukurHaf
    Nov 19 at 23:18










  • oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
    – PKonstant
    Nov 19 at 23:29















up vote
1
down vote



accepted










Add this attribute to your controller class:



[Produces("application/json")]


So it becomes:



[Produces("application/json")]
public class YourController: Controller {

[HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}
}


That should be enough, otherwise I believe the default is XML (unless the client explicitly asks for JSON using the Accept HTTP header).






share|improve this answer





















  • Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
    – PKonstant
    Nov 19 at 22:59










  • Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
    – HaukurHaf
    Nov 19 at 23:06










  • Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
    – PKonstant
    Nov 19 at 23:12












  • It will probably yield the same result as the built in json serialization ....
    – HaukurHaf
    Nov 19 at 23:18










  • oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
    – PKonstant
    Nov 19 at 23:29













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Add this attribute to your controller class:



[Produces("application/json")]


So it becomes:



[Produces("application/json")]
public class YourController: Controller {

[HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}
}


That should be enough, otherwise I believe the default is XML (unless the client explicitly asks for JSON using the Accept HTTP header).






share|improve this answer












Add this attribute to your controller class:



[Produces("application/json")]


So it becomes:



[Produces("application/json")]
public class YourController: Controller {

[HttpGet]
public IEnumerable<string> Get()
{
return new string { "value1", "value2" };
}
}


That should be enough, otherwise I believe the default is XML (unless the client explicitly asks for JSON using the Accept HTTP header).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 22:55









HaukurHaf

9,67352845




9,67352845












  • Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
    – PKonstant
    Nov 19 at 22:59










  • Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
    – HaukurHaf
    Nov 19 at 23:06










  • Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
    – PKonstant
    Nov 19 at 23:12












  • It will probably yield the same result as the built in json serialization ....
    – HaukurHaf
    Nov 19 at 23:18










  • oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
    – PKonstant
    Nov 19 at 23:29


















  • Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
    – PKonstant
    Nov 19 at 22:59










  • Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
    – HaukurHaf
    Nov 19 at 23:06










  • Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
    – PKonstant
    Nov 19 at 23:12












  • It will probably yield the same result as the built in json serialization ....
    – HaukurHaf
    Nov 19 at 23:18










  • oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
    – PKonstant
    Nov 19 at 23:29
















Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
– PKonstant
Nov 19 at 22:59




Ok but I would like to know how to return a Json string so it returns something like { "key":"value"}
– PKonstant
Nov 19 at 22:59












Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
– HaukurHaf
Nov 19 at 23:06




Then you need to change your method so it returns a key/value type, like a Dictionary<string, string>. A list of strings will just be serialized to a plain string array.
– HaukurHaf
Nov 19 at 23:06












Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
– PKonstant
Nov 19 at 23:12






Can't I just use the Newtonsof.Json's Serialize/Deserialize for returning a class? (So you know, I'm not interested returning "value1", "value2")
– PKonstant
Nov 19 at 23:12














It will probably yield the same result as the built in json serialization ....
– HaukurHaf
Nov 19 at 23:18




It will probably yield the same result as the built in json serialization ....
– HaukurHaf
Nov 19 at 23:18












oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
– PKonstant
Nov 19 at 23:29




oh, so you are saying if I return a Dictionary, then it will return as a json string. So does that mean everything returned by a Get are always in json string?
– PKonstant
Nov 19 at 23:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383719%2fhow-to-return-a-json-from-a-net-core-web-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin