Skip to content

Latest commit

 

History

History
327 lines (183 loc) · 13.4 KB

File metadata and controls

327 lines (183 loc) · 13.4 KB
title author description ms.author ms.date no-loc uid
Get started with ASP.NET Core MVC
rick-anderson
Learn how to get started with ASP.NET Core MVC.
riande
10/16/2019
Blazor
Identity
Let's Encrypt
Razor
SignalR
tutorials/first-mvc-app/start-mvc

Get started with ASP.NET Core MVC

By Rick Anderson

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

[!INCLUDE consider RP]

This tutorial teaches the basics of building an ASP.NET Core MVC web app.

The app manages a database of movie titles. You learn how to:

[!div class="checklist"]

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

At the end, you have an app that can manage and display movie data.

[!INCLUDE]

Prerequisites

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]


Create a web app

  • From the Visual Studio select Create a new project.

  • Select ASP.NET Core Web Application and then select Next.

new ASP.NET Core Web Application

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

    new ASP.NET Core Web Application

  • Select Web Application(Model-View-Controller), and then select Create.

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

Visual Studio used the default template for the MVC project you just created. You have a working app right now by entering a project name and selecting a few options. This is a basic starter project.

The tutorial assumes familarity with VS Code. See Getting started with VS Code and Visual Studio Code help for more information.

  • Open the integrated terminal.

  • Change directories (cd) to a folder which 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.

    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

  • Confirm the following configurations:

    • Target Framework set to .NET Core 3.1.
    • Authentication set to No Authentication.

    Select Next.

    macOS .NET Core 3.1 selection

  • Name the project MvcMovie, and then select Create.

    macOS name the project


Run the app

Select Ctrl-F5 to run the app in non-debug mode.

[!INCLUDE]

  • Visual Studio starts IIS Express and runs the app. Notice that the address bar shows localhost:port# and not something like example.com. That's because localhost is the standard hostname for your local computer. When Visual Studio creates a web project, a random port is used for the web server.

  • Launching the app with Ctrl+F5 (non-debug mode) allows you to make code changes, save the file, refresh the browser, and see the code changes. Many developers prefer to use non-debug mode to quickly launch the app and view 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

Press Ctrl+F5 to run without the debugger.

[!INCLUDE]

Visual Studio Code starts Kestrel, launches a browser, and navigates to https://localhost:5001. The address bar shows localhost:port:5001 and not something like example.com. That's because localhost is the standard hostname for local computer. Localhost only serves web requests from the local computer.

Launching the app with Ctrl+F5 (non-debug mode) allows you to make code changes, save the file, refresh the browser, and see the code changes. Many developers prefer to use non-debug mode to refresh the page and view changes.

Home or Index page

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. That's because localhost is the standard hostname for your local computer. 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

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

[!INCLUDE consider RP]

This tutorial teaches the basics of building an ASP.NET Core MVC web app.

The app manages a database of movie titles. You learn how to:

[!div class="checklist"]

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

At the end, you have an app that can manage and display movie data.

[!INCLUDE]

Prerequisites

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]


Create a web app

  • From the Visual Studio select Create a new project.

  • Select ASP.NET Core Web Application and then select Next.

new ASP.NET Core Web Application

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

    new ASP.NET Core Web Application

  • Select Web Application(Model-View-Controller), and then select Create.

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

Visual Studio used the default template for the MVC project you just created. You have a working app right now by entering a project name and selecting a few options. This is a basic starter project, and it's a good place to start.

The tutorial assumes familarity with VS Code. See Getting started with VS Code and Visual Studio Code help for more information.

  • Open the integrated terminal.

  • Change directories (cd) to a folder which 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.

    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.

  • In the Configure your new ASP.NET Core Web API dialog, accept the default Target Framework of .NET Core 2.2.

    macOS .NET Core 2.2 selection

  • Name the project MvcMovie, and then select Create.


Run the app

Select Ctrl-F5 to run the app in non-debug mode.

[!INCLUDE]

  • Visual Studio starts IIS Express and runs the app. Notice that the address bar shows localhost:port# and not something like example.com. That's because localhost is the standard hostname for your local computer. When Visual Studio creates a web project, a random port is used for the web server.

  • Launching the app with Ctrl+F5 (non-debug mode) allows you to make code changes, save the file, refresh the browser, and see the code changes. Many developers prefer to use non-debug mode to quickly launch the app and view 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

  • Select Accept to consent to tracking. This app doesn't track personal information. The template generated code includes assets to help meet General Data Protection Regulation (GDPR).

    Home or Index page

    The following image shows the app after accepting tracking:

    Home or Index page

Press Ctrl+F5 to run without the debugger.

[!INCLUDE]

Visual Studio Code starts Kestrel, launches a browser, and navigates to https://localhost:5001. The address bar shows localhost:port:5001 and not something like example.com. That's because localhost is the standard hostname for local computer. Localhost only serves web requests from the local computer.

Launching the app with Ctrl+F5 (non-debug mode) allows you to make code changes, save the file, refresh the browser, and see the code changes. Many developers prefer to use non-debug mode to refresh the page and view changes.

  • Select Accept to consent to tracking. This app doesn't track personal information. The template generated code includes assets to help meet General Data Protection Regulation (GDPR).

    Home or Index page

    The following image shows the app after accepting tracking:

    Home or Index page

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. That's because localhost is the standard hostname for your local computer. 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.

  • Select Accept to consent to tracking. This app doesn't track personal information. The template generated code includes assets to help meet General Data Protection Regulation (GDPR).

    Home or Index page

    The following image shows the app after accepting tracking:

    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