000.Introduction to ASP.NET Core--【Asp.net core 介紹】

来源:http://www.cnblogs.com/Meng-NET/archive/2016/09/23/5900147.html
-Advertisement-
Play Games

Introduction to ASP.NET Core Asp.net core 介紹 270 of 282 people found this helpful By Daniel Roth, Rick Anderson and Shaun Luttin Meng.Net 自譯 ASP.NET C ...



Introduction to ASP.NET Core

Asp.net core 介紹

270 of 282 people found this helpful

By Daniel Roth, Rick Anderson and Shaun Luttin

Meng.Net 自譯

ASP.NET Core is a significant redesign of ASP.NET. This topic introduces the new concepts in ASP.NET Core and explains how they help you

Asp.net core 是 asp.net 的重大重構版本 , 本文介紹asp.net core新的概念,並且解釋你怎樣

develop modern web apps.

開發現代web 應用.

Sections:

What is ASP.NET Core

ASP.NET Core is a new open-source and cross-platform framework for building modern cloud based internet connected applications, such as

Asp.net core 是一個新的 ,開源的,跨平臺的框架, 可用來構建現代的,基於雲連接的應用程式,例如:

web apps, IoT apps and mobile backends. ASP.NET Core apps can run on .NET Core or on the full .NET Framework. It was architected to

web應用,物聯網應用,移動後臺. Asp.net core 程式 可以運行在.net core 或者 .net framework 上. 它被構建

 provide an optimized development framework for apps that are deployed to the cloud or run on-premises. It consists of modular components

提供一個最優化的應用開發框架,可以部署在雲或運行在本地. 它由最小限度模塊化組件

with minimal overhead, so you retain flexibility while constructing your solutions. You can develop and run your ASP.NET Core apps cross-

構成. 因此構建程式時你可以保持靈活性. 你可以 跨平臺 開發/運行你的asp.net core 應用 在

platform on Windows, Mac and Linux. ASP.NET Core is open source at GitHub.

Windows /mac /linux 系統, asp.net core 在github 上是開源的 .

Why build ASP.NET Core

The first preview release of ASP.NET came out almost 15 years ago as part of the .NET Framework. Since then millions of developers have

作為.net framework 的一部分, 第一個asp.net 預覽 已放發佈超過15年了 . 從此,數百萬的開發者

used it to build and run great web apps, and over the years we have added and evolved many capabilities to it.

用它 構建/運行 偉大的web應用, 並且在這些年中我們給他新增並擴展了很多功能.

ASP.NET Core has a number of architectural changes that result in a much leaner and modular framework. ASP.NET Core is no longer based on

Asp.net core 有一些結構設計上的改變,使得其高度簡潔,模塊化. Asp.net core 不在基於 system.web.dll .

System.Web.dll. It is based on a set of granular and well factored NuGet packages. This allows you to optimize your app to include just the

它基於 一個細粒度的 / 因素分解的 nuget 包集合 . 這樣 你就可以優化你的應用,使其僅包含

NuGet packages you need. The benefits of a smaller app surface area include tighter security, reduced servicing, improved performance, and

你需要的 nuget 文件.  好處是 一個小的 引用 但是包含了安全/低耗/演進/

decreased costs in a pay-for-what-you-use model.

降低你的花費開銷.

With ASP.NET Core you gain the following foundational improvements:

使用 asp.net core 你獲得了 以下基本的改進:

  • A unified story for building web UI and web APIs

一個 構建 web UI / web api 的統一模式

集成 現代的客戶端框架 及 開發流程

一個 基於雲的 環境配置系統

內嵌 依賴註入 DI

  • New light-weight and modular HTTP request pipeline

新的 輕量級的 / 模塊化 的 http 請求管道

  • Ability to host on IIS or self-host in your own process

可以 宿主與iis 或者 自宿主

  • Built on .NET Core, which supports true side-by-side app versioning

以 .net core 作為基礎, 支持 並行 版本 控制

  • Ships entirely as NuGet packages

完全以 nuget 包作為承載

  • New tooling that simplifies modern web development

新的工具簡化現代web開發

  • Build and run cross-platform ASP.NET apps on Windows, Mac and Linux

編譯並跨平臺運行,在 windows/mac/linux 系統上

  • Open source and community focused

開源/開源社區,支持的

Application anatomy   

應用程式解析

An ASP.NET Core app is simply a console app that creates a web server in its Main method:

一個asp.net core 應用,是一個簡單的控制台應用在它的main方法中創建的web服務.

 

 1 using System;
 2 using Microsoft.AspNetCore.Hosting;
 3 
 4 namespace aspnetcoreapp
 5 {
 6     public class Program
 7     {
 8         public static void Main(string[] args)
 9         {
10             var host = new WebHostBuilder()
11                 .UseKestrel()
12                 .UseStartup<Startup>()
13                 .Build();
14 
15             host.Run();
16         }
17     }
18 }
Program.cs

 

Main uses WebHostBuilder, which follows the builder pattern, to create a web application host. The builder has methods that define the web server

Main 方法使用 WebHostBuilder 創建了一個 Host 。 WebHostBuilder 中 定義了 創建web伺服器(Kestrel)的方法。

(for example UseKestrel) and the startup class (UseStartup). In the example above, the Kestrel web server is used, but other web servers can be

在上面的例子中,指定使用了 Kestrel 伺服器,同時也可以指定使用其它的伺服器(如IIS)。

specified. We’ll show more about UseStartup in the next section. WebHostBuilder provides many optional methods including UseIISIntegration for

關於 Startup 我們在下麵的章節中將會講解更多。WebHostBuilder 類提供了很多可選方法,如 UseIISIntegration() 可以使用IIS宿主

hosting in IIS and IIS Express and UseContentRoot for specifying the root content directory. The Build and Run methods build the IWebHost that

程式,UseContentRoot()  提供根目錄。Build()  創建 熱點

will host the app and start it listening for incoming HTTP requests.

Run()  將開始監聽程式的 HTTP 請求。

Startup

The UseStartup method on WebHostBuilder specifies the Startup class for your app.

WebHostBuilder 類 UseStartup() 方法使用你在程式中指定的 Startup 類。

 

 1 public class Program
 2 {
 3     public static void Main(string[] args)
 4     {
 5         var host = new WebHostBuilder()
 6             .UseKestrel()
 7             .UseStartup<Startup>()
 8             .Build();
 9 
10         host.Run();
11     }
12 }
Program.cs

 

The Startup class is where you define the request handling pipeline and where any services needed by the app are configured. The Startup class

在 Startup 類中你將定義 請求處理管道 和 註冊配置應用所需的 服務。

must be public and contain the following methods:

Startup 類必須是 public 的 ,並且包含下麵兩個方法:

 

 1 public class Startup
 2 {
 3     public void ConfigureServices(IServiceCollection services)
 4     {
 5     }
 6 
 7     public void Configure(IApplicationBuilder app)
 8     {
 9     }
10 }
Startup.cs

 

  • ConfigureServices defines the services (see Services below) used by your app (such as the ASP.NET MVC Core framework, Entity Framework

  ConfigureServices() 定義了應用中需要的服務,如:ASP.NET MVC Core、Entity Framework Core、Identity 等等。

Core, Identity, etc.)

  • Configure defines the middleware in the request pipeline

  Configure() 定義了請求管道中的中間件。

Services

A service is a component that is intended for common consumption in an application. Services are made available through dependency injection.

service 是一種在 應用 中被用來公共使用的 組件。服務(Services)通過依賴註入(DI)的方式使用。

ASP.NET Core includes a simple built-in inversion of control (IoC) container that supports constructor injection by default, but can be easily

Asp.net core 內置了IOC 容器,這個容器預設使用構造函數註入的方式,他可以簡易的替換掉你自己選擇的第三方IOC容器。

replaced with your IoC container of choice. In addition to its loose coupling benefit, DI makes services available throughout your app. For

除了松耦合的好處外,DI 讓 services 在你的整個應用程式過程中都有效,

example, Logging is available throughout your app. See Dependency Injection for more details.

如:Logging

Middleware

In ASP.NET Core you compose your request pipeline using Middleware. ASP.NET Core middleware performs asynchronous logic on an

在 asp.net core 中,你將用中間件組成你的請求管道。中間件在 HttpContext 中將非同步調用執行,一個執行完後會按順序執行下一個

HttpContext and then either invokes the next middleware in the sequence or terminates the request directly. You generally “Use” middleware by

或者直接終止退出請求。你通常會這樣使用中間件:

taking a dependency on a NuGet package and invoking a corresponding UseXYZ extension method on the IApplicationBuilder in the Configure

在Configure() 中 IApplicationBuilder 實例上 使用形如 UseXYZ 的擴展方法。

method.

ASP.NET Core comes with a rich set of prebuilt middleware:

Asp.net core 官方 提供了一組可用的中間件:

You can also author your own custom middleware.

同樣,你也可以自定義你自己的中間件。

You can use any OWIN-based middleware with ASP.NET Core. See Open Web Interface for .NET (OWIN) for details.

Servers

The ASP.NET Core hosting model does not directly listen for requests; rather it relies on an HTTP server implementation to forward the request

Asp.net core 的host 模塊不會直接監聽請求;當然了,它依賴於一個 HTTP server 的實現。

to the application. The forwarded request is wrapped as a set of feature interfaces that the application then composes into an HttpContext.

實現類轉遞過來的請求按照藉口約定組裝進 HttpContext 中。

ASP.NET Core includes a managed cross-platform web server, called Kestrel, that you would typically run behind a production web server like

Asp.net core 包含一個被托管的、跨平臺的web伺服器(Kestrel),就像iis 或 nginx 一樣。

IIS or nginx.

Content root

The content root is the base path to any content used by the app, such as its views and web content. By default the content root is the same as

根目錄(The content root)是應用中所有內容的根目錄,如 views 。預設情況下,

application base path for the executable hosting the app; an alternative location can be specified with WebHostBuilder.

它與宿主app執行的應用根目錄相同;也可用 WebHostBuilder 指定。

Web root

The web root of your app is the directory in your project for public, static resources like css, js, and image files. The static files middleware will

The web root 目錄是你應用中 靜態資源文件(css、js)等的根目錄。

only serve files from the web root directory (and sub-directories) by default. The web root path defaults to <content root>/wwwroot, but you can

預設情況下,靜態資源中間件只有在文件從web root 目中請求時才服務。預設情況下 The web root 的路徑是

specify a different location using the WebHostBuilder.

<content root>/wwwroot ,同時此路徑你也可以在WebHostBuilder 中另外指定不同的路徑。

Configuration

ASP.NET Core uses a new configuration model for handling simple name-value pairs. The new configuration model is not based on

Asp.ent core 用了一個新的 配置處理模塊 來處理簡單的 鍵-值 對。 新的配置處理模塊不再基於 System.Configuration

System.Configuration or web.config; rather, it pulls from an ordered set of configuration providers. The built-in configuration providers support a

web.config ; 當然,它從配置提供程式中拉去。 內置的配置提供程式支持

variety of file formats (XML, JSON, INI) and environment variables to enable environment-based configuration. You can also write your own

多種類型的文件(xml、json、ini)並且 環境變數可以基於環境配置。同樣,你也可以自己寫

custom configuration providers.

自定義的配置提供程式。

See Configuration for more information.

Environments

Environments, like “Development” and “Production”, are a first-class notion in ASP.NET Core and can be set using environment variables. See

像 Development 、 Production 環境,在 asp.net core 中是一種優秀的概念 , 並且是可配置使用的環境變數。

Working with Multiple Environments for more information.

Build web UI and web APIs using ASP.NET Core MVC

  • You can create well-factored and testable web apps that follow the Model-View-Controller (MVC) pattern. See MVC and Testing.
  • You can build HTTP services that support multiple formats and have full support for content negotiation. See Formatting Response Data
  • Razor provides a productive language to create Views
  • Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files
  • You can create HTTP services with full support for content negotiation using custom or built-in formatters (JSON, XML)

用完全受支持的,使用內置或自定義的json 格式化器 創建 自定義的 http 服務

  • Model Binding automatically maps data from HTTP requests to action method parameters
  • Model Validation automatically performs client and server side validation

Client-side development

ASP.NET Core is designed to integrate seamlessly with a variety of client-side frameworks, including AngularJS, KnockoutJS and Bootstrap.

Asp.net core 從設計上無縫集成了客戶端開發 框架 ,包括:AngularJSKnockoutJSBootstrap

See Client-Side Development for more details.

Next steps


© Copyright 2016, Microsoft. Revision 406ff0d3.

 

                                         蒙

                                    2016-09-23  15:31  周五

 

 

 

             支付寶打賞:                                    微信打賞:  

                     

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 內置對象: Response對象:響應請求,Response對象用於動態響應客戶端請示,控制發送給用戶的信息,並將動態生成響應。 Response.Write("<script>alert('添加成功!')</script>"); 彈出提示窗,顯示添加成功 Response.Redirect("De ...
  • 本次要分享的是利用windows+nginx+iis+redis+Task.MainForm組建分散式架構,由標題就能看出此內容不是一篇分享文章能說完的,所以我打算分幾篇分享文章來講解,一步一步實現分散式架構;下麵將先給出整個架構的核心節點簡介,希望各位多多點贊: . 架構設計圖展示 . nginx ...
  • 按位取反運算符是按照二進位的每一位取反,比如byte類型,~0的結果就是255。 該功能可以在mask中做一些反轉操作 如下代碼,a存放了2,4,8三個值。用按位取反'~'運算符反轉 列印結果是 false,flase,false,true,true。Mask已經被反轉 比如在unity引擎中,該操 ...
  • 寫過不少次關於八皇後問題的代碼了,不過都是基於標準的控制台層面上的輸入輸出。這次決定採用WPF來實現一個帶有界面的八皇後的小程式 在開始寫代碼之前,首先回顧一下八皇後問題: 八皇後問題,是一個古老而著名的問題,是回溯演算法的典型案例。該問題是國際西洋棋棋手馬克斯·貝瑟爾於1848年提出:在8×8格的國 ...
  • c#自定義日誌記錄 很簡單:將類複製到項目中,最後在配置文件上配置一下:logUrl即可。 預設保存在:項目/temp/log ...
  • 當你在開發程式的時候, 調試(debugging)和日誌(logging)都是非常重要的工作。在應用中使用日誌主要有三個目的 l 監視代碼中的變數的變化情況,把數據周期性地記錄到文件中供其它應用進行統計分析工作 l 跟蹤代碼運行的軌跡,作為日後審計的依據 l 擔當集成開發環境中的調試器,... ...
  • 1、Connection對象主要提供與資料庫的連接功能 配置web.config文件 <appSettings> <add key="ConnectionString" value="Server=10.136.*.*;database=MTL;uid=sa;pwd=sa;"/> </appSett ...
  • // StringBuffer sb = new StringBuffer();// for(Object bid :list){// sb.append(bid+",");// }// return sb.deleteCharAt(sb.length()-1).toString(); ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...