Skip to content

Commit 1ddb7b0

Browse files
committed
Use camelcase for JSON in ASP.NET Core by default. Closes #330
1 parent 1f728d2 commit 1ddb7b0

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

site/jekyll/getting-started/tutorial.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ var CommentBox = React.createClass({
940940
```
941941

942942
## Optimization: Bundling and minification
943-
Bundling refers to the practice of combining multiple JavaScript files into a single large file to reduce the number of HTTP requests to load a page. Minification refers to the removal of comments and unnecessary whitespace from JavaScript files to make them smaller. Together, bundling and minification can help to significantly improve the performance of your website.
943+
Bundling refers to the practice of combining multiple JavaScript files into a single large file to reduce the number of HTTP requests to load a page. Minification refers to the removal of comments and unnecessary whitespace from JavaScript files to make them smaller. Together, bundling and minification can help to significantly improve the performance of your website.
944944

945945
There used to be a section on bundling and minification in this tutorial, but unfortunately the latest library being used by ASP.NET Core MVC ([BundlerMinifier](https://github.com/madskristensen/BundlerMinifier)) is not easily extensible, which makes it difficult to add JSX processing to it. For production use, it is currently recommended to use a tool like Gulp or [Webpack](/guides/webpack.html) to bundle and minify your JavaScript.
946946

@@ -1042,20 +1042,15 @@ public ActionResult Index()
10421042
}
10431043
```
10441044

1045-
We also need to modify `Startup.cs` to tell ReactJS.NET which JavaScript files it requires for the server-side rendering, and ensure it encodes properties as camelcase just like ASP.NET Core does with JSON responses (this [will be fixed with ReactJS.NET 3.0](https://github.com/reactjs/React.NET/issues/330)):
1045+
We also need to modify `Startup.cs` to tell ReactJS.NET which JavaScript files it requires for the server-side rendering:
10461046

10471047
```csharp{4-10}
10481048
// Initialise ReactJS.NET. Must be before static files.
10491049
app.UseReact(config =>
10501050
{
10511051
config
10521052
.AddScript("~/js/remarkable.min.js")
1053-
.AddScript("~/js/tutorial.jsx")
1054-
.SetJsonSerializerSettings(new JsonSerializerSettings
1055-
{
1056-
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
1057-
ContractResolver = new CamelCasePropertyNamesContractResolver()
1058-
});
1053+
.AddScript("~/js/tutorial.jsx");
10591054
});
10601055
```
10611056

src/React.AspNet/ReactBuilderExtensions.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using React.Exceptions;
1515
using React.TinyIoC;
1616
using Microsoft.Extensions.DependencyInjection;
17+
using Newtonsoft.Json.Serialization;
18+
1719
#if !NET451
1820
using Microsoft.Extensions.Caching.Memory;
1921
#endif
@@ -46,6 +48,11 @@ public static IApplicationBuilder UseReact(
4648
app.ApplicationServices.GetService<IHttpContextAccessor>(),
4749
registerOptions
4850
));
51+
52+
// Camelcase JSON properties by default - Can be overridden per-site in "configure".
53+
ReactSiteConfiguration.Configuration.JsonSerializerSettings.ContractResolver =
54+
new CamelCasePropertyNamesContractResolver();
55+
4956
configure(ReactSiteConfiguration.Configuration);
5057

5158
// Allow serving of .jsx files
@@ -99,4 +106,4 @@ private static void RegisterAspNetServices(TinyIoCContainer container, IServiceP
99106
#endif
100107
}
101108
}
102-
}
109+
}

src/React.Sample.Mvc6/Startup.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6868
config
6969
.SetReuseJavaScriptEngines(true)
7070
.AddScript("~/js/Sample.jsx")
71-
.SetJsonSerializerSettings(new JsonSerializerSettings
72-
{
73-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
74-
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
75-
});
71+
.SetUseDebugReact(true);
7672
});
7773

7874
// Add static files to the request pipeline.

0 commit comments

Comments
 (0)