Hướng dẫn asp.ne core

In the ASP.NET Core series, we are going to go through a detailed example of how to use .NET Core, Angular, and MySQL for ASP.NET Core web application development.

If someone asks: “Why this combination of technologies”, without getting in too much detail, the answer would be:

  • The technologies are free of charge
  • Applications can be deployed on both Windows and Linux OS
  • Production-grade performance
  • … And because we can 🙂

What are we going to do in this guide?

We are going to use MySQL as our database. First, we are going to install the MySQL server, create tables, and populate them with some data.

Then, we are going to step into the world of ASP.NET Core Web API development. It is going to be our server-side part of the application. As we progress through the ASP.NET Core series, we are going to use repository pattern, generics, LINQ, entity framework core, create more projects and services to demonstrate some good practices. Overall we will try to write the application as we would in the real-time environment. Furthermore, you will learn about ASP.NET Core architecture and code organization, so you can make it more readable and maintainable.

There are three approaches to using Entity Framework: Database First, Code First, and Model First. In this tutorial, we are going to use the Database First approach, because we want to create our database prior to typing the .NET code. This approach is good when you know the structure of your database beforehand, and we get to use the visual editor which makes the creation of relationships between tables much easier.

After we finish the ASP.NET Core series, we are going to introduce three of the most popular client frameworks (Angular, React, or Vue.js) to consume our Web API. This will result in creating a full-stack web application.

In the end, we are going to publish our app on both Windows and Linux OS, and finish strong by completing the entire development cycle.

Prerequisites:

  • MySQL server installed on your machine (installation guide)
  • .NET SDK
  • Visual Studio 2019 if you are using .NET 5 (installation guide) or Visual Studio 2022 for .NET 6

Background

  • Basic C# knowledge
  • Intermediate C# knowledge

Basic ASP.NET Core Web API:

  • Creating a Database for Our Project
  • Basic Code Preparations
  • Custom Logging in ASP.NET Core
  • Repository Pattern with Entity Framework Core
  • Using Repository for GET Requests
  • Using Repository for POST, PUT and DELETE Requests

Advanced ASP.NET Core Web API Concepts:

  • Paging
  • Filtering
  • Searching
  • Sorting
  • Data Shaping
  • HATEOAS

ASP.NET Core Application Configuration:

  • Basic Configuration Concepts
  • Options Pattern
  • Options Validation
  • Configuration Providers
  • Creating a Custom Configuration Provider
  • Securing Sensitive Data Locally
  • Azure Key Vault- Securing Data in Production

Consuming ASP.NET Core Web API:

  • Consuming ASP.NET Core Web API With Angular
  • Blazor WebAssembly HttpClient – Consuming a Web API
  • A Few Great Ways to Consume RESTful API in C#
  • Using C# and DalSoft.RestClient to Consume Any REST API

Hosting ASP.NET Core Web API on different platforms:

  • Hosting ASP.NET Core Web API on Windows with IIS

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Get started with ASP.NET Core MVC

  • Bài viết
  • 08/30/2022
  • 14 phút để đọc

Trong bài viết này

By Rick Anderson

This tutorial teaches ASP.NET Core MVC web development with controllers and views. If you're new to ASP.NET Core web development, consider the Razor Pages version of this tutorial, which provides an easier starting point. See Choose an ASP.NET Core UI, which compares Razor Pages, MVC, and Blazor for UI development.

This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views.

At the end of the series, you'll have an app that manages and displays movie data. You learn how to:

  • Create a web app.
  • Add and scaffold a model.
  • Work with a database.
  • Add search and validation.

View or download sample code (how to download).

Prerequisites

Create a web app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • Start Visual Studio and select Create a new project.
  • In the Create a new project dialog, select ASP.NET Core Web App (Model-View-Controller) > Next.
  • In the Configure your new project dialog, enter MvcMovie for Project name. It's important to name the project MvcMovie. Capitalization needs to match each namespace when code is copied.
  • Select Next.
  • In the Additional information dialog, select .NET 6.0 (Long-term support).
  • Select Create.

Hướng dẫn asp.ne core

For alternative approaches to create the project, see Create a new project in Visual Studio.

Visual Studio uses the default project template for the created MVC project. The created project:

  • Is a working app.
  • Is a basic starter project.

The tutorial assumes familiarity with VS Code. For more information, see Getting started with VS Code and Visual Studio Code help.

  • Open the integrated terminal.

  • Change to the directory (cd) that will contain the project.

  • Run the following command:

    dotnet new mvc -o MvcMovie
    code -r MvcMovie
    
    • If a dialog box appears with Required assets to build and debug are missing from 'MvcMovie'. Add them?, select Yes

    • dotnet new mvc -o MvcMovie: Creates a new ASP.NET Core MVC project in the MvcMovie folder.

    • code -r MvcMovie: Loads the MvcMovie.csproj project file in Visual Studio Code.

For Visual Studio for Mac, see the .NET 5 version of this tutorial.

Run the app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • Select Ctrl+F5 to run the app without the debugger.

    Visual Studio displays the following dialog when a project is not yet configured to use SSL:

    Hướng dẫn asp.ne core

    Select Yes if you trust the IIS Express SSL certificate.

    The following dialog is displayed:

    Hướng dẫn asp.ne core

    Select Yes if you agree to trust the development certificate.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

Visual Studio runs the app and opens the default browser.

The address bar shows localhost:port# and not something like example.com. The standard hostname for your local computer is localhost. When Visual Studio creates a web project, a random port is used for the web server.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.
  • Save the file.
  • Quickly refresh the browser and see the code changes.

You can launch the app in debug or non-debug mode from the Debug menu:

Hướng dẫn asp.ne core

You can debug the app by selecting the MvcMovie button in the toolbar:

Hướng dẫn asp.ne core

The following image shows the app:

Hướng dẫn asp.ne core

  • Select Ctrl+F5 to run without the debugger.

    • Trust the HTTPS development certificate by running the following command:

      dotnet dev-certs https --trust
      

      The preceding command doesn't work on Linux. See your Linux distribution's documentation for trusting a certificate.

      The preceding command displays the following dialog, provided the certificate was not previously trusted:

      Hướng dẫn asp.ne core

    • Select Yes if you agree to trust the development certificate.

      See Trust the ASP.NET Core HTTPS development certificate for more information.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

    Visual Studio Code:

    • Starts Kestrel
    • Launches a browser.
    • Navigates to https://localhost:5001.

    The address bar shows localhost:port:5001 and not something like example.com. The standard hostname for your local computer is localhost. Localhost only serves web requests from the local computer.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.

  • Save the file.

  • Quickly refresh the browser and see the code changes.

    Hướng dẫn asp.ne core

For Visual Studio for Mac, see the .NET 5 version of this tutorial.

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac

Visual Studio help

  • Learn to debug C# code using Visual Studio
  • Introduction to the Visual Studio IDE

Visual Studio Code help

  • Getting started

  • Debugging

  • Integrated terminal

  • Keyboard shortcuts

    • macOS keyboard shortcuts
    • Linux keyboard shortcuts
    • Windows keyboard shortcuts

Visual Studio for Mac help

  • Visual Studio for Mac Tour
  • Introducing Visual Studio for Mac

In the next tutorial in this series, you learn about MVC and start writing some code.

This tutorial teaches ASP.NET Core MVC web development with controllers and views. If you're new to ASP.NET Core web development, consider the Razor Pages version of this tutorial, which provides an easier starting point. See Choose an ASP.NET Core UI, which compares Razor Pages, MVC, and Blazor for UI development.

This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views.

At the end of the series, you'll have an app that manages and displays movie data. You learn how to:

  • Create a web app.
  • Add and scaffold a model.
  • Work with a database.
  • Add search and validation.

View or download sample code (how to download).

Prerequisites

Create a web app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • Start Visual Studio and select Create a new project.
  • In the Create a new project dialog, select ASP.NET Core Web Application > Next.
  • In the Configure your new project dialog, enter MvcMovie for Project name. It's important to name the project MvcMovie. Capitalization needs to match each namespace matches when code is copied.
  • Select Create.
  • In the Create a new ASP.NET Core web application dialog, select:
    • .NET Core and ASP.NET Core 5.0 in the dropdowns.
    • ASP.NET Core Web App (Model-View-Controller).
    • Create.

Hướng dẫn asp.ne core

For alternative approaches to create the project, see Create a new project in Visual Studio.

Visual Studio used the default project template for the created MVC project. The created project:

  • Is a working app.
  • Is a basic starter project.

The tutorial assumes familiarity with VS Code. For more information, see Getting started with VS Code and Visual Studio Code help.

  • Open the integrated terminal.

  • Change to the directory (cd) that will contain the project.

  • Run the following command:

    dotnet new mvc -o MvcMovie
    code -r MvcMovie
    
    • If a dialog box appears with Required assets to build and debug are missing from 'MvcMovie'. Add them?, select Yes

    • dotnet new mvc -o MvcMovie: Creates a new ASP.NET Core MVC project in the MvcMovie folder.

    • code -r MvcMovie: Loads the MvcMovie.csproj project file in Visual Studio Code.

  • Select File > New Solution.

    Hướng dẫn asp.ne core

  • In Visual Studio for Mac earlier than version 8.6, select .NET Core > App > Web Application (Model-View-Controller) > Next. In version 8.6 or later, select Web and Console > App > Web Application (Model-View-Controller) > Next.

    Hướng dẫn asp.ne core

  • In the Configure your new Web Application dialog:

    • Confirm that Authentication is set to No Authentication.
    • If an option to select a Target Framework is presented, select the latest 5.x version.
    • Select Next.
  • Name the project MvcMovie, and then select Create.

    Hướng dẫn asp.ne core

Run the app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • Select Ctrl+F5 to run the app without the debugger.

    Visual Studio displays the following dialog when a project is not yet configured to use SSL:

    Hướng dẫn asp.ne core

    Select Yes if you trust the IIS Express SSL certificate.

    The following dialog is displayed:

    Hướng dẫn asp.ne core

    Select Yes if you agree to trust the development certificate.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

    Visual Studio:

    • Starts IIS Express.
    • Runs the app.

    The address bar shows localhost:port# and not something like example.com. The standard hostname for your local computer is localhost. When Visual Studio creates a web project, a random port is used for the web server.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.
  • Save the file.
  • Quickly refresh the browser and see the code changes.

You can launch the app in debug or non-debug mode from the Debug menu item:

Hướng dẫn asp.ne core

You can debug the app by selecting the IIS Express button

Hướng dẫn asp.ne core

The following image shows the app:

Hướng dẫn asp.ne core

  • Select Ctrl+F5 to run without the debugger.

    • Trust the HTTPS development certificate by running the following command:

      dotnet dev-certs https --trust
      

      The preceding command doesn't work on Linux. See your Linux distribution's documentation for trusting a certificate.

      The preceding command displays the following dialog, provided the certificate was not previously trusted:

      Hướng dẫn asp.ne core

    • Select Yes if you agree to trust the development certificate.

      See Trust the ASP.NET Core HTTPS development certificate for more information.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

    Visual Studio Code:

    • Starts Kestrel
    • Launches a browser.
    • Navigates to https://localhost:5001.

    The address bar shows localhost:port:5001 and not something like example.com. The standard hostname for your local computer is localhost. Localhost only serves web requests from the local computer.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.

  • Save the file.

  • Quickly refresh the browser and see the code changes.

    Hướng dẫn asp.ne core

  • Select Run > Start Without Debugging to launch the app.

    Visual Studio for Mac:

    • Starts Kestrel server.
    • Launches a browser.
    • Navigates to http://localhost:port, where port is a randomly chosen port number.

    Visual Studio for Mac displays the following popup:

    Hướng dẫn asp.ne core

    Select Yes if you trust the development certificate.

    The following dialog is displayed:

    Hướng dẫn asp.ne core

    Enter your password and select OK

    Select Yes if you agree to trust the development certificate.

    See Trust the ASP.NET Core HTTPS development certificate for more information

    The address bar shows localhost:port# and not something like example.com. The standard hostname for your local computer is localhost. When Visual Studio creates a web project, a random port is used for the web server.

You can launch the app in debug or non-debug mode from the Run menu.

The following image shows the app:

Hướng dẫn asp.ne core

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac

Visual Studio help

  • Learn to debug C# code using Visual Studio
  • Introduction to the Visual Studio IDE

Visual Studio Code help

  • Getting started

  • Debugging

  • Integrated terminal

  • Keyboard shortcuts

    • macOS keyboard shortcuts
    • Linux keyboard shortcuts
    • Windows keyboard shortcuts

Visual Studio for Mac help

  • Visual Studio for Mac Tour
  • Introducing Visual Studio for Mac

In the next part of this tutorial, you learn about MVC and start writing some code.

This tutorial teaches ASP.NET Core MVC web development with controllers and views. If you're new to ASP.NET Core web development, consider the Razor Pages version of this tutorial, which provides an easier starting point. See Choose an ASP.NET Core UI, which compares Razor Pages, MVC, and Blazor for UI development.

This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views.

At the end of the series, you'll have an app that manages and displays movie data. You learn how to:

  • Create a web app.
  • Add and scaffold a model.
  • Work with a database.
  • Add search and validation.

View or download sample code (how to download).

Prerequisites

Create a web app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • From the Visual Studio, select Create a new project.

  • Select ASP.NET Core Web Application > Next.

    Hướng dẫn asp.ne core

  • Name the project MvcMovie and select Create. It's important to name the project MvcMovie so when you copy code, the namespace will match.

    Hướng dẫn asp.ne core

  • Select Web Application(Model-View-Controller). From the dropdown boxes, select .NET Core and ASP.NET Core 3.1, then select Create.

    Hướng dẫn asp.ne core

Visual Studio used the default project template for the created MVC project. The created project:

  • Is a working app.
  • Is a basic starter project.

The tutorial assumes familiarity with VS Code. For more information, see Getting started with VS Code and Visual Studio Code help.

  • Open the integrated terminal.

  • Change directories (cd) to a folder that will contain the project.

  • Run the following command:

    dotnet new mvc -o MvcMovie
    code -r MvcMovie
    
    • A dialog box appears with Required assets to build and debug are missing from 'MvcMovie'. Add them?, select Yes.

    • dotnet new mvc -o MvcMovie: Creates a new ASP.NET Core MVC project in the MvcMovie folder.

    • code -r MvcMovie: Loads the MvcMovie.csproj project file in Visual Studio Code.

  • Select File > New Solution.

    Hướng dẫn asp.ne core

  • In Visual Studio for Mac earlier than version 8.6, select .NET Core > App > Web Application (Model-View-Controller) > Next. In version 8.6 or later, select Web and Console > App > Web Application (Model-View-Controller) > Next.

    Hướng dẫn asp.ne core

  • In the Configure your new Web Application dialog:

    • Confirm that Authentication is set to No Authentication.
    • If an option to select a Target Framework is presented, select the latest 3.x version.
    • Select Next.
  • Name the project MvcMovie, and then select Create.

    Hướng dẫn asp.ne core

Run the app

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac
  • Select Ctrl+F5 to run the app without debugging.

    Visual Studio displays the following dialog when a project is not yet configured to use SSL:

    Hướng dẫn asp.ne core

    Select Yes if you trust the IIS Express SSL certificate.

    The following dialog is displayed:

    Hướng dẫn asp.ne core

    Select Yes if you agree to trust the development certificate.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

    Visual Studio:

    • Starts IIS Express.
    • Runs the app.

    The address bar shows localhost:port# and not something like example.com. The standard hostname for your local computer is localhost. When Visual Studio creates a web project, a random port is used for the web server.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.
  • Save the file.
  • Quickly refresh the browser and see the code changes.

You can launch the app in debug or non-debug mode from the Debug menu item:

Hướng dẫn asp.ne core

You can debug the app by selecting the IIS Express button

Hướng dẫn asp.ne core

The following image shows the app:

Hướng dẫn asp.ne core

  • Select Ctrl+F5 to run the app without debugging.

    • Trust the HTTPS development certificate by running the following command:

      dotnet dev-certs https --trust
      

      The preceding command doesn't work on Linux. See your Linux distribution's documentation for trusting a certificate.

      The preceding command displays the following dialog, provided the certificate was not previously trusted:

      Hướng dẫn asp.ne core

    • Select Yes if you agree to trust the development certificate.

      See Trust the ASP.NET Core HTTPS development certificate for more information.

    For information on trusting the Firefox browser, see Firefox SEC_ERROR_INADEQUATE_KEY_USAGE certificate error.

    Visual Studio Code:

    • Starts Kestrel
    • Launches a browser.
    • Navigates to https://localhost:5001.

    The address bar shows localhost:port:5001 and not something like example.com. The standard hostname for your local computer is localhost. Localhost only serves web requests from the local computer.

Launching the app without debugging by selecting Ctrl+F5 allows you to:

  • Make code changes.

  • Save the file.

  • Quickly refresh the browser and see the code changes.

    Hướng dẫn asp.ne core

  • Select Run > Start Without Debugging to launch the app.

    Visual Studio for Mac: starts Kestrel server, launches a browser, and navigates to http://localhost:port, where port is a randomly chosen port number.

Visual Studio for Mac displays the following popup:

Hướng dẫn asp.ne core

Select Yes if you trust the development certificate.

The following dialog is displayed:

Hướng dẫn asp.ne core

Enter your password and select OK

Select Yes if you agree to trust the development certificate.

See Trust the ASP.NET Core HTTPS development certificate for more information

The address bar shows localhost:port# and not something like example.com. The standard hostname for your local computer is localhost. When Visual Studio creates a web project, a random port is used for the web server. When you run the app, you'll see a different port number.

You can launch the app in debug or non-debug mode from the Run menu.

The following image shows the app:

Hướng dẫn asp.ne core

  • Visual Studio
  • Visual Studio Code
  • Visual Studio for Mac

Visual Studio help

  • Learn to debug C# code using Visual Studio
  • Introduction to the Visual Studio IDE

Visual Studio Code help

  • Getting started

  • Debugging

  • Integrated terminal

  • Keyboard shortcuts

    • macOS keyboard shortcuts
    • Linux keyboard shortcuts
    • Windows keyboard shortcuts

Visual Studio for Mac help

  • Visual Studio for Mac Tour
  • Introducing Visual Studio for Mac

In the next part of this tutorial, you learn about MVC and start writing some code.

Phản hồi

Gửi và xem ý kiến phản hồi dành cho