閱讀目錄:
ASP.NET vNext 發布已經過去了一個多月的時間,發布詳情請訪問:振奮人心吶!關于下一代.NET——ASP.NET vNext 的更多說明,當時看這完篇新聞的第一感覺就像文章標題一樣“振奮人心”,其實每個人都有自己的感受,對于我來說,ASP.NET vNext 最大的特性就是跨平臺(基于 Mono 運行時),試想下:作為.NET程序員,終于不被java或C++程序員挑自己的短板,確實是蠻振奮人心的。當然,除了跨平臺,使用 Roslyn 編譯器動態編譯(修改源代碼,刷新頁面即可)、云優先(Cloud First)、開源等等特性也是蠻重要的。
以下內容,只是一些個人看法,僅供參考學習,也歡迎討論指教。
Visual Studio "14" CTP(Community Technology PReviews)是下一代 Visual Studio 的社區預覽版,我個人認為 Visual Studio "14" CTP(以下簡稱:VS 14 CTP) 就是為 ASP.NET vNext 而量身定做的,當然也包含了其他模塊,比如:Store Apps、Cloud等等,以下是關于VS 14 CTP 的版本描述:
CTPs are provided for testing and feedback purposes only. CTPs are unsupported, English-only releases. They are not subject to final validation and are not intended for use on production computers, or to create production code.Installing a CTP release will place a computer in an unsupported state. For that reason, we recommend only installing CTP releases in a virtual machine, or on a computer that is available for reformatting.Currently, Visual Studio "14" CTPs have known compatibility issues with previous releases of Visual Studio and should not be installed side-by-side on the same computer.
從版本中可以看出,VS 14 CTP 僅用于測試和反饋,只提供英文版本,而且安裝VS 14 CTP 會和已有的Visual Studio 版本沖突,也就是說安裝VS 14 CTP 要把電腦中Visual Studio 其他版本卸載,才能進行安裝。
至于“installed side-by-side on the same computer”,因為是先安裝的 VS 14 CTP,再安裝的 Visual Studio 其他版本,暫時沒發現異常情況。
VS 14 CTP 創建ASP.NET vNext 項目模塊:
VS 14 CTP 下載地址:
ASP.NET vNext 的發布文章稱其為.NET下一代,其實我個人覺得并不準確,ASP.NET vNext 就像其名字一樣,應該是 ASP.NET 的下一代版本。ASP.NET vNext 代表著.NET的未來的發展方向,比如說跨平臺、開源等,也就是 vNext。關于 ASP.NET vNext,園中也有幾位大神寫過幾篇介紹的文章,以下是個人整理關于 ASP.NET vNext 的一些特性(更新時間為6月3日):
MVC、 Web API 和 Web Pages合并成一個框架,稱為 MVC 6。新的框架使用一組通用的抽象為路由、 行為選擇、 篩選器、 模型綁定等等。
MVC 6 并不依賴于 System.Web,是一個更精簡的框架,用更快的啟動時間和內存占用較低(也就是“低內存消耗”以及“高吞吐量”)。
ASP.NET vNext 應用程序可以使用云優化(Cloud-Optimized).NET Framework。大約 11 M的大小相比于 200 M的完整的框架,也是組成的 NuGet 包的集合(云優化的功勞)。
云優化框架的減少使實際部署的應用程序框架:
提供云優化 Framework 的 NuGet 包,可實現以下目標:
ASP.NET vNext 與 Host 無關。你可以承載你的應用程序在 IIS 中,或自托管在一個自定義的過程(web API 2 和 SignalR 2 已經支持自托管,ASP.NET vNext 也會給 MVC 帶來同樣的功能)。
支持依賴注入(也可以使用自己的 IoC 容器來注冊依賴項)。
ASP.NET vNext 使用 Roslyn 編譯器動態編譯代碼。你可以編輯一個代碼文件,刷新瀏覽器,并看到所做的更改,并沒有重建項目。除了簡化開發過程,動態代碼編譯在現在變得可能,如在 Visual Studio Online ("Monaco") 進行代碼編寫,你可以選擇你自己的編輯器和工具。
ASP.NET vNext 開放源代碼。
ASP.NET vNext 跨平臺。
.NET未來構想圖,對于 ASP.NET vNext 來說,可以看出尤其側重于云優化-Cloud Optimized,包含高吞吐量、低內存消耗、并行執行和跨平臺:
ASP.NET vNext 成員圖:
從上圖可以更直觀的看出 ASP.NET vNext 所涵蓋的東西,這樣的統一管理,帶給編程人員好處是很多的,比如:一路由系統、 一個模型綁定系統、 一個篩選器管道等,也可以完美的從 Web Pages 過渡到 MVC。
.NET vNext 和 .NET vNext -云優化(包含模塊)示意圖:
ASP.NET vNext 云優化示意圖:
代碼到 Web 服務器示意圖,”快速迭代式開發“,即修改代碼刷新瀏覽器,接近實時,也可以實現”云編輯“功能:
ASP.NET vNext 跨平臺示意圖:
關于 ASP.NET vNext 的實踐,MSDN 已經更新了"Music Store"(音樂商店)項目,感興趣的朋友可以參考練習下,下面做的ASP.NET vNext 簡單示例,只是個人的學習記錄,方面以后自己查看,園友們可以直接略過。
首先打開 VS 14 CTP,新建 ASP.NET vNext 項目。
ASP.NET vNext 主要包含三類模板:
新建 vNextDemo 項目后,發現目錄結構非常簡單.
共包含兩個項目文件:
在 ASP.NET vNext 中看不到我們常用的 config 文件了,取而代之的是 json(或是其他文件類型)配置文件,例如用來配置連接字符串的 config.json 文件:
1 {2 "Data": {3 "DefaultConnection": { 4 "ConnectionString": "Server=(localdb)//mssqllocaldb;Database=aspnetvnext-49333c44-d261-476e-b40a-3d9b759fc2da;Trusted_Connection=True;Mult貼一段 project.json 示例配置:
1 { 2 "dependencies": { 3 "Helios": "0.1-alpha-build-0585", 4 "Microsoft.AspNet.Mvc": "0.1-alpha-build-1268", 5 "Microsoft.AspNet.Identity.Entity": "0.1-alpha-build-1059", 6 "Microsoft.AspNet.Identity.Security": "0.1-alpha-build-1059", 7 "Microsoft.AspNet.Security.Cookies": "0.1-alpha-build-0506", 8 "Microsoft.AspNet.Server.WebListener": "0.1-alpha-build-0520", 9 "Microsoft.AspNet.StaticFiles": "0.1-alpha-build-0443",10 "Microsoft.Data.Entity": "0.1-alpha-build-0863",11 "Microsoft.Data.Entity.SqlServer": "0.1-alpha-build-0863",12 "Microsoft.Framework.ConfigurationModel.Json": "0.1-alpha-build-0233",13 "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0-alpha"14 },15 "commands": {16 /* Change the port number when you are self hosting this application */17 "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"18 },19 "configurations": {20 "net45": {21 "dependencies": {22 "System.Data": "",23 "System.ComponentModel.DataAnnotations": ""24 }25 },26 "k10": {27 }28 }29 }在 dependencies(依賴關系)節點,看起來有點像 NuGet 的 packages.config 配置包,但是不同之處是,以前版本中會直接添加這項項的引用,而且還取決于這些包,現在都是動態配置,比如刪除一個引用,它會自動刪除所依賴項,如果在 project.json 中未使用,也會動態的移除掉。
除了 dependencies,還包含 commands(命令)和 configurations(配置)節點,commands 中的”web“配置表示,應用程序默認啟動地址,configurations 中的”net45“表示完整的桌面應用程序,”k10“表示云優化。
Startup.cs
Startup.cs 是整個應用程序的啟動類,有點類似于我們常用的 Global.asax,貼一段示例代碼(ASP.NET vNext Web Application):
1 using System; 2 using Microsoft.AspNet.Builder; 3 using Microsoft.AspNet.Http; 4 using Microsoft.AspNet.Identity; 5 using Microsoft.AspNet.Identity.Security; 6 using Microsoft.AspNet.Routing; 7 using Microsoft.AspNet.Security.Cookies; 8 using Microsoft.Data.Entity; 9 using Microsoft.Framework.ConfigurationModel;10 using Microsoft.Framework.DependencyInjection;11 using WebApplication1.Models;12 13 namespace WebApplication114 {15 public class Startup16 {17 public void Configure(IBuilder app)18 {19 // Enable Browser Link support20 app.UseBrowserLink();21 22 // Setup configuration sources23 var configuration = new Configuration();24 configuration.AddJsonFile("config.json");25 configuration.AddEnvironmentVariables();26 27 // Set up application services28 app.UseServices(services =>29
新聞熱點
疑難解答