asp.net core 杂记
1、输出时设置编码,读取配置
context.Response.ContentType = "text/html;charset=utf-8"; //"application/json;charset=utf-8","image/jpeg"
await context.Response.WriteAsync(Configuration["Title"]);
dotnet add package System.Text.Encoding.CodePages --version 4.5.1
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
Encoding encoding = Encoding.GetEncoding("gb2312");
using (var reader = new StreamReader(stream, encoding))
2、指定端口不指定IP,用机器ip (.net core 2.1)
.UseUrls("http://*:5000")
http://localhost:5000和http://ip:5000访问 (.net 6 及以后版本)
在appsettings.json加入 "urls":"http://192.168.120.23:5000;http://localhost:5000",

3、ASP.Net Core 在 CentOS 下使用 System.Drawing 绘图类报错
Unhandled Exception: System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory
at Interop.Libdl.dlopen(String fileName, Int32 flag)
at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary()
at System.Drawing.SafeNativeMethods.Gdip..cctor()
--- End of inner exception stack trace ---
at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromDelegate_linux(StreamGetHeaderDelegate getHeader, StreamGetBytesDelegate getBytes, StreamPutBytesDelegate putBytes, StreamSeekDelegate doSeek, StreamCloseDelegate close, StreamSizeDelegate size, IntPtr& image)
at System.Drawing.Image.InitFromStream(Stream stream)
at System.Drawing.Image.LoadFromStream(Stream stream, Boolean keepAlive)
解决办法:
# locate libdl
/usr/lib64/libdl-2.17.so
/usr/lib64/libdl.so.2
# cd /usr/lib64
# ln -s libdl-2.17.so libdl.so
然后又报另外一个错误:Unable to load DLL 'libgdiplus'
# yum install libgdiplus-devel
这样就解决了
4、nginx强制使用https访问(http跳转到https)
server {
listen 80;
server_name *.test.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
浙公网安备 33010602011771号