做仿制网站,小程序注册步骤,网站开发电商,网站提交链接入口写在前面ASP.NET Core是微软新推出的支持跨平台、高性能、开源的开发框架#xff0c;它的优势不必多说#xff0c;因为已经说得太多了。当然#xff0c;现在依然有着数量庞大的系统运行于.NET Framework上#xff0c;由于有大量的Break Changes#xff0c;很多项目项目团队… 写在前面ASP.NET Core是微软新推出的支持跨平台、高性能、开源的开发框架它的优势不必多说因为已经说得太多了。当然现在依然有着数量庞大的系统运行于.NET Framework上由于有大量的Break Changes很多项目项目团队也不敢贸然升级其中的考量也不全部是技术原因更多的可能还是业务推进因素。小编自年前开始考虑升级一套电商系统原先是基于.NET Framework 4.5的打算直接升级到.NET Core 3.1由于系统规模比较庞大所以一旦开工就是一个漫长的工程我的博客也在很长时间没有再更新有点对不起读者了。年前第一次重构时由于低估这套系统的复杂性再加上有些冒进步子迈得有点大出现了很多问题不得不重新开始。这一次重构先易后难步步为营难题统一在后面解决到现在已经完成了全部工程的百分之八十后面的也没有太困难了所以特地抽出时间小结一下。详细内容类库部分类库部分的迁移应该是最简单的了我是创建了一个新的类库然后把代码copy过去很少有地方需要修改当然了有一些引用的第三方类库需要特殊对待如Automapper、Autofac、FluentValidation等这些也很简单看看文档就行。.NET Framework中会有一些常用的封装库如Session、Cookie和HttpRuntime等这些变化比较大所以自己在Startup中启用。SessionStartup.Configure:app.UseSession(new SessionOptions
{Cookie new CookieBuilder{},IdleTimeout TimeSpan.FromSeconds(1),IOTimeout Timeout.InfiniteTimeSpan
});
Startup.ConfigureServices:services.AddSession();
使用Session可以通过HttpContext调用HttpContext.Session.SetString(sessionId, sessionValue);
HttpContext.Session.GetString(sessionId);
context.Session.Remove(sessionId);
CookieResponse.Cookies.Append(User, 1, new CookieOptions()
{Expires DateTime.Now.AddMinutes(10)
});//新增操作
Response.Cookies.Delete(User);//删除操作
HttpRuntime的使用可以通过IMemoryCache替换具体的使用方法可参考MSDN(链接https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?viewaspnetcore-3.1)System.Drawing已经不存在了我使用的是ZKWeb.System.Drawing基本上类名、枚举名没变化只是命名空间Drawing变成了DrawingCore依赖注入部分全部迁移到Startup.ConfigureServicesController部分顺便说一下静态资源部分如JS、CSS、Image、Font这些复制到wwwroot目录上另外app.UseStaticFiles();会在模板中出现。1、获取Controller及Action信息可以通过RouteData.Values[controller].ToString()RouteData.Values[action].ToString()2、很多的信息都放到了Request.Header[“”]中如果之前可以用过Request直接点出来的但是现在点不出来了可以尝试使用这种方式说不准会有意外惊喜。另外有一个相关的常量在这里出示一下使用方式即Request.Header[HeaderNames.Authority]当然Request.HttpMethod 改为了 Request.Method。public static class HeaderNames
{public static readonly string Accept;public static readonly string AcceptCharset;public static readonly string AcceptEncoding;public static readonly string AcceptLanguage;public static readonly string AcceptRanges;public static readonly string AccessControlAllowCredentials;public static readonly string AccessControlAllowHeaders;public static readonly string AccessControlAllowMethods;public static readonly string AccessControlAllowOrigin;public static readonly string AccessControlExposeHeaders;public static readonly string AccessControlMaxAge;public static readonly string AccessControlRequestHeaders;public static readonly string AccessControlRequestMethod;public static readonly string Age;public static readonly string Allow;public static readonly string Authority;public static readonly string Authorization;public static readonly string CacheControl;public static readonly string Connection;public static readonly string ContentDisposition;public static readonly string ContentEncoding;public static readonly string ContentLanguage;public static readonly string ContentLength;public static readonly string ContentLocation;public static readonly string ContentMD5;public static readonly string ContentRange;public static readonly string ContentSecurityPolicy;public static readonly string ContentSecurityPolicyReportOnly;public static readonly string ContentType;public static readonly string Cookie;public static readonly string CorrelationContext;public static readonly string Date;public static readonly string DNT;public static readonly string ETag;public static readonly string Expect;public static readonly string Expires;public static readonly string From;public static readonly string Host;public static readonly string IfMatch;public static readonly string IfModifiedSince;public static readonly string IfNoneMatch;public static readonly string IfRange;public static readonly string IfUnmodifiedSince;public static readonly string KeepAlive;public static readonly string LastModified;public static readonly string Location;public static readonly string MaxForwards;public static readonly string Method;public static readonly string Origin;public static readonly string Path;public static readonly string Pragma;public static readonly string ProxyAuthenticate;public static readonly string ProxyAuthorization;public static readonly string Range;public static readonly string Referer;public static readonly string RequestId;public static readonly string RetryAfter;public static readonly string Scheme;public static readonly string SecWebSocketAccept;public static readonly string SecWebSocketKey;public static readonly string SecWebSocketProtocol;public static readonly string SecWebSocketVersion;public static readonly string Server;public static readonly string SetCookie;public static readonly string Status;public static readonly string StrictTransportSecurity;public static readonly string TE;public static readonly string TraceParent;public static readonly string TraceState;public static readonly string Trailer;public static readonly string TransferEncoding;public static readonly string Translate;public static readonly string Upgrade;public static readonly string UpgradeInsecureRequests;public static readonly string UserAgent;public static readonly string Vary;public static readonly string Via;public static readonly string Warning;public static readonly string WebSocketSubProtocols;public static readonly string WWWAuthenticate;public static readonly string XFrameOptions;
}
3、Request.IsAjaxRequest这个已经不存在了可以自行实现。public static bool IsAjaxRequest(this HttpRequest request)
{if (request null)throw new ArgumentNullException(request);if (request.Headers ! null)return request.Headers[X-Requested-With] XMLHttpRequest;return false;
}
4、Area注册之前的AreaRegistration已经不存在如果需要设置Area可以在每个Controller上设置[Area(“Admin”)]路由处的注册可以考虑如下方式app.UseEndpoints(endpoints
{endpoints.MapControllerRoute(name: default,pattern: {controllerHome}/{actionIndex}/{id?});endpoints.MapControllerRoute(name: areas,pattern: {area:exists}/{controllerHome}/{actionIndex}/{id?});
});
5、AbsoluteUri也已经不存在了但是可以通过如下三个方法取代/// summary/// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations./// /summary/// param namerequestThe request to assemble the uri pieces from./param/// returnsThe combined components of the request URL in a fully un-escaped form (except for the QueryString)/// suitable only for display./returnspublic static string GetDisplayUrl(this HttpRequest request);/// summaryReturns the relative URI./summary/// param namerequestThe request to assemble the uri pieces from./param/// returnsThe path and query off of paramref namerequest /./returnspublic static string GetEncodedPathAndQuery(this HttpRequest request);/// summary/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers/// and other HTTP operations./// /summary/// param namerequestThe request to assemble the uri pieces from./param/// returnsThe encoded string version of the URL from paramref namerequest /./returnspublic static string GetEncodedUrl(this HttpRequest request);
6、过滤器之前继承ActionFilterAttribute现在实现IActionFilter注册方式为services.AddMvc(oo.Filters.Add(new XX()))当然之前的很多过滤器或者Controller基类方法已经不存在了如Controller OnAuthentication。IResultFilter中的OnResultExecuting(ResultExecutingContext filterContext)需要通过filterContext.Controller as Controller来获取默认的Controller。最后有一个比较重要的类ActionDescriptorControllerDescriptor继承自ActionDescriptor这里可以通过类型转换获取相关信息。之前有很多的FilterAttribute也可以通过中间件来取代。7、Action上被去掉的Attribute如[ValidateInput(false)][ChildActionOnly]View部分1、页面基类型及扩展之前我们创建页面基类型是通过继承System.Web.Mvc.WebViewPageTModel来实现现在我们可以通过RazorPageTModel来取代。扩展HtmlHelper也换成了IHtmlHelper接口。HtmlString也替换了MvcHtmlString更上层也以接口方式来取代IHtmlContent。2、Ajax.BeginForm换成了form asp-controllerDistributorGrade asp-actionSave idaddform data-ajaxtrue data-ajax-methodpost data-ajax-beginbegin data-ajax-successsuccess。当前.NET Core 依然支持Html.BeginForm不过我建议大家有时间的时候都替换一下具体请参考下一条。3、第2条出现的asp-action等是通过Razor Tag Helpers来实现的很多的自定义需要加入到_ViewImports.cshtml当然一些引用也可以统一放到这里如using Microsoft.AspNetCore.Routing这样就可以在当前的Area中作为全局引用了。Razor Tag Help是一个十分重要的功能它使得.NET Core MVC的开发更像是在写Html语言更加的清晰更加具有生产力。如Html.TextBoxFor()可以用通过input asp-for””/替换以下图片摘自MSDNFramework MVC的写法Core MVC的写法一些Tag Help集锦引用链接https://docs.microsoft.com/en-US/aspnet/core/mvc/views/tag-helpers/intro?viewaspnetcore-3.1Built-in ASP.NET Core Tag HelpersAnchor Tag HelperCache Tag HelperComponent Tag HelperDistributed Cache Tag HelperEnvironment Tag HelperForm Tag HelperForm Action Tag HelperImage Tag HelperInput Tag HelperLabel Tag HelperLink Tag HelperPartial Tag HelperScript Tag HelperSelect Tag HelperTextarea Tag HelperValidation Message Tag HelperValidation Summary Tag Helper4、Html.Action和Html.RenderAction可以通过ViewComponents来取代public class XXXXViewComponent : ViewComponent
{public IViewComponentResult Invoke(){return this.View();}
}
调用方式是await Component.InvokeAsync(“XXXXViewComponent”)详情请点击链接5、MvcHtmlString.Create()可以使用new Microsoft.AspNetCore.Html.HtmlString()取代6、IP地址可以通过HttpRequest.HttpContext.Connection.RemoteIpAddress获取7、之前通过helper 定义页面的函数这个已经被去掉了现在可以通过functions来取代小结限于篇幅先总结这么多系统尚未完全结束不过升级到.NET Core是一个非常棒的过程可以更好地体验.NET Core的强大。如果小伙伴在升级过程中也遇到了很多问题希望这篇文章可以给大家一些帮助另外我没有写到的大家可以留个言我统一收集一下。