Asp.Net/Asp.Net core/Blazor摸索笔记

本文章随时更新,如有疑问或错误的地方欢迎留言。

目录

  1. 在子目录(如:admin)中使用Blazor:
  2.  启用多端view:(MVC中适用)
  3. 解决中文进行编码问题
  4. JSON的一些全局配置
  5. Apache代理WS
  6.  DbContext全局不跟踪(NoTracking)
  7. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值