How can I pass object from WEB API CORE 2.0 to Angular 7?












-1















How can I pass object from WEB API CORE 2.0 to Angular 7?
Please give me example.
I got object = null in Angular 7 and don't understand why.



Web Api Controller



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public JsonResult ListArtists()
{
var artists = _context.Artists.ToList();
return Json(new { results = artists });
}


POCO Classes



public class Artist
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id
{
get; set;
}
public FakeData fake { get; set; }
}
public class FakeData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string name { get; set; }
}


Angular models



export class Artist {
id: number;
fake: FakeData;
}
export class FakeData {
id: number;
name: string;
}


Getting data in angular using observable



getArtistsList(): Observable<Artist> { 
return this.http.get<Artist>(this.myAppUrl + 'api/ArtistsLibrary/ListArtists');
}


component



artists_count: number;
artists: Artist;
ngOnInit() {
this.form = this.fb.group({ searchText: [null, null] });
this.LoadData();
}
LoadData() {
console.log("LoadData");
console.log("checkedList=" + this.checkedList);
const genre_storage = sessionStorage.getItem("genre");
debugger;
this._artistsService
.getArtistsList()
.pipe()
.subscribe((artists: Artist) => {
this.artists = artists;
this.artists_count = artists.length;
});
}









share|improve this question

























  • Pls add relevant code here

    – User3250
    Jan 1 at 10:44











  • Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

    – Tao Zhou
    Jan 2 at 3:09
















-1















How can I pass object from WEB API CORE 2.0 to Angular 7?
Please give me example.
I got object = null in Angular 7 and don't understand why.



Web Api Controller



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public JsonResult ListArtists()
{
var artists = _context.Artists.ToList();
return Json(new { results = artists });
}


POCO Classes



public class Artist
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id
{
get; set;
}
public FakeData fake { get; set; }
}
public class FakeData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string name { get; set; }
}


Angular models



export class Artist {
id: number;
fake: FakeData;
}
export class FakeData {
id: number;
name: string;
}


Getting data in angular using observable



getArtistsList(): Observable<Artist> { 
return this.http.get<Artist>(this.myAppUrl + 'api/ArtistsLibrary/ListArtists');
}


component



artists_count: number;
artists: Artist;
ngOnInit() {
this.form = this.fb.group({ searchText: [null, null] });
this.LoadData();
}
LoadData() {
console.log("LoadData");
console.log("checkedList=" + this.checkedList);
const genre_storage = sessionStorage.getItem("genre");
debugger;
this._artistsService
.getArtistsList()
.pipe()
.subscribe((artists: Artist) => {
this.artists = artists;
this.artists_count = artists.length;
});
}









share|improve this question

























  • Pls add relevant code here

    – User3250
    Jan 1 at 10:44











  • Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

    – Tao Zhou
    Jan 2 at 3:09














-1












-1








-1








How can I pass object from WEB API CORE 2.0 to Angular 7?
Please give me example.
I got object = null in Angular 7 and don't understand why.



Web Api Controller



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public JsonResult ListArtists()
{
var artists = _context.Artists.ToList();
return Json(new { results = artists });
}


POCO Classes



public class Artist
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id
{
get; set;
}
public FakeData fake { get; set; }
}
public class FakeData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string name { get; set; }
}


Angular models



export class Artist {
id: number;
fake: FakeData;
}
export class FakeData {
id: number;
name: string;
}


Getting data in angular using observable



getArtistsList(): Observable<Artist> { 
return this.http.get<Artist>(this.myAppUrl + 'api/ArtistsLibrary/ListArtists');
}


component



artists_count: number;
artists: Artist;
ngOnInit() {
this.form = this.fb.group({ searchText: [null, null] });
this.LoadData();
}
LoadData() {
console.log("LoadData");
console.log("checkedList=" + this.checkedList);
const genre_storage = sessionStorage.getItem("genre");
debugger;
this._artistsService
.getArtistsList()
.pipe()
.subscribe((artists: Artist) => {
this.artists = artists;
this.artists_count = artists.length;
});
}









share|improve this question
















How can I pass object from WEB API CORE 2.0 to Angular 7?
Please give me example.
I got object = null in Angular 7 and don't understand why.



Web Api Controller



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public JsonResult ListArtists()
{
var artists = _context.Artists.ToList();
return Json(new { results = artists });
}


POCO Classes



public class Artist
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id
{
get; set;
}
public FakeData fake { get; set; }
}
public class FakeData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string name { get; set; }
}


Angular models



export class Artist {
id: number;
fake: FakeData;
}
export class FakeData {
id: number;
name: string;
}


Getting data in angular using observable



getArtistsList(): Observable<Artist> { 
return this.http.get<Artist>(this.myAppUrl + 'api/ArtistsLibrary/ListArtists');
}


component



artists_count: number;
artists: Artist;
ngOnInit() {
this.form = this.fb.group({ searchText: [null, null] });
this.LoadData();
}
LoadData() {
console.log("LoadData");
console.log("checkedList=" + this.checkedList);
const genre_storage = sessionStorage.getItem("genre");
debugger;
this._artistsService
.getArtistsList()
.pipe()
.subscribe((artists: Artist) => {
this.artists = artists;
this.artists_count = artists.length;
});
}






angular asp.net-core-webapi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:18









t-prisar

7781719




7781719










asked Jan 1 at 10:34









user3161827user3161827

61




61













  • Pls add relevant code here

    – User3250
    Jan 1 at 10:44











  • Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

    – Tao Zhou
    Jan 2 at 3:09



















  • Pls add relevant code here

    – User3250
    Jan 1 at 10:44











  • Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

    – Tao Zhou
    Jan 2 at 3:09

















Pls add relevant code here

– User3250
Jan 1 at 10:44





Pls add relevant code here

– User3250
Jan 1 at 10:44













Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

– Tao Zhou
Jan 2 at 3:09





Possible duplicate of Don't see object values from ASP.NET Core MVC in Angular 7 http get

– Tao Zhou
Jan 2 at 3:09












1 Answer
1






active

oldest

votes


















-1














You can create a method in webapi controller to pass the object after serializing into json format.
In angular, you can call that route using angular service to get that json. The json can be mapped into your required javascript object in angular.



This way you are able to pass C# object from asp.net core into javascript object in angular.



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public IActionResult ListArtists()
{
var artists = _context.Artists.ToList();
return Ok(artists);
}


Try checking if the url endpoint is giving you json data. It is a parsing problem.






share|improve this answer


























  • That's what I did(I attached my code). But got object = null in Angular 7.

    – user3161827
    Jan 1 at 12:46











  • I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

    – t-prisar
    Jan 1 at 17:31











  • I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

    – user3161827
    Jan 1 at 19:44











  • Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

    – user3161827
    Jan 1 at 19:56











  • How u subscribed?

    – t-prisar
    Jan 2 at 4:13











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',
autoActivateHeartbeat: false,
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%2f53994766%2fhow-can-i-pass-object-from-web-api-core-2-0-to-angular-7%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









-1














You can create a method in webapi controller to pass the object after serializing into json format.
In angular, you can call that route using angular service to get that json. The json can be mapped into your required javascript object in angular.



This way you are able to pass C# object from asp.net core into javascript object in angular.



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public IActionResult ListArtists()
{
var artists = _context.Artists.ToList();
return Ok(artists);
}


Try checking if the url endpoint is giving you json data. It is a parsing problem.






share|improve this answer


























  • That's what I did(I attached my code). But got object = null in Angular 7.

    – user3161827
    Jan 1 at 12:46











  • I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

    – t-prisar
    Jan 1 at 17:31











  • I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

    – user3161827
    Jan 1 at 19:44











  • Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

    – user3161827
    Jan 1 at 19:56











  • How u subscribed?

    – t-prisar
    Jan 2 at 4:13
















-1














You can create a method in webapi controller to pass the object after serializing into json format.
In angular, you can call that route using angular service to get that json. The json can be mapped into your required javascript object in angular.



This way you are able to pass C# object from asp.net core into javascript object in angular.



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public IActionResult ListArtists()
{
var artists = _context.Artists.ToList();
return Ok(artists);
}


Try checking if the url endpoint is giving you json data. It is a parsing problem.






share|improve this answer


























  • That's what I did(I attached my code). But got object = null in Angular 7.

    – user3161827
    Jan 1 at 12:46











  • I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

    – t-prisar
    Jan 1 at 17:31











  • I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

    – user3161827
    Jan 1 at 19:44











  • Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

    – user3161827
    Jan 1 at 19:56











  • How u subscribed?

    – t-prisar
    Jan 2 at 4:13














-1












-1








-1







You can create a method in webapi controller to pass the object after serializing into json format.
In angular, you can call that route using angular service to get that json. The json can be mapped into your required javascript object in angular.



This way you are able to pass C# object from asp.net core into javascript object in angular.



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public IActionResult ListArtists()
{
var artists = _context.Artists.ToList();
return Ok(artists);
}


Try checking if the url endpoint is giving you json data. It is a parsing problem.






share|improve this answer















You can create a method in webapi controller to pass the object after serializing into json format.
In angular, you can call that route using angular service to get that json. The json can be mapped into your required javascript object in angular.



This way you are able to pass C# object from asp.net core into javascript object in angular.



[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public IActionResult ListArtists()
{
var artists = _context.Artists.ToList();
return Ok(artists);
}


Try checking if the url endpoint is giving you json data. It is a parsing problem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 17:29

























answered Jan 1 at 11:37









t-prisart-prisar

7781719




7781719













  • That's what I did(I attached my code). But got object = null in Angular 7.

    – user3161827
    Jan 1 at 12:46











  • I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

    – t-prisar
    Jan 1 at 17:31











  • I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

    – user3161827
    Jan 1 at 19:44











  • Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

    – user3161827
    Jan 1 at 19:56











  • How u subscribed?

    – t-prisar
    Jan 2 at 4:13



















  • That's what I did(I attached my code). But got object = null in Angular 7.

    – user3161827
    Jan 1 at 12:46











  • I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

    – t-prisar
    Jan 1 at 17:31











  • I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

    – user3161827
    Jan 1 at 19:44











  • Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

    – user3161827
    Jan 1 at 19:56











  • How u subscribed?

    – t-prisar
    Jan 2 at 4:13

















That's what I did(I attached my code). But got object = null in Angular 7.

– user3161827
Jan 1 at 12:46





That's what I did(I attached my code). But got object = null in Angular 7.

– user3161827
Jan 1 at 12:46













I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

– t-prisar
Jan 1 at 17:31





I have changed my answer. Try calling the url from browser or postman. If it gives the data check the format it is giving. Since you are putting array of Artist,your json result should be enclosed with .

– t-prisar
Jan 1 at 17:31













I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

– user3161827
Jan 1 at 19:44





I tried. IActionResult and JsonResult both correct and I have no problem with webapi.

– user3161827
Jan 1 at 19:44













Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

– user3161827
Jan 1 at 19:56





Postman is correct for both IActionresult snd Jsonresult. Problem that Angular returns object Fakedata = null.

– user3161827
Jan 1 at 19:56













How u subscribed?

– t-prisar
Jan 2 at 4:13





How u subscribed?

– t-prisar
Jan 2 at 4:13




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994766%2fhow-can-i-pass-object-from-web-api-core-2-0-to-angular-7%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

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith