angular 4 + WebApi

0

Good morning,

I'm trying to show the objects obtained in the get request from the browser's console from angular test.service.ts:

   getTestValues_Get() :Observable<Itest []>{
  return this._http.get(this.url)
        .map(response => <Itest[]> response.json());  }

the call from the test.component.ts component:

ngOnInit() {             
         this._TestService.getSettings_Get()
         .subscribe(( test: ITest []) => {
              this.test= test;
             console.log("Array ->:" + this.test)
         });}

From the WebApi returns this in ValuesController.cs.

   [HttpGet]
    public Setting Get()
    {
        using (Entities EntityDB= new Entities())
        {
           return EntityDB.Settings.FirstOrDefault(e=>e.TestId == 1);
        }
    }

When I run it I get this error:

  

The 'ObjectContent'1' type failed to serialize the response body for   content type 'application / json

I have also added in WebApiConfig.cs:

        config.Formatters.Remove(config.Formatters.XmlFormatter);
        config.Formatters.Add(config.Formatters.JsonFormatter);

        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

Any idea where the problem might be?

Greetings,

    
asked by Kram_ 01.09.2017 в 13:55
source

1 answer

0

Have you checked that this gives you back the correct object debugging?

[HttpGet]
public Setting Get()
{
    using (Entities EntityDB= new Entities())
    {
       return EntityDB.Settings.FirstOrDefault(e=>e.TestId == 1);
    }
}

if what you try is to return json by default, (remember that if you do not configure anything the api returns the format requested by the browser), it should only be enough to add this line.

config.Formatters.Remove(config.Formatters.XmlFormatter);
    
answered by 26.09.2017 в 21:37