c# - Cant get ASP.NET MVC 6 Controller to return JSON -


i have mvc 6 project in using fiddler test out web api. if take following controller action uses entityframework 7 return list. html render fine.

[httpget("/")] public iactionresult index() {     var model = orderrepository.getall();      return view(model); } 

but when try return json response instead 502 error.

[httpget("/")] public jsonresult index() {     var model = orderrepository.getall();      return json(model); } 

any idea on why object isnt serialized json correctly?

first of can use ienumerable<order> or ienumerable<object> return type instead of jsonresult , return orderrepository.getall(). recommend read the article fr additional information.

about error bad gateway. try add newtonsoft.json in latest version 8.0.2 dependencies in package.json , use use

services.addmvc()     .addjsonoptions(options => {         options.serializersettings.referenceloophandling =             newtonsoft.json.referenceloophandling.ignore;     }); 

by way 1 can reproduce error "http error 502.3 - bad gateway", describes if set breakpoint on return statement of working code , wait long enough. see error "http error 502.3 - bad gateway" on many common errors.

you can consider more helpful serialization options. example

services.addmvc()     .addjsonoptions(options => {         // handle loops correctly         options.serializersettings.referenceloophandling =             newtonsoft.json.referenceloophandling.ignore;          // use standard name conversion of properties         options.serializersettings.contractresolver =             new camelcasepropertynamescontractresolver();          // include $id property in output         options.serializersettings.preservereferenceshandling =             preservereferenceshandling.objects;     }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -