.Net开发中不太常用的DLL及用法
GBK,GB2312编码
<ItemGroup> <PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" /> </ItemGroup>
// System.Text.Encoding.CodePages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var Encoding = Encoding.GetEncoding("GBK");
获取操作系统
#if NET6_0_OR_GREATER OperatingSystem.IsWindows(); #else System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows); #endif OperatingSystem.IsBrowser();
.NET6忽略上传文件大小限制
builder.WebHost.UseKestrel(so => { so.Limits.MaxRequestBodySize = null; });
SQLite 加密,使用微软驱动可以实现加密,
普通的可视化工具不能打开,可以使用https://github.com/sqlitebrowser/sqlitebrowser/releases/download/v3.12.2/DB.Browser.for.SQLite-3.12.2-win64.zip
dotnet remove package Microsoft.Data.Sqlite dotnet add package Microsoft.Data.Sqlite.Core dotnet add package SQLitePCLRaw.bundle_e_sqlcipher
隐式引用命名空间
<!--隐式引用命名空间--> <ItemGroup Condition="'$(Language)' == 'C#' AND ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable')"> <Using Include="System" /> <Using Include="System.Linq" /> </ItemGroup> <!--排除命名空间--> <ItemGroup> <Using Remove="Microsoft.AspNetCore.Builder" /> </ItemGroup>
模型绑定
绑定源不仅仅只有这一种:
- [FromQuery]:从Url的查询字符串中获取值。查询字符串就是Url中问号(?)后面拼接的参数
- [FromRoute]:从路由数据中获取值。例如上例中的{id}
- [FromForm]:从表单中获取值。
- [FromBody]:从请求正文中获取值。
- [FromHeader]:从请求标头中获取值。
- [FromServices]:从DI容器中获取服务。相比其他源,它特殊在值不是来源于HTTP请求,而是DI容器。
建议大家在编写接口时,尽量显式指明绑定源。
集合入参绑定时
[FromQuery] string[] ids
可以采用以下格式之一:
- ids=1&ids=2
- ids[0]=1&ids[1]=2
- [0]=1&[1]=2
- ids[a]=1&ids[b]=2&ids.index=a&ids.index=b
- [a]=1&[b]=2&index=a&index=b
此外,表单还可以支持一种格式:ids[]=1&ids[]=2
[FromQuery] Dictionary<int, string> idNames
可以采用以下格式之一:
- idNames[1]=j&idNames[2]=k,注意:方括号中的数字是字典的key
- [1]=j&[2]=k
- idNames[0].key=1&idNames[0].value=j&idNames[1].key=2&idNames[1].value=k,注意:方括号中的数字是索引,不是字典的key
- [0].key=1&[0].value=j&[1].key=2&[1].value=k
docker下.net程序DateTime少8小时
可以在Dockerfile中添加
#修改时区,默认基于FROM镜像的时区
#Debian语法
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone