Learning ASP. NET Core Razor programming Series 1, asp. netrazor

Source: Internet
Author: User

Learning ASP. NET Core Razor programming Series 1, asp. netrazor
I. Overview

. NET Core 1.0 was intended to be learned when it was released. However, according to Microsoft's previous release rules, version 1.0 can be regarded as a public beta version and Version 2.0 is stable. Now the 2.1 version has been released, and the previous concerns about its "instability" have been dispelled. I decided to learn about it.

ASP. NET Core has the following advantages:

  • Generate a unified scenario of Web UI and Web API.
  • Integrates the new client framework and development workflow.
  • Environment-based cloud ready Configuration System.
  • Built-in dependency injection.
  • Lightweight, high-performance, modular HTTP request pipeline.
  • It can be hosted on IIS, Nginx, Apache, Docker, or in its own processes.
  • When setting the target to. NET Core, you can use parallel application version control.
  • Simplified New Web development tools.
  • Compatibility,. NET Core is compatible with. NET Framework, Xamarin, and Mono through the. NET standard.
  • It can be generated and run in Windows, macOS, and Linux.
  • Open source code and community-centric.
How does one perform cross-platform operations?

First, let's take a look at the official introduction to. net core.

. NET Framework is a "cross-Windows" platform. On this basis, it also supports "cross-language" in C # and VB languages ",

. NET Core is a general development platform that is jointly maintained by the. NET community on Microsoft and GitHub. It is cross-platform and supports Windows, macOS, and Linux, and can be used in devices, clouds, and embedded/IoT solutions.

. NET Core includes the following parts:

. NET runtime: Provides type system, assembly loading, garbage collector, local interoperability and other basic services.

A set of framework libraries: provides basic metadata types, application writing types, and basic utilities.

A set of SDK tools and language compilers: provides basic developer experience and can be used for. NET Core sdks.

"Dotnet" application host, used to start. NET Core applications. It selects runtime and manages runtime, and provides an assembly loading policy to start the application. The same host can also be used for. NET Core Release in roughly the same way. NET Core is released in two ways, including package release on NuGet.org and independent release. Released on NuGet.org in package mode, which makes the application more flexible and modular while improving performance.

. Net api and compatibility

You can regard. NET Core as a cross-platform version of. NET Framework in the. NET Framework base class library (BCL. It implements. NET standard specifications. . NET Core provides a subset of APIs that can be used in. NET Framework or Mono/Xamarin. In some cases, the type is not fully implemented (some Members are unavailable or moved ).

Relationship with. NET Standard

The. NET standard is an API specification used to describe a set of consistent. NET APIs that developers can use in each. NET implementation code. . NET implementation requires implementation of this specification to be considered to comply with. NET Standard and support for. NET Standard-oriented libraries.

Comparison with. NET Framework

. NET was first released by Microsoft in 2000, and then developed so far. For more than 15 years,. NET Framework has been the main. NET Implementation produced by Microsoft.

The main differences between. NET Core and. NET Framework are:

Application Model --. NET Core does not support all. NET Framework application models. In a program, many of these models are based on Windows technology, such as WPF (based on DirectX ). However, both. NET Core and. NET Framework support the console and ASP. NET Core application models.

API --. NET Core contains many APIs that are the same as. NET Framework but have a small number of APIs and have different components (different assembly names; different types and shapes in key use cases ). Currently, these differences usually need to be changed to port the source to. NET Core. . NET Core implements the. NET standard API. Over time, it will include more. NET Framework BCL APIs.

Subsystem --. NET Core implements the sub-level of the. NET Framework neutron system. It aims to implement simpler implementation and programming models. For example, code access security (CAS) is not supported, but reflection is supported.

Platform --. NET Framework supports Windows and Windows Server, while NET Core also supports macOS and Linux.

Open source --. NET Core is an open source, and the read-only subset of. NET Framework is an open source.

Although. NET Core is unique and significantly different from. NET Framework and other. NET implementations, it is still easy to share code using source or binary sharing technology.


2. start learning the first ASP. NET Core application

This tutorial describes how to create a Web application based on the ASP. NET Core Razor page.

First, install the following software

  • . NET Core SDK 2.0 or later
  • Visual Studio 2017, 15.3 or later, and includes ASP. NET Web development function modules

2. Create a Razor Web Application

2. In the pop-up "new project" dialog box, make the following options.

  • In the list on the left, select. NET Core, for example, Center 1.
  • In the middle list, select ASP. NET Core Web application, for example, Center 2.
  • Enter"RazorMvcBooks", Such as 3 in the middle.
  • In step 4, click "OK" to create a Web application

3. In the pop-up "NewASP. NET Core WebApplication-RazorMvcBooks"Dialog box to make the following selection:

  • In Area 1, select "Net Core" and "Asp. NET Core 2.0" from the drop-down list"
  • In step 2, select the Web Application
  • Click OK, as shown in figure 3.



To use the. NET Framework of ASP. NET Core, you must first select the framework in the first drop-down box, and then select the version of ASP. NET Core in the second drop-down box. For example, 1.

4. Visual Studio 2017 creates an ASP. NET Core MVC Razor page project using the default template. We just entered a project name and selected several options to create a Web application. This is a simple entry-level project. The project structure is as follows.

5. Press F5 to run the application in stock transfer mode or press Ctrl + F5 to run the application in non-debug mode.


  • Visual Studio 2017 starts IIS Express and runs the RazorMvcBooks application. Note: the address bar displays "localhost: Port" instead of a domain name (such as www.cnblogs.com ). This is because localhost is the standard Host Name of the local computer. When Visual Studio 2017 creates a Web project, the Web server uses a random port. In the preceding figure, the port number is 53416. The URL in the browser displays "http: // localhost: 53416 /". When you run the application, you may see a different port number.

6. if you use Ctrl + F5 (non-debug mode) to enable the application, you can change the code in this mode. After saving the file, refresh the browser, you do not need to restart the application to view the code changes. Many developers prefer to use non-debug mode to quickly start the application and view changes. See. In the figure, 1 is the content to be displayed on the page at startup. I modified it in the program code. 2. Press F5 in the browser to refresh the page, as shown in 3.

7. Use the default template to createRazorMvcBooks ProjectThere are three pages: Home, About, and Contact. Depending on the size of the browser window, you may need to click the menu icon to display the relevant menu link. For example.

8. Test link. InRazorMVCBooks pageClick the About link. For example. .

3. project files and folders

The following table lists the files and folders in the project. For example. In this tutorial, understanding the startup. cs file is the most important.


File or folder



A folder for storing static files. Some files include CSS style sheets, images, and JavaScript files used by the website.


This folder mainly contains Razor page files. To. CshtmlThe file ending with Razor is. CsThe ending file is a C # class file.

Appsettings. json

Json-format files for application configuration

Program. cs

Entry point for ASP. NET Core applications

Startup. cs

Configure the service and request pipeline to process all requests to the application.

4. File description in the Page folder

The _ layout. cshtml file contains the general layout of HTML elements (scripts and style sheets) and applications. For example, when you clickRazorMvcBooks, Home, About links, you will see the same header and footer layout.

The _ viewstart. cshtml file is used as the layout attribute of the _ layout. cshtml file.

_ Viewimports. cshtml contains the Razor command imported to each Razor page.

The _ validationscriptspartial. cshtml file provides references to the jQuery verification script. When we add and edit a webpage, The _ validationscriptspartial. cshtml file will be used.

About. cshtml, Contact. cshtml, and Index. cshtml are basic pages used to start applications.

Error. cshtml is used to display Error messages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.