[ASP.net MVC] 設定Web API傳回JSON 時使用特定格式

預設ASP.net MVC 中跟回property name, 但frontend 若有不同格式, 則會在migration 時會出現不能正常deserialize 的問題. 若要解決問題的話, 則須要自己設定.

在global.asax.cs 中, 的application_start()中, 加入以下代碼

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

便可以deserialze 作camel case.

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.