Introducing ASP.NET 5

來源:互聯網
上載者:User

標籤:

原文: http://weblogs.asp.net/scottgu/introducing-asp-net-5

 

ASP.NET 5預覽版發布了, 可以在這下載最新的Visual Studio 2015體驗

ASP.NET 5開源, 可以在Windows, Linux和Mac上開發並運行.

ASP.NET 5 有如下一些基本的改進:

  • 跨平台可在 Windows, Mac和Linux上編譯並運行
  • Built on .NET Core, which supports true side-by-side app versioning
  • 一些新的工具簡化現在web開發
  • Single aligned web stack for Web UI and Web APIs
  • 雲環境配置
  • 整合支援建立使用NuGet包
  • 整合以來注入
  • 可以host在IIS或者self-host
現代web開發

 ASP.NET 5有許多不錯的特效能讓你更好的開發web應用:

動態開發(Dynamic Development)

在Visual Studio 2015, 動態編譯能給我們帶來更流暢的開發體驗. 當你想開到改代碼後的結果時你不需要在去手動編譯你的程式. 你需要的僅僅是(1) 修改代碼, (2) 儲存更改, (3) 重新整理瀏覽器, (4) 查看更改後的結果.

整合流行的web開發工具(Bower, Grunt, Gulp)

Visual Studio 2015 繼承了Bower, Grunt和Gulp.

  • Bower是一個用戶端包管理器, 包括JavaScript和CSS.
  • Grun和Gulp是任務運行器.

Bower: 通過直接在bower.json config檔案中加入JS類庫到ASP.NET項目:

你依然能通過NuGet包管理器添加服務端的包.

 

Grunt: 在現代web開發中, 你要管理很多任務: 編譯LESS, TypeScript, CoffeeScript linting, JavaScript minification, 運行JS單元測試 等等. 任務運行器能夠很方便的管理協調這些任務. Visual Studio 2015支援兩種流行的工作管理員, Grunt和Gulp.

例如, 如果你想使用Grunt編譯LESS檔案. 開啟package.json 添加 grunt-contrib-less 包, 他是一個第三方的Grunt外掛程式.

使用任務運行器的側邊欄綁定任務到編譯的步驟(編譯前, 編譯時間, clean, 開啟解決方案時).

 

更簡單的依賴管理

在ASP.NET 5中可以通過添加NuGet包管理依賴. 你可以使用NuGet包管理器或者簡單的編輯JSON檔案(project.json) . 

project.json檔案類似於下面的樣子:

在Visual Studio 2015中, 智能提示會協助你找到NuGet包.

智能提示還可以協助你找到可用的版本號碼:

雲配置

在ASP.NET 5中, 我們消除了使用Web.config檔案配置值

在Startup.cs檔案中, 你可以添加提供config值的源.

上面的代碼展示了一個項目被設定成從一個json檔案擷取config值和環境變數.  在config.json檔案中你可以提供下面的內容.

依賴注入

在MVC, Web API 和 SignalR中整合了依賴注入. ASP.NET 5 提供了內建的依賴注入. You can access services at startup, in middleware, in filters, in controllers, in model binding and virtually any part of the pipeline where you want to use your services. ASP.NET 5 includes a minimalistic DI container to bootstrap the system, but you can easily replace the default container with your container of choice (Autofac, Ninject, etc). Services can be singleton, scoped to the request or transient.

來看看怎麼在ASP.NET MVC 6中使用建構函式注入, 建立一個ASP.NET 5 Starter Web項目. 添加一個簡單的time service:

using System;

 

namespace WebApplication1

{

    public class TimeService

    {

        public TimeService()

        {

            Ticks = DateTime.Now.Ticks.ToString();

        }

        public String Ticks { get; set; }

    }

}

 

下一步在Startup類的ConfigureServices方法中註冊time service為一個瞬時service:

public void ConfigureServices(IServiceCollection services)

{

    services.AddMvc();

    services.AddTransient<TimeService>();

}

然後, 更新HomeController使用建構函式注入.

public class HomeController : Controller

{

    public TimeService TimeService { get; set; }

 

    public HomeController(TimeService timeService)

    {

        TimeService = timeService;

    }

 

    public IActionResult About()

    {

        ViewBag.Message = TimeService.Ticks + " From Controller";

        System.Threading.Thread.Sleep(1);

        return View();

    }

 

    // Code removed for brevity

}

 

在MVC 6可以使用[Activate]通過屬性注入服務.你可以在controller、fitler、view components使用[Activate]注入:

public class HomeController : Controller

{

    [Activate]

    public TimeService TimeService { get; set; }

 

    // Code removed for brevity

}

MVC 6同樣支援在Razor視圖中通過使用@inject關鍵字注入:

@using WebApplication23

@inject TimeService TimeSvc

 

<h3>@ViewBag.Message</h3>

 

<h3>

    @TimeSvc.Ticks From Razor

</h3>

 

更快的HTTP效能

ASP.NET 5引入了新的HTTP請求管道, 以為他是模組化的因此你可以僅添加你需要的組件. 管道同事不在依賴於 System.Web. 通過減少管道的開銷,你的應用效能能更好.

在Startup類的Configure方法中自訂你需要在管道中用那些組件. 在Configure方法中指定你要用哪些請求管道. ASP.NET 5已經包含了一些來自於Katana項目的, 比如靜態檔案中介軟體, 驗證許可權和診斷中介軟體. 下面的代碼展示了你可以添加刪除管道.

public void Configure(IApplicationBuilder app)

{

    // Add static files to the request pipeline.

    app.UseStaticFiles();

 

    // Add cookie-based authentication to the request pipeline.

    app.UseIdentity();

 

    // Add MVC and routing to the request pipeline.

    app.UseMvc(routes =>

    {

    routes.MapRoute(

        name: "default",

        template: "{controller}/{action}/{id?}",

        defaults: new { controller = "Home", action = "Index" });

 

});

你可以寫一些自己的管道組件中介軟體.

開源

開原始碼在這 GitHub. 

Introducing ASP.NET 5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.