本文章随时更新,如有疑问或错误的地方欢迎留言。
目录
在子目录(如:admin)中使用Blazor:
?启用多端view:(MVC中适用)
解决中文进行编码问题
JSON的一些全局配置
Apache代理WS
?DbContext全局不跟踪(NoTracking)
在子目录(如:admin)中使用Blazor:
//目录名以admin为例
//blazor server 模式:
endpoints.MapFallbackToPage("{*path:regex(^(admin).*$)}", "/_host");
//blazor wasm (ASP.NET Core hosted) 模式:
//1、Client项目:
//编辑项目文件,在 <PropertyGroup>组中加入:
<StaticWebAssetBasePath>admin</StaticWebAssetBasePath>
//修改www/index.html文件:样式文件或其它引用文件如有路径错误,对应修改即可
<base href="/admin/" />
//2、Server项目:
//修改Startup.cs文件 在Config方法中修改或加入:
app.UseBlazorFrameworkFiles("/admin");
// app.UseEndpoints中修改MapFallbackToFile如下即可
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=index}/{id?}");
endpoints.MapFallbackToFile("/admin/{*path:nonfile}", "admin/index.html");
});
?启用多端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)
);
|