000.Introduction to ASP.NET Core–【Asp.net core 介绍】


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?
  • Why build ASP.NET
    Core?
  • Application
    anatomy
  • Startup
  • Services
  • Middleware
  • Servers
  • Content
    root
  • Web root
  • Configuration
  • Environments
  • Build web UI and web APIs using ASP.NET Core
    MVC
  • Client-side
    development
  • Next steps

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
的合并模式

  • Integration of modern client-side
    frameworks
    and development workflows

集成 现代底客户端框架 及
开发流程

  • A cloud-ready environment-based configuration
    system

一个 基于出口之
环境布置体系

  • Built-in dependency
    injection

内嵌 依赖注入 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图片 2

 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 类。

 

图片 3图片 4

 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 的
,并且带有下面两只章程:

 

图片 5图片 6

 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()
定义了要管道中的中档件。

  • See Application
    Startup
    for more details

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 官方
提供了一样组可用的高中级件:

  • Static
    files
  • Routing
  • Authentication

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
从计划性上无缝集成了客户端支出 框架 ,包括:AngularJS、KnockoutJS
、Bootstrap

See Client-Side
Development for
more details.

Next
steps

  • Building your first ASP.NET Core MVC app with Visual
    Studio
  • Your First ASP.NET Core Application on a Mac Using Visual Studio
    Code
  • Building Your First Web API with ASP.NET Core MVC and Visual
    Studio
  • Fundamentals

© Copyright 2016, Microsoft. Revision 406ff0d3.

 

                                         蒙

                                    2016-09-23 
15:31  周五

 

 

 

         

                     

 

相关文章