在 App_Start 必須要有 WebApiConfig.cs:
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Web.Http; namespace ProjectName { public static class WebApiConfig { public static void Register(HttpConfiguration config) { var settings = config.Formatters.JsonFormatter.SerializerSettings; settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); settings.Formatting = Formatting.Indented; config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }以上 highlight 的地方把 .net 輸出的所有 json 檔案的 property 全部變成 Camel case,反過來收到的 json 也會從 Camel case 變成 Pscal case。另外,在 Global.asax 必須要有
namespace ProjectName { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
No comments:
Post a Comment