Home > Net >  How the.net 5.0 WEBAPI GET return Json? [for]
How the.net 5.0 WEBAPI GET return Json? [for]

Time:02-24

Is a very simple example, but how also won't return JSON,
Environment: the.net 5.0 webapi

First to Startup file

Public void ConfigureServices (IServiceCollection services)
{
services.AddControllers();
}

Public void the Configure (IApplicationBuilder app, IWebHostEnvironment env)
{
If (env. IsDevelopment ())
{
App. UseDeveloperExceptionPage ();
}
App. UseHttpsRedirection ();
App. UseDefaultFiles ();
App. UseRouting ();
App. UseStaticFiles ();

App. UseEndpoints (endpoints=& gt;
{
Endpoints. MapControllers ();

});
}


And then the controller
[HttpGet]
Public stringGet ()
{
Return XXX. ToJson (); Here in directing a conversion of data, debugging is normal json structure ok
}

But running F12 found out is the text of the content-type is text/plan

Then add [Produces the controller (" application/json ")], was transformed

But json is received after forced to escape the json, can use


[HttpGet]
Public JsonResult the Get ()
{

Return new JsonResult (xxxObject);//xxxObject class object is the original
}


Try to find is indeed a json, but the json object is empty, only a large number, and {}

Then try to return objects directly

[HttpGet]
Public xxxObjectGet ()
{

Return xxxObject;//xxxObject class object is the original
}

Find it empty and JsonResult, json {}

Seek advice on how to use the.net 5 to create a simple get request, can return the json,

Thank you, I feel so can achieve a simple function, some depressed,

On the Internet looking for example is essentially a few years ago WEB Form, with no

Thank you,

CodePudding user response:

Try

 

[httpGet]
Public dynamic Test ()
{
Return new {id=1};
}



CodePudding user response:

 public string the Get () 
{
Return JsonConvert. SerializeObject (result);
}
//receipt of each other, JsonConvert. DeserializeObject (response);
//if is js, JSON. Parse (data)

//or
Public XXObject the Get ()
{
//...
Return xxobject;
}

CodePudding user response:

reference 1/f, nan peony reply:
try

 

[httpGet]
Public dynamic Test ()
{
Return new {id=1};
}




No, and with the object is the same, get json is an empty {}

CodePudding user response:

refer to the second floor X - I - n reply:
 public string the Get () 
{
Return JsonConvert. SerializeObject (result);
}
//receipt of each other, JsonConvert. DeserializeObject (response);
//if is js, JSON. Parse (data)

//or
Public XXObject the Get ()
{
//...
Return xxobject;
}


Method 1, it is certainly possible, but I don't want to escape the standard json

Method 2 articles I have not tried,

CodePudding user response:

This also tried,
 
Public HttpResponseMessage the Get ()
{

HttpResponseMessage httpReponseMessage=new HttpResponseMessage ();
String jsonstr="{id: 1, name=123}";
HttpReponseMessage. The Content=new StringContent (jsonstr);
Return httpReponseMessage;
}

Also not line, returns the

 {" version ": {" major" : 1, "minor" : 1, "build" : 1, "revision" : 1, "majorRevision" : 1, "minorRevision" : 1}, "content" : {" headers ": [{" key" : "the content-type", "value" : [" text/plain; charset=utf-8 "]}]}, "statusCode" : 200, "reasonPhrase" : "OK", "headers" : [], "trailingHeaders" : [], "requestMessage:" null, "isSuccessStatusCode" : true} 

CodePudding user response:

reference fxyc87 reply: 3/f
Quote: refer to 1st floor nan peony reply:
try

 

[httpGet]
Public dynamic Test ()
{
Return new {id=1};
}




No, and with the object is the same, get json is an empty {}



Not line? Why not? Why I again did you not, how don't you tell me about what other people know how to not

reference fxyc87 reply: 3/f
Quote: refer to 1st floor nan peony reply:
try

 

[httpGet]
Public dynamic Test ()
{
Return new {id=1};
}




No, and with the object is the same, get json is an empty {}






reference fxyc87 reply: 3/f
Quote: refer to 1st floor nan peony reply:
try

 

[httpGet]
Public dynamic Test ()
{
Return new {id=1};
}




No, and with the object is the same, get json is an empty {}


API routing have set up? The way in? Debugging a break of what, see method performs no, clearly tell you that this method is no problem, let's think about other problems, don't squeeze just look at the official template API implementation, to create a new project with webapi, inside an example can be reference for you

CodePudding user response:

I think go to the website to check is better than ask in this, after all, more than in 3.1, ha ha,
  • Related