后端迁移注意事项
文件上传部分 Request.Files替换为Request.Form.Files
旧:
Request.Files
Request.Form.Files.AllKeys[i]
Request.Form.Files[i].InputStream
新:
Request.Form.Files
Request.Form.Files[i].Name
Request.Form.Files[i].OpenReadStream()
Get请求返回问题
旧:
return Json(rst, JsonRequestBehavior.AllowGet);
新:
return Json(rst);
获取完整的请求路径Request.Url.AbsoluteUri 和获取当前请求的主机Request.Url.Host
旧:
Request.Url.AbsoluteUri
Request.Url.Host
Request.Url.LocalPath
Request.InputStream
Request.Url.Scheme
Request.Url.Authority
新:
Request.GetDisplayUrl()
Request.Host.Value
HttpContext.Request.Path
HttpContext.Request.Body
Request.Scheme
Request.Host.Value
获取远程客户端的 IP 主机地址
旧:
Request.UserHostAddress
新:
HttpContext.Connection.RemoteIpAddress
SetSession赋值,取值方式
旧:
Session["UserSchMsg"] = null;
Session["UserSchMsg"]
Session.SessionID
新:
HttpContext.Session.Remove("UserSchMsg");
HttpContext.Session.GetString("UserSchMsg")
HttpContext.Session.Id
清除session
旧:
System.Web.HttpContext.Current.Session.RemoveAll();
新:
HttpContext.Session.Clear();
设置缓存
旧:
ServiceManager.GetService<ICacheProvider>().GetCache().Set(key, Session.SessionID, DateTimeOffset.Now.AddDays(1));
新:
ServiceManager.GetService<ICacheProvider>().SetCache(key, HttpContext.Session.Id, TimeSpan.FromDays(1));
获取缓存数据
旧:
var cache = ServiceManager.GetService<ICacheProvider>().GetCache();
var tmpList = cache.Get(CacheKeys.MenuItem) as List<MenuItem>;
新:
var cache = ServiceManager.GetService<ICacheProvider>();
var tmpList = cache.GetCache<List<MenuItem>>(CacheKeys.MenuItem);
生成二维码【升级后】
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">内容</param>
/// <returns></returns>
public static byte[] CreateQRCode(string content, int bl = 5)
{QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);QRCode qrcode = new QRCode(qrCodeData);Bitmap qrCodeImage = qrcode.GetGraphic(bl, Color.Black, Color.White, null, 15, 6, false);MemoryStream ms = new MemoryStream();qrCodeImage.Save(ms, ImageFormat.Jpeg);return ms.ToArray();
}
新:
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">内容</param>
/// <returns></returns>
public static byte[] CreateQRCode(string content, int bl = 5)
{QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);byte[] qrCodeImage = [];using (PngByteQRCode qrCode = new PngByteQRCode(qrCodeData)){qrCodeImage = qrCode.GetGraphic(bl, Color.Black, Color.White);}return qrCodeImage;
}
EFCore返回Json数据首字母变成了小写解决方案
Program.cs
// 添加服务到容器
builder.Services.AddControllersWithViews((option) =>
{option.Filters.Add(typeof(MemberShipFilterAttribute));
}).AddApplicationPart(typeof(WgController).Assembly) // 注册外部类库中的控制器.AddControllersAsServices()// 将控制器作为服务添加到 DI 容器中.AddJsonOptions(options =>{// 设置为 PascalCase,保留属性首字母大写options.JsonSerializerOptions.PropertyNamingPolicy = null;});
获取服务端地址
旧:
@Url.GetURL
新:
@Url.Action
Server.UrlEncode
旧:
callBack = Server.UrlEncode($"{RequestHost}/rightframe/home/wxcallback"),
新:
callBack = WebUtility.UrlEncode($"{RequestHost}/rightframe/home/wxcallback")
CoreDbContext
旧:
using (var dbContext = new CoreDbContext()){}
新:
private readonly DbContextOptions<CoreDbContext> options = null;
/// <summary>
/// 构造函数
/// </summary>
public MatService(DbContextOptions<CoreDbContext> _options)
{options = _options;
}
//用到的地方
using (var dbContext = new CoreDbContext(options)){}
HostingEnvironment.MapPath
旧:
HostingEnvironment.MapPath("~/")
新:
using Microsoft.AspNetCore.Hosting;
using System.IO;private readonly IWebHostEnvironment _webHostEnvironment;public YourClassName(IWebHostEnvironment webHostEnvironment)
{_webHostEnvironment = webHostEnvironment;
}public string GetVideoThumbnailPath()
{// 获取应用程序的根目录string rootPath = _webHostEnvironment.ContentRootPath;// 构建 TempFiles\VideoThumbnail\ 路径string url = Path.Combine(rootPath, "TempFiles", "VideoThumbnail");return url;
}
获取UrlHelper语法变化
旧:
var urlHelper = new UrlHelper(Request.RequestContext);
var url = urlHelper.ImageUrl(pic.Pic.ToString(), 1024).ToString();
新:
var url = Url.ImageUrl(pic.Pic.ToString(), 1024).ToString();
RequestUri.Host
旧:
var key = Request.RequestUri.Host + "@" + SessionId;
新:
var key = $"{HttpContext.Request.Host.Value}@{SessionId}";
Execl导入StreamToLists根据读取流的语法变更。
旧:
/// <summary>/// 导入摄像头(第三方) Excel/// </summary>/// <returns></returns>[HttpPost]public JsonResult Channel_ImportFile(){if (Request.Files["ChannelFile"] != null){ExcelToList<ThirdImport> iExcelToList = new ExcelToList<ThirdImport>();var list = iExcelToList.StreamToLists(Request.Files["ChannelFile"].InputStream);if (list.Count > 0){list.RemoveAt(0);}return Json(new Page{rows = list,total = list.Count});}return Json(new Page{rows = new List<object>(),total = 0});}
新:
/// <summary>/// 导入摄像头(第三方) Excel/// </summary>/// <returns></returns>[HttpPost]public JsonResult Channel_ImportFile([FromForm] IFormFile channelFile){if (channelFile != null){ExcelToList<ThirdImport> iExcelToList = new ExcelToList<ThirdImport>();var list = new List<ThirdImport>();try{using (var stream = channelFile.OpenReadStream()){list = iExcelToList.StreamToLists(stream);}if (list.Count > 0){list.RemoveAt(0); // 移除 Excel 中的第一行(可能是表头)}}catch (Exception){return Json(new Page{rows = new List<object>(),total = 0});}return Json(new Page{rows = list,total = list.Count});}return Json(new Page{rows = new List<object>(),total = 0});}
return HttpNotFound()
旧:
return HttpNotFound();
新:
return NotFound();
前端迁移注意事项
前端模型
旧:
@using WingSoft.Web.Core;
@using WingSoft.Core;
@using Newtonsoft.Json;
@model List<MenuGroupItem>
新:
@inherits WingSoft.Web.Core.RazorBase<List<MenuGroupItem>>
@model List<MenuGroupItem>
前端文件版本号
旧:
<script src="~/Scripts/Mysafe.Core.js?v=@UiVersion"></script>
新:
<script src="~/Scripts/Mysafe.Core.js" asp-append-version="true"></script>
string rawJson = await new System.IO.StreamReader(Request.InputStream).ReadToEndAsync()
旧:
string rawJson = await new System.IO.StreamReader(Request.InputStream).ReadToEndAsync()
新:
string rawJson;using (var reader = new StreamReader(Request.Body, Encoding.UTF8, leaveOpen: true)){rawJson = await reader.ReadToEndAsync();}
MvcHtmlString
旧:
public List<MvcHtmlString> AssetPics { get; set; }
新:
public List<HtmlString> AssetPics { get; set; }