本文章随时更新,如有疑问或错误的地方欢迎留言。
目录
- 在子目录(如:admin)中使用Blazor:
- 启用多端view:(MVC中适用)
- 解决中文进行编码问题
- JSON的一些全局配置
- Apache代理WS
- DbContext全局不跟踪(NoTracking)
- Blazor Wasm 端字符串加密解密 (更新于:2021-08-04)
在子目录(如:admin)中使用Blazor:
具体参见我另一篇文章的更新
启用多端view:(MVC中适用)
//效果类似以前asp.net mvc的根据设备加载不同的View文件。
//如手机web端可以写视图文件为: index.mobile.hmtl等。
.AddRazorOptions(options => {
options.ViewLocationExpanders.Clear();
options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
})
TemplateViewLocationExpander的内容参考,可自行发挥:
其中HttpContext.Request.DeviceType()为扩展方法,根据UserAgent判断设置是手机还是PC浏览器还是微信内置浏览器等,你还可以细化出是IE,Firfox,或谷歌等。
public class TemplateViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
var request = context.ActionContext.HttpContext.Request;
var MobileList = new List<string>();
var WeChatList = new List<string>();
var deviceType = context.Values["DeviceType"];
if (deviceType == DeviceType.Mobile.ToString())
{
foreach (var item in viewLocations)
{
MobileList.Add(item.Replace(".cshtml", ".mobile.cshtml"));
}
}
else if (deviceType == DeviceType.WeChat.ToString())
{
foreach (var item in viewLocations)
{
WeChatList.Add(item.Replace(".cshtml", ".wechat.cshtml"));
MobileList.Add(item.Replace(".cshtml", ".mobile.cshtml"));
}
}
return WeChatList.Concat(MobileList).Concat(viewLocations);
}
public void PopulateValues(ViewLocationExpanderContext context)
{
context.Values["DeviceType"] = context.ActionContext.HttpContext.Request.DeviceType().ToString();
}
}
解决中文进行编码问题
services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
JSON的一些全局配置
services.AddControllersWithViews()
.AddJsonOptions(options=> {
options.JsonSerializerOptions.WriteIndented=false ;
//取消中文字被Unicode编码
options.JsonSerializerOptions.Encoder =JavaScriptEncoder.Create(UnicodeRanges.All);
//使用驼峰样式的key:JsonNamingPolicy.CamelCase,不使用则设置为null,
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
//反序列化不区分大小写
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Converters.AddDateFormatString("yyyy-MM-dd HH:mm:ss");
//options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; //忽略循环引用 .net6 中可用
});
Apache代理WS
httpd.conf:
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
vhost:
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:87/$1" [P,L]
DbContext全局不跟踪(NoTracking)
services.AddDbContext<DBContext>(
options => options.UseMySql(Configuration.GetConnectionString("MysqlConnection"))
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
);
Blazor Wasm 端字符串加密解密
WASM下使用System.Security.Cryptography中的大部分API是不能使用的,会报错
WASM下 System.Security.Cryptography中的加密仅支持以下,可以用SHA替代Md5
System.Security.Cryptography.RandomNumberGenerator
System.Security.Cryptography.IncrementalHash
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA256
System.Security.Cryptography.SHA384
System.Security.Cryptography.SHA512
System.Security.Cryptography.SHA1Managed
System.Security.Cryptography.SHA256Managed
System.Security.Cryptography.SHA384Managed
System.Security.Cryptography.SHA512Managed
微软官方目前也没有给出替代方案。具体可看微软官方文档:
中断性变更:Blazor WebAssembly 不支持的 System.Security.Cryptography API - .NET | Microsoft Docs