Hướng dẫn học mvc asp.net để làm dự án

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
  • 11/16/2023

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 ().

Prerequisites

  • with the ASP.NET and web development workload.
    Hướng dẫn học mvc asp.net để làm dự án
  • Visual Studio Code
  • C# for Visual Studio Code (latest version)
  • .NET 8.0 SDK

The Visual Studio Code instructions use the .NET CLI for ASP.NET Core development functions such as project creation. You can follow these instructions on macOS, Linux, or Windows and with any code editor. Minor changes may be required if you use something other than Visual Studio Code.

  • Visual Studio 2022 for Mac (latest version)
    • In Visual Studio 2022 for Mac, select Tools > Preferences... > Preview Features and enable Use the .NET 8 SDK if installed (requires restart). Important Microsoft has announced the retirement of Visual Studio for Mac. Visual Studio for Mac will no longer be supported starting August 31, 2024. Alternatives include:
    • Visual Studio Code with the C# Dev Kit and related extensions, such as .NET MAUI and Unity.
    • Visual Studio IDE running on Windows in a VM on Mac.
    • Visual Studio IDE running on Windows in a VM in the Cloud. For more information, see Visual Studio for Mac retirement announcement.

Create a web app

  • 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

    dotnet dev-certs https --trust

    0 for Project name. It's important to name the project MvcMovie. Capitalization needs to match each

    dotnet dev-certs https --trust

    1 when code is copied.
  • Select Next.
  • In the Additional information dialog:
    • Select .NET 8.0 (Long Term Support).
    • Verify that Do not use top-level statements is unchecked.
  • Select Create.

Hướng dẫn học mvc asp.net để làm dự án

For more information, including 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 .

  • Select New Terminal from the Terminal menu to open the integrated terminal.
  • Change to the directory (

    dotnet dev-certs https --trust

  • that will contain the project.
  • Run the following commands:

    dotnet new mvc -o MvcMovie code -r MvcMovie

    The

    dotnet dev-certs https --trust

    3 command creates a new ASP.NET Core MVC project in the MvcMovie folder. The

    dotnet dev-certs https --trust

    4 command opens the MvcMovie project folder in the current instance of Visual Studio Code.

Visual Studio Code might display a dialog box that asks: Do you trust the authors of the files in this folder?

  • If you trust all files in the parent folder, select Trust the authors of all files in the parent folder.
  • Select Yes, I trust the authors since the project folder has files generated by .NET.
  • When Visual Studio Code requests that you add assets to build and debug the project, select Yes. If Visual Studio Code doesn't offer to add build and debug assets, select View > Command Palette and type "

    dotnet dev-certs https --trust

    5" into the search box. From the list of commands, select the

    dotnet dev-certs https --trust

    6 command.

Visual Studio Code adds a

dotnet dev-certs https --trust

7 folder with generated

dotnet dev-certs https --trust

8 and

dotnet dev-certs https --trust

9 files.

  • Select File > New Project....
  • Select Web and Console > App > Web Application (Model-View-Controller) > Continue.
  • In the Configure your new Web Application (Model-View-Controller) dialog:
    • Select .NET 8.0 for the Target Framework.
    • Verify that Do not use top-level statements is unchecked.
  • Select Continue.
  • Enter

    dotnet dev-certs https --trust

    0 for Project name. It's important to name the project MvcMovie. Capitalization needs to match each

    dotnet dev-certs https --trust

    1 when code is copied.
  • Select Create.

Run the app

  • 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 học mvc asp.net để làm dự án
    Select Yes if you trust the IIS Express SSL certificate. The following dialog is displayed:
    Hướng dẫn học mvc asp.net để làm dự án
    Select Yes if you agree to trust the development certificate. For information on trusting the Firefox browser, see .

Visual Studio runs the app and opens the default browser.

The address bar shows

dotnet new mvc -o MvcMovie
code -r MvcMovie

3 and not something like

dotnet new mvc -o MvcMovie
code -r MvcMovie

3. The standard hostname for your local computer is

dotnet new mvc -o MvcMovie
code -r MvcMovie

4. 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.