python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結

寫在前面

ASP.NET Core是微軟新推出的支持跨平臺、高性能、開源的開發框架,它的優勢不必多說,因為已經說得太多了。當然,現在依然有著數量龐大的系統運行於.NET Framework上,由於有大量的Break Changes,很多項目項目團隊也不敢貿然升級,其中的考量也不全部是技術原因,更多的可能還是業務推進因素。

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結


小編自年前開始考慮升級一套電商系統,原先是基於.NET Framework 4.5的,打算直接升級到.NET Core 3.1,由於系統規模比較龐大,所以一旦開工就是一個漫長的工程。

年前第一次重構時,由於低估這套系統的複雜性再加上有些冒進,步子邁得有點大,出現了很多問題,不得不重新開始。這一次重構先易後難,步步為營,難題統一在後面解決,到現在已經完成了全部工程的百分之八十,後面的也沒有太困難了,所以特地抽出時間小結一下。

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結


詳細內容:

類庫部分

類庫部分的遷移應該是最簡單的了,我是創建了一個新的類庫,然後把代碼copy過去,很少有地方需要修改,當然了有一些引用的第三方類庫需要特殊對待,如Automapper、Autofac、FluentValidation等,這些也很簡單,看看文檔就行。

.NET Framework中,會有一些常用的封裝庫,如Session、Cookie和HttpRuntime等,這些變化比較大,所以自己在Startup中啟用。

  • Session:
    • Startup.Configure: 1: app.UseSession(new SessionOptions 2: { 3: Cookie = new CookieBuilder 4: { 5: 6: }, 7: IdleTimeout = TimeSpan.FromSeconds(1), 8: IOTimeout = Timeout.InfiniteTimeSpan 9: });
    • Startup.ConfigureServices: 1: services.AddSession();
    • 使用Session,可以通過HttpContext調用: 1: HttpContext.Session.SetString("sessionId", sessionValue); 2: HttpContext.Session.GetString("sessionId"); 3: context.Session.Remove("sessionId");
  • Cookie:
    • 1: Response.Cookies.Append("User", "1", new CookieOptions() 2: { 3: Expires = DateTime.Now.AddMinutes(10) 4: }); 5: Response.Cookies.Delete("User");
  • HttpRuntime的使用,可以通過IMemoryCache替換,具體的使用方法可參考MSDN
  • System.Drawing已經不存在了,我使用的是ZKWeb.System.Drawing,基本上類名、枚舉名沒變化,只是命名空間Drawing變成了DrawingCore
  • 依賴注入部分全部遷移到Startup.ConfigureServices

Controller部分

順便說一下,靜態資源部分,如JS、CSS、Image、Font這些複製到wwwroot目錄上,另外app.UseStaticFiles();會在模板中出現。

1、獲取Controller及Action信息,可以通過RouteData.Values["controller"].ToString(),RouteData.Values["action"].ToString()

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結


2、很多的信息都放到了Request.Header[“”]中,如果之前可以用過Request直接點出來的,但是現在點不出來了,可以嘗試使用這種方式,說不準會有意外驚喜。另外有一個相關的常量在這裡出示一下,使用方式即Request.Header[HeaderNames.Authority],當然Request.HttpMethod 改為了 Request.Method。

<code>   1:  public static class HeaderNames/<code>
<code>   2:    {/<code>
<code>   3:      public static readonly string Accept;/<code>
<code>   4:      public static readonly string AcceptCharset;/<code>
<code>   5:      public static readonly string AcceptEncoding;/<code>
<code>   6:      public static readonly string AcceptLanguage;/<code>
<code>   7:      public static readonly string AcceptRanges;/<code>
<code>   8:      public static readonly string AccessControlAllowCredentials;/<code>
<code>   9:      public static readonly string AccessControlAllowHeaders;/<code>
<code>  10:      public static readonly string AccessControlAllowMethods;/<code>
<code>  11:      public static readonly string AccessControlAllowOrigin;/<code>
<code>  12:      public static readonly string AccessControlExposeHeaders;/<code>
<code>  13:      public static readonly string AccessControlMaxAge;/<code>
<code>  14:      public static readonly string AccessControlRequestHeaders;/<code>
<code>  15:      public static readonly string AccessControlRequestMethod;/<code>
<code>  16:      public static readonly string Age;/<code>
<code>  17:      public static readonly string Allow;/<code>
<code>  18:      public static readonly string Authority;/<code>
<code>  19:      public static readonly string Authorization;/<code>
<code>  20:      public static readonly string CacheControl;/<code>
<code>  21:      public static readonly string Connection;/<code>
<code>  22:      public static readonly string ContentDisposition;/<code>
<code>  23:      public static readonly string ContentEncoding;/<code>
<code>  24:      public static readonly string ContentLanguage;/<code>
<code>  25:      public static readonly string ContentLength;/<code> 
<code>  26:      public static readonly string ContentLocation;/<code>
<code>  27:      public static readonly string ContentMD5;/<code>
<code>  28:      public static readonly string ContentRange;/<code>
<code>  29:      public static readonly string ContentSecurityPolicy;/<code>
<code>  30:      public static readonly string ContentSecurityPolicyReportOnly;/<code>
<code>  31:      public static readonly string ContentType;/<code>
<code>  32:      public static readonly string Cookie;/<code>
<code>  33:      public static readonly string CorrelationContext;/<code>
<code>  34:      public static readonly string Date;/<code>
<code>  35:      public static readonly string DNT;/<code>
<code>  36:      public static readonly string ETag;/<code>
<code>  37:      public static readonly string Expect;/<code>
<code>  38:      public static readonly string Expires;/<code>
<code>  39:      public static readonly string From;/<code>
<code>  40:      public static readonly string Host;/<code>
<code>  41:      public static readonly string IfMatch;/<code>
<code>  42:      public static readonly string IfModifiedSince;/<code>
<code>  43:      public static readonly string IfNoneMatch;/<code>
<code>  44:      public static readonly string IfRange;/<code>
<code>  45:      public static readonly string IfUnmodifiedSince;/<code>
<code>  46:      public static readonly string KeepAlive;/<code>
<code>  47:      public static readonly string LastModified;/<code>
<code>  48:      public static readonly string Location;/<code>
<code>  49:      public static readonly string MaxForwards;/<code>
<code>  50:      public static readonly string Method;/<code> 
<code>  51:      public static readonly string Origin;/<code>
<code>  52:      public static readonly string Path;/<code>
<code>  53:      public static readonly string Pragma;/<code>
<code>  54:      public static readonly string ProxyAuthenticate;/<code>
<code>  55:      public static readonly string ProxyAuthorization;/<code>
<code>  56:      public static readonly string Range;/<code>
<code>  57:      public static readonly string Referer;/<code>
<code>  58:      public static readonly string RequestId;/<code>
<code>  59:      public static readonly string RetryAfter;/<code>
<code>  60:      public static readonly string Scheme;/<code>
<code>  61:      public static readonly string SecWebSocketAccept;/<code>
<code>  62:      public static readonly string SecWebSocketKey;/<code>
<code>  63:      public static readonly string SecWebSocketProtocol;/<code>
<code>  64:      public static readonly string SecWebSocketVersion;/<code>
<code>  65:      public static readonly string Server;/<code>
<code>  66:      public static readonly string SetCookie;/<code>
<code>  67:      public static readonly string Status;/<code>
<code>  68:      public static readonly string StrictTransportSecurity;/<code>
<code>  69:      public static readonly string TE;/<code>
<code>  70:      public static readonly string TraceParent;/<code>
<code>  71:      public static readonly string TraceState;/<code>
<code>  72:      public static readonly string Trailer;/<code>
<code>  73:      public static readonly string TransferEncoding;/<code>
<code>  74:      public static readonly string Translate;/<code>
<code>  75:      public static readonly string Upgrade;/<code> 
<code>  76:      public static readonly string UpgradeInsecureRequests;/<code>
<code>  77:      public static readonly string UserAgent;/<code>
<code>  78:      public static readonly string Vary;/<code>
<code>  79:      public static readonly string Via;/<code>
<code>  80:      public static readonly string Warning;/<code>
<code>  81:      public static readonly string WebSocketSubProtocols;/<code>
<code>  82:      public static readonly string WWWAuthenticate;/<code>
<code>  83:      public static readonly string XFrameOptions;/<code>
<code>  84:    }/<code>

3、Request.IsAjaxRequest

這個已經不存在了,可以自行實現。

<code>   1:  public static bool IsAjaxRequest(this HttpRequest request)/<code>
<code>   2:  {/<code>
<code>   3:      if (request == null)/<code>
<code>   4:          throw new ArgumentNullException("request");/<code>
<code>   5:   /<code>
<code>   6:      if (request.Headers != null)/<code>
<code>   7:          return request.Headers["X-Requested-With"] == "XMLHttpRequest";/<code>
<code>   8:      return false;/<code>
<code>   9:  }/<code>

4、Area註冊

之前的AreaRegistration已經不存在,如果需要設置Area,可以在每個Controller上設置[Area(“Admin”)],路由處的註冊可以考慮如下方式

<code>   1:  app.UseEndpoints(endpoints =>/<code>
<code>   2:  {/<code>
<code>   3:      endpoints.MapControllerRoute(/<code>
<code>   4:          name: "default",/<code>
<code>   5:          pattern: "{controller=Home}/{action=Index}/{id?}");/<code>
<code>   6:      /<code>
<code>   7:      endpoints.MapControllerRoute(/<code>
<code>   8:              name: "areas",/<code>
<code>   9:              pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"/<code>
<code>  10:          );/<code>
<code>  11:  });/<code>

5、AbsoluteUri也已經不存在了,但是可以通過如下方式取代:

<code>   1:  /// <summary>/<code>
<code>   2:  /// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)/<code>
<code>   3:  /// suitable only for display. This format should not be used in HTTP headers or other HTTP operations./<code>
<code>   4:  /// /<code>
<code>   5:  /// <param>The request to assemble the uri pieces from./<code>
<code>   6:  /// <returns>The combined components of the request URL in a fully un-escaped form (except for the QueryString)/<returns>/<code>
<code>   7:  /// suitable only for display./<code>
<code>   8:  public static string GetDisplayUrl(this HttpRequest request);/<code>
<code>   9:   /<code>
<code>  10:  /// <summary>Returns the relative URI./<summary>/<code>
<code>  11:  /// <param>The request to assemble the uri pieces from./<code>
<code>  12:  /// <returns>The path and query off of <paramref>./<returns>/<code> 
<code>  13:  public static string GetEncodedPathAndQuery(this HttpRequest request);/<code>
<code>  14:   /<code>
<code>  15:  /// <summary>/<code>
<code>  16:  /// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers/<code>
<code>  17:  /// and other HTTP operations./<code>
<code>  18:  /// /<code>
<code>  19:  /// <param>The request to assemble the uri pieces from./<code>
<code>  20:  /// <returns>The encoded string version of the URL from <paramref>./<returns>/<code>
<code>  21:  public static string GetEncodedUrl(this HttpRequest request);/<code>

6、過濾器

之前繼承ActionFilterAttribute,現在實現IActionFilter,註冊方式為services.AddMvc(o=>o.Filters.Add(new XX())),當然之前的很多過濾器或者Controller基類方法已經不存在了,如Controller OnAuthentication。

IResultFilter中的OnResultExecuting(ResultExecutingContext filterContext)需要通過filterContext.Controller as Controller來獲取默認的Controller。

最後有一個比較重要的類ActionDescriptor,ControllerDescriptor繼承自ActionDescriptor,這裡可以通過類型轉換獲取相關信息。

之前有很多的FilterAttribute也可以通過中間件來取代。

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結

7、Action上被去掉的Attribute,如[ValidateInput(false)],[ChildActionOnly]

View部分

1、頁面基類型及擴展

之前我們創建頁面基類型,是通過繼承System.Web.Mvc.WebViewPage<tmodel>來實現,現在我們可以通過RazorPage<tmodel>來取代。

擴展HtmlHelper也換成了IHtmlHelper接口。HtmlString也替換了MvcHtmlString,更上層也以接口方式來取代IHtmlContent。

<code>   1:  public static IHtmlContent AlignTypeSelect(this IHtmlHelper _html, string xxxxx)/<code>
<code>   2:  {/<code>
<code>   3:      //your code/<code>
<code>   4:      return new HtmlString(html.ToString());/<code>
<code>   5:  }/<code>

2、Ajax.BeginForm換成了當前.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()可以用通過替換,以下圖片摘自MSDN:

Framework MVC的寫法

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結

Core MVC的寫法

python大牛總結的ASP.NET MVC升級到ASP.NET Core MVC踩坑小結

一些Tag Help集錦:

Built-in ASP.NET Core Tag Helpers

Anchor Tag Helper

Cache Tag Helper

Component Tag Helper

Distributed Cache Tag Helper

Environment Tag Helper

Form Tag Helper

Form Action Tag Helper

Image Tag Helper

Input Tag Helper

Label Tag Helper

Link Tag Helper

Partial Tag Helper

Script Tag Helper

Select Tag Helper

Textarea Tag Helper

Validation Message Tag Helper

Validation Summary Tag Helper

4、@Html.Action和@Html.RenderAction可以通過ViewComponents來取代

<code>   1:  public class XXXXViewComponent : ViewComponent/<code>
<code>   2:  {/<code>
<code>   3:      public IViewComponentResult Invoke()/<code>
<code>   4:      {/<code>
<code>   5:          return this.View("");/<code>
<code>   6:      }/<code>
<code>   7:  }/<code>

調用方式是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的強大。如果小夥伴在升級過程中也遇到了很多問題,希望這篇文章可以給大家一些幫助,另外我沒有寫到的,大家可以留個言,我統一收集一下。

最後

多說一句,很多人學Python過程中會遇到各種煩惱問題,沒有人解答容易放棄。小編是一名python開發工程師,這裡有我自己整理了一套最新的python系統學習教程,包括從基礎的python腳本到web開發、爬蟲、數據分析、數據可視化、機器學習等。想要這些資料的可以關注小編,並在後臺私信小編:“01”即可領取。


分享到:


相關文章: