AspNetCore.Docs/aspnetcore/tutorials/first-mvc-app/start-mvc.md

13 KiB

title author description ms.author ms.date no-loc uid ms.custom
Get started with ASP.NET Core MVC rick-anderson Learn how to get started with ASP.NET Core MVC. riande 01/20/2021
Home
Privacy
Kestrel
appsettings.json
ASP.NET Core Identity
cookie
Cookie
Blazor
Blazor Server
Blazor WebAssembly
Identity
Let's Encrypt
Razor
SignalR
tutorials/first-mvc-app/start-mvc contperf-fy21q3

Get started with ASP.NET Core MVC

By Rick Anderson

::: moniker range=">= aspnetcore-5.0"

[!INCLUDE consider RP]

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:

[!div class="checklist"]

  • 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

Visual Studio

[!INCLUDE]

Visual Studio Code

[!INCLUDE]

Visual Studio for Mac

[!INCLUDE]


Create a web app

Visual Studio

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

Create a new ASP.NET Core web application

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.

Visual Studio Code

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.

Visual Studio for Mac

  • Select File > New Solution.

    macOS New solution

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

    macOS web app template selection

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

    macOS name the project


Run the app

Visual Studio

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

    [!INCLUDE]

    Visual Studio:

    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:

Debug menu

You can debug the app by selecting the IIS Express button

IIS Express

The following image shows the app:

Home or Index page

Visual Studio Code

  • Select Ctrl+F5 to run without the debugger.

    [!INCLUDE]

    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.

    Home or Index page

Visual Studio for Mac

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

    [!INCLUDE]

    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:

Home or Index page


[!INCLUDE]

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

[!div class="step-by-step"] Next: Add a controller

::: moniker-end

::: moniker range="< aspnetcore-5.0"

[!INCLUDE consider RP]

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:

[!div class="checklist"]

  • 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

Visual Studio

[!INCLUDE]

Visual Studio Code

[!INCLUDE]

Visual Studio for Mac

[!INCLUDE]


Create a web app

Visual Studio

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

  • Select ASP.NET Core Web Application > Next.

    Create a new ASP.NET Core Web Application project

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

    Configure your new project

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

    New project dialog, .NET Core in left pane, ASP.NET Core web

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

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

Visual Studio Code

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.

Visual Studio for Mac

  • Select File > New Solution.

    macOS New solution

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

    macOS web app template selection

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

    macOS name the project


Run the app

Visual Studio

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

    [!INCLUDE]

    Visual Studio:

    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:

Debug menu

You can debug the app by selecting the IIS Express button

IIS Express

The following image shows the app:

Home or Index page

Visual Studio Code

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

    [!INCLUDE]

    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.

    Home or Index page

Visual Studio for Mac

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

[!INCLUDE]

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:

Home or Index page


[!INCLUDE]

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

[!div class="step-by-step"] Next

::: moniker-end