Quick Start ASP. NET Core Read this article is enough

Time:2019-8-31

Originally this article just wanted to briefly introduce the ASP. NET Core MVC project (after all, to take care of a lot of new friends), but think of it as a bit fierce (considering the acuteness of friends), so that you can quickly start ASP. NET Core through the study of this article. Since it’s a quick start, I’ve gone through too much in-depth content here! Then in the following columns of articles slowly elaborate on the concepts.

What is. NET Core

Many friends see. NET Core as ASP. NET Core, in fact, this is a misunderstanding, because. NET Core is a common open source development platform (a “platform”), based on which we can develop applications such as ASP. NET Core, Windows 10 Universal Windows Platform (UWP), Tizen and so on, and I. Our series of tutorials is to develop ASP. NET Core applications with. NET Core. It is also maintained on GitHub by Microsoft’s official team and members of the. NET community. It is cross-platform (supporting Windows, macOS and Linux) and can be used to generate devices, clouds and IoT applications.

NET Core also has the following features:

  • Cross-platform: It can run on Windows, macOS and Linux operating systems.
  • Consistency across architectures: Running code with the same behavior across multiple architectures, including x64, x86, and ARM.
  • Command-line tools: Includes easy-to-use command-line tools for local development and continuous integration solutions.
  • Flexible deployment: Can be included in application or installed parallel user or computer scope. It can be used with Docker container.
  • Compatibility: NET Core is compatible with. NET Framework, Xamarin and Mono through. NET Standard.
  • Open Source: NET Core is an open source platform using MIT and Apache 2 licenses. NET Core is a. NET Foundation project.
  • Supported by Microsoft:.NET Core is supported by a strong Microsoft team.

What is ASP.NET Core

ASP.NET Core is a web framework created by Microsoft to build web applications, APIs and micro services. It uses common patterns, such as MVC (Model-View-Controller), dependency injection, and a request processing pipeline composed of middleware. It is open source based on Apache 2.0 license, that is, source code is freely available, and community members are welcome to contribute in the form of defect repair and new function submission.

ASP. NET Core runs on Microsoft’s. NET runtime library, similar to Java’s virtual machine (JVM) or Ruby’s interpreter. There are several languages (C Visual Basic, F) that can be used to write ASP. NET Core programs. C# is the most common choice, of course, most of me use C# for development. You can build and run ASP.NET Core applications on Windows, Mac, and Linux.

Why use ASP.NET Core to develop applications

Existing web framework options are numerous: Node/Express, Spring, Ruby on Rails, Django, Laravel, and so on. What are the advantages of ASP.NET Core? Why use ASP. NET Core to develop applications?

  • speedASP.NET Core is fast. Because. NET Core is compiled and run, execution speed is much faster than interpreted execution language, such as JavaScript or Ruby, ASP. NET Core has also been specially optimized for multi-threaded and asynchronous tasks. It’s normal to execute 5-10 times faster than code written with Node.js.
  • ecologyASP. NET Core may be new, but. NET has been tested for a long time. There are thousands of packages on NuGet (. NET) package management systems, such as npm, Ruby gems, or Maven. There are ready-made packages for JSON deserialization, database connection, PDF generation, or almost any requirement you can think of.
  • SecurityMicrosoft’s start-up team pays attention to security, and ASP. NET Core is secure from its foundation. It has automatically processed purified input data and cross-domain forgery requests (CSRF), so you don’t have to worry about that. You also have the advantage of static type checking in the. NET compiler, which acts like a constant vigilance and a review of obsessive-compulsive disorders. In this way, when using a variable or some data, those unconscious errors are inevitable.
  • cross platformIt can run on Windows, Mac or Linux with the. NET runtime library installed.
  • Open SourceNET Core is an open source (MIT license) offered by Microsoft to. NET Foundation in 2014. Now it is one of the most active. NET Foundation projects. It can be freely adopted by individuals and enterprises, including for personal, academic or commercial purposes. At the same time, open source means that you can read its source code to get solutions when you have problems, and you can also mention Issues on Gayhub.

Millions of developers have used (and will continue to use) ASP.NET 4.x to create Web applications. ASP.NET Core is a redesigned ASP.NET 4.x, which changes the architecture and forms a simpler modular framework.

ASP.NET Core also has the following advantages:

  • Generate a unified view of the Web UI and Web API.
  • Build for testability.
  • Razor Pages can make page-based coding simpler and more efficient.
  • It can be developed and run on Windows, macOS and Linux.
  • Open source and community-centric.
  • Integration of new client framework and development workflow.
  • Cloud Ready Configuration System Based on Environment.
  • Built-in dependency injection.
  • Lightweight, high performance, modular HTTP request pipeline.
  • Can be hosted on IIS, Nginx, Apache, Docker or self-hosted in their own processes.
  • Based on. NET Core runtime, parallel application version control can be used.

NET Core Environment Construction

Before continuing to write ASP. NET Core code, we need to install the running environment of. NET Core. In this part, we will go further to build the environment of. NET Core.

1. First, you can search. NET Core by Google. If you are right, the first is Microsoft’s official download address. Of course, you can click here to download it (the latest version of SDK is v2.1.500, and the latest version of runtime is v2.1.6).

2. Double-click on the SDK you downloaded and proceed to install it foolishly. The installation of Microsoft’s software is so simple that if I go into details, you will dislike me for being verbose. So I’ll just post a successful installation here.

3. Hold on nextShift + Right mouse buttonThen select “Open Powershell window here” or “Open command line window here”. Then enterdotnet --infoLook at the. NET Core information that we have installed, the current running environment, the version information that has been installed before, I have more versions, because it took me a long time. If you install it for the first time, there may be only one. The interface shown in the second figure below shows that our. net core development environment is ready! Let’s roll up our sleeves and start.

Quickly create an ASP.NET Core project and practice it

In order to take care of more small partners, I will not use CLI command line to create ASP. NET Core project, or use VS2017 in a regular way! What vs2017 needs activation code? Then you can use the community version! Community edition, learning is enough! There’s a lot of nonsense. Let’s get started.

The first step must be to open your VS 2017, and then click on the top left corner “File”, “New”, “Project” (or if you are in trouble, you can use it.)Ctrl+Shift+NThis shortcut key opens the following Create New Project dialog box, then select it as shown in the figure and click OK (what? Didn’t you succeed in creating it? Then I think you are insulting me:

2. Haha, clicking on the above image does not create a success, but will pop up the next dialog box, as shown below, he will let you choose the target framework is. NET Core or. NET Framework; choose to create an empty solution or create a web project with templates! As for their differences, interested friends can create each one and then compare their differences! Here we select the MVC web application as shown in the following figure:

3. After successful creation, we can see the following structure, the standard MVC structure, but it is different from the MVC in the. net framework era. Wwwroot: The static file directory of the website (why can it be loaded here? You can think about it first.

Appsettings.json: Configuration files, such as database connection strings, and so on.

Program. cs: Program entry file (there is a Main method);

Startup.cs Start Profile;

Dependencies: Installation, configuration, upgrade of third-party components on which management projects rely

Controller: Controller

Models: Entities

Views: Views

Because of the limited space, there is not much explanation.

4. Press the F5 of the keyboard or click the Run button as shown below to see the effect.

5. If nothing unexpected happens, you will see the interface shown in the following figure.

6. See no, it’s so simple that we run an ASP.NET Core MVC site. Is it over by now? You think a lot in Sao Nian, because I want you to know something more. So this sixth article is used to talk nonsense, and then as a partition.

7. Models: Right-click on the Models folder to create two new classes: a Content class and a ContentViewModel class. The code is as follows (I won’t teach you how to create classes here. If you tell me, it’s insulting your IQ):

namespace Sample01.Models
{
 /// <summary>
 /// 2018.11.19
 /// Zhu Lei
 /// Content entity
 /// </summary>
 public class Content
 {
  /// <summary>
  /// Primary key
  /// </summary>
  public int Id { get; set; }

  /// <summary>
  /// Title
  /// </summary>
  public string title { get; set; }
  /// <summary>
  /// Content
  /// </summary>
  public string content { get; set; }
  /// <summary>
  /// State 1 Normal 0 Delete
  /// </summary>
  public int status { get; set; }
  /// <summary>
  /// Creation time
  /// </summary>
  public DateTime add_time { get; set; }
  /// <summary>
  /// Modification time
  /// </summary>
  public DateTime modify_time { get; set; }
 }
}
namespace Sample01.Models
{
 /// <summary>
 /// 2018.11.19
 /// Zhu Lei
 /// Content View Mode
 /// </summary>
 public class ContentViewModel
 {
  /// <summary>
  /// Content List
  /// </summary>
  public List<Content> Contents { get; set; }
 }
}

8. Controller: The model is built, so let’s build a new controller and then create some simulated data. The code is as follows:

namespace Sample01.Controllers
{
 /// <summary>
 /// 2018.11.19
 /// Zhu Lei
 /// Content Controller
 /// </summary>
 public class ContentController : Controller
 {
  /// <summary>
  /// Home page display
  /// </summary>
  /// <returns></returns>
  public IActionResult Index()
  {
   var contents = new List<Content>();
   for (int i = 1; i < 11; i++)
   {
    Content. Add (new Content {Id = i, Title =$"{i} Title", content =$"{i} content", status = 1, add_time = DateTime. Now. AddDays (-i)});
   }
   return View(new ContentViewModel { Contents=contents});
  }
 }
}

9. Views: The model and controller have been built, so let’s build a view to show the data we created. There are many ways to create this view. Here we introduce a foolish way to create this view. Place the mouse in the index braces, then right-click the mouse to create the view. As shown below, you can create the view file in the / Views / Content / Index. cshtml file:

10. Let’s modify the View slightly according to the following code:

@model ContentViewModel
@using Humanizer;
@{
 ViewData ["Title"]= "Content List";
}

<div>
 <div>@ViewData["Title"]</div>

 <table>
  <thead>
   <tr>
    <td> <input type="checkbox"></td>
    <td> serial number</td>
    <td> Title</td>
    <td> Content</td>
    <td> Add time</td>
   </tr>
  </thead>

  @foreach (var item in Model.Contents)
  {
  <tr>
   <td>
    <input type="checkbox">
   </td>
   <td>@item.Id</td>
   <td>@item.title</td>
   <td>@item.content</td>
   <td>@item.add_time.Humanize()</td>

  </tr>
  }
 </table>
</div>

11. Then modify the layout file to be located atViews/Shared/_Layout.cshtmlThe layout file contains the “base” HTML of all views. This includes the navigation bar, which is displayed at the top of each page. In order to add new entries to the navigation bar, we need to add our Content column to this file as follows:


<li><a asp-area="" asp-controller="Content" asp-action="Index">Content</a></li>

12. The code is basically complete here. Press your F5 key and navigate to Content to see the effect.

13. It’s really over here!

source code

Do you think I will upload the source code? Sao Nian, what do you think, so simple, or knock it down by yourself! Don’t try to be a man with high eyes and low hands. In addition, if you follow the code step by step, you may not be able to run successfully, What? Because there’s a Nuget package in the view that you need to add by yourself, and I didn’t point out that the purpose is to solve it by yourself! Another long-winded sentence, don’t be a person with high eyes and low hands!

summary

Okay, it’s time to conclude. First of all, what is. NET Core? What is ASP. NET Core? Then take you step by step to configure the. NET Core development environment. Finally, with you step by step to create an ASP. NET Core MVC project, and at the same time through a practical operation to teach you how to display a list of Contents on the page. If you follow the landlord and knock the code bit by bit, then run! Then you will find that ASP. NET Core is so simple. What? Do you think it’s easy? So in the next article, the blogger will bring you some complicated concepts, such as what dependency injection, configuration file loading (analysis of the source code), and so on. So far, ASP. NET Core Quick Start is enough to read this article, and it’s finished satisfactorily.