Fork me on GitHub

升级 ASP.NET Core 3.0 设置 JSON 返回 PascalCase 格式与 SignalR 问题

由于一些 JS 组件要求 JSON 格式是 PascalCase 格式,新版本 ASP.NET Core 3.0 中默认移除了 Newtonsoft.Json ,使用了微软自己实现的 System.Text.Json 来代替,可以修改继续使用 Newtonsoft.Json 来设置 PascalCase 格式属性名。

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

           //返回 PascalCase 问题
            //services.AddMvc()
            //        .AddJsonOptions(option => option.JsonSerializerOptions.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());

            services.AddControllersWithViews().AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()
            {
                NamingStrategy = new DefaultNamingStrategy()
            });

备注:使用 DefaultContractResolver 是 PascalCase 风格,使用 CamelCasePropertyNamesContractResolver 是 CamelCase 风格。

当然也可以使用默认的 System.Text.Json 来代替

services.AddControllersWithViews().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

SignalR 问题

统一Hub 放在 UseEndpoints 下

SignalRAppBuilderExtensions.UseSignalR(IApplicationBuilder, Action<HubRouteBuilder>)”已过时:“This method is obsolete and will be removed in a future version. The recommended alternative is to use MapHub<THub> inside Microsoft.AspNetCore.Builder.UseEndpoints(...).

客户端迁移

SignalR JavaScript client has changed from @aspnet/signalr to @microsoft/signalr

REFER:
https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support

https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.0
https://www.cnblogs.com/dudu/p/10776880.html
https://www.cnblogs.com/stulzq/p/11497624.html

posted @ 2019-09-25 21:24  花儿笑弯了腰  阅读(1535)  评论(0编辑  收藏  举报