Original https://docs.asp.net/en/latest/intro.html
by Daniel Roth, Rick Anderson and Shaun Luttin
Chapter
- What is ASP. NET Core?
- Why is an ASP. NET Core redesigned?
- Application analysis
- Start
- Service
- Middleware
ASP. NET Core is a major modification to ASP. It's basically a redesign. This article introduces you to some of the new concepts of ASP. And how these new features can help us write a popular web app today.
What is ASP. NET Core
ASP. NET Core is an open-source, cross-platform, web-based framework for the development of today's popular cloud page applications. To provide a cool development framework that can be deployed in the cloud and run locally, we built this framework almost from scratch. ASP. NET 5 is made up of module components that make the application less resource expensive and allow our solutions to remain flexible. ASP. NET 5 can be developed and run across platforms in three operating systems, Windows, Macs, and Linux. The ASP. NET 5 is fully open source and all source code is on GitHub.
Why redesign Asp.netcore
As part of the. NET Framework, ASP. NET from the first preview release to now has been almost 15 years, during this time, thousands of developers with ASP. I developed a lot of very good web applications, at the same time, we also improved and added a lot of features to ASP.
The ASP. NET core changes greatly in architecture so that the framework becomes very concise and modular. Instead of relying on the System.Web.dll assembly, ASP. NET Core is based on a series of finer-grained, more granular nuget packages, which allows you to optimize your application as needed, with only the NuGet packages you need. The benefits of a smaller app surface area include tighter security, reduced servicing, improved performance, and decrease D costs in a pay-for-what-you-use model.
With ASP. NET Core you will get a basic improvement:
- Build Web UI and Web APIs using a unified pattern,
- An integrated modern browser-side framework and development workflow,
- Cloud service-based configuration system,
- Self-containing dependency injection
- New lightweight and more modern HTTP request pipeline,
- You can deploy to IIS, or you can use yourself as a server,
- Built on. NET Core to support true side-by-side application versioning,
- Ship entiely as NuGet packages,
- Tools to simplify the development of modern web pages,
- Cross-platform, can be compiled and run on the Windows,mac,linux three platforms,
- Expose the source code and to the community attention.
Application analysis
The ASP. NET Core app is just a console application that creates a Web service in the Main method:
using System; using Microsoft.AspNetCore.Hosting; namespace aspnetcoreapp{ public class program { public Span style= "COLOR: #0000ff" >static void Main ( string [] args) { var host = new Webhostbuilder (). Usekestrel (). Usestartup <startup> (). Build (); Host. Run (); } }}
The main method uses the Webhostbuilder class that follows the builder pattern to build the Web application host. The web hosting constructor has methods that can be used to indicate which Web service (such as the Userkestrel method) to use, and from which class to start (by means of the Userstart method). In the example above, we use the Kestrel Web server, and we can also specify to use a different Web server. In the following chapters, let's talk about the UserStartup method. The Webhostbuilder class provides a number of options, such as the Useiisintegration method, which allows us to put our web application into IIS and IIS Express, and the Usercontentroot method can be used to specify the root directory of the content. The build method constructs a Iwebhost type of Web application server, and the Run method starts the application and listens for HTTP requests.
Start
Use the UserStartup method in the Webhostbuilder class to specify the startup class for the Web App.
Public class program{ publicstaticvoid Main (string[] args) { varnew webhostbuilder () . Usekestrel () . Usestartup<Startup>() . Build (); Host. Run ();} }
The Startup class in angle brackets is what we use to handle the request pipeline and the services that are needed to configure the program. The Startup class must be a public class and include the following two methods:
Public class startup{ publicvoid configureservices (iservicecollection services) { Public void Configure (Iapplicationbuilder app) { }}
- The Configureservices method defines the services that are needed in a Web application (see the "Services" section below for details) (for example, the ASP. NET MVC Core framework,entity Framework core,identify, etc.). is what we call the service).
- The Configure method indicates the middleware that is needed in the request pipeline.
- For more detailed confidence please see launch application.
Service
In an application, a service is a component that is used for resource sharing. The service is added to the system by means of a dependency injection. ASP. NET 5 includes a simple built-in "control inversion container (IoC)", which supports construction injection by default. It is also very easy to replace the default IOC container with your favorite IOC container. In addition to the benefits of loose coupling, DI (Dependency injection) also enables services to run through your application, such as: Logs (Logging) can be used anywhere in your entire Web application, depending on injection.
Middleware
in ASP. NET Core, the request pipeline is composed of middleware. In the HTTP context, from a logical point of view, the ASP. NET Core middleware executes asynchronously in the HTTP context, then selectively invokes the next middleware in the middleware sequence or terminates the HTTP request directly to return to the browser. In general, the middleware we use relies on nuget packages, which are unified in the Configure method in the Startup class, and are called by extension methods of the Iapplicationbuilder class, which are usually written as usexxx.
The ASP. NET 5 has built-in a large wave of middleware:
- static files
- Routing (Routing)
- Diagnostics (Diagnostics)
- Identity Verification (authentication)
You can also create custom middleware that belongs to you.
You can also use any OWIN-based middleware in ASP. For details, see Open Web Interface for. NET (OWIN) Introduction.
Server
The ASP. NET Core application hosting model does not directly listen for network requests, but relies on HTTP servers to deliver HTTP requests specifically to him. The specially delivered HTTP request is packaged into a collection of interfaces, which are encapsulated in the Htppcontext class in ASP. In ASP., you already have a cross-platform Web server written in managed code called Kestrel, and you can, of course, run your ASP. NET core programs in IIS and Nginx.
Content root Directory
Content and directories are the root paths of any resources (such as views and web content) that are used in our programs. By default, the content root is the root directory where our programs run, and of course you can specify additional directories as the root of the content through the Webhostbuilder class.
Web pages and directories
Web root is a folder in our project, used to store common static files, static files are the CSS, JS, and pictures that we often use to write Web pages. In the silent case, the static file middleware only handles files under the root directory of the Web page, files under Subdirectories, Web root directories in the project's Wwwroot file, and, of course, you can also specify another folder as the root directory of the Web page through the Webhostbuilder class.
Configuration
ASP. NET Core uses a new configuration model to handle simple "name-value" data. The new configuration model is not based on the "System.Configuration" and "Web. config" files, but rather relies on an ordered set of configuration providers. The built-in configuration provider for ASP. NET core supports multiple format profiles (Xml,json,ini) and uses environment variables, enabling environment-based configurations to be implemented. You can also write your own custom configuration provider as needed.
See configuration section for details
Environment
environments, such as production or product environments, can be set through environment variables in ASP. For more information, see Working in a variety of environments.
Using ASP. NET Core MVC to create Web UI and Web APIs
You can use the MVC pattern to create a web app that can be detached and testable, see MVC and testing for details.
You can write an HTTP service to support multiple return formats and support full content negotiation. For details, see Formatting the data.
Razor provides a product-level language to create a view
Tag Helpers can be used to generate HTML elements in the Razor file using server-side code.
You can creat HTTP Services with the full suport for content negotiation using custom or built-in formatters (Json,xml).
Model bindings automatically map the data in an HTTP request to the parameters of the action method.
Model validation automates client authentication and server-side validation.
Front-end development
ASP. NET Core is designed to seamlessly integrate a variety of front-end frameworks such as Angularjs,knockoutjs and Bootstrap, see front-end development for details.
Next
- 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
Introduction to ASP.