Essentials Flashcards
Which versions of the .NET SDK are currently installed in my computer?
You can see which versions of the .NET SDK are currently installed with a terminal. Open a terminal and run the following command.
dotnet –list-sdks
You get an output similar to:
3.1.424 [C:\program files\dotnet\sdk]
5.0.100 [C:\program files\dotnet\sdk]
6.0.402 [C:\program files\dotnet\sdk]
7.0.100 [C:\program files\dotnet\sdk]
Which versions of the .NET runtime are currently installed in my computer?
Run:
dotnet –list-runtimes
You get an output similar to:
Create a new project using command line instructions.
Step #1 Create a folder and a global.json file for the new project
PS C:\dev> dotnet new globaljson –sdk-version 7.0.401 –output FirstProject
The template “global.json file” was created successfully.
What is wrong with this command instruction?
PS C:\dev> dotnet new mvc –no-https –output FirstProject –framework net6.0.22
Error: Invalid option(s):
–framework net6.0.22
‘net6.0.22’ is not a valid value for –framework. The possible values are:
net6.0 - Target net6.0
net7.0 - Target net7.0
Create a new project using command line instructions.
Step #2 Create a new ASP.NET Core project using the MVC Framework
PS C:\dev> dotnet new mvc –no-https –output FirstProject –framework net6.0
The template “ASP.NET Core Web App (Model-View-Controller)” was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/6.0-third-party-notices for details.
Processing post-creation actions…
Restoring C:\dev\FirstProject\FirstProject.csproj:
Determining projects to restore…
Restored C:\dev\FirstProject\FirstProject.csproj (in 87 ms).
Restore succeeded.
Https is not required.
Create a new project using command line instructions.
Step #3 Create a solution file for your new project
PS C:\dev> dotnet new sln -o FirstProject
The template “Solution File” was created successfully.
Create a new project using command line instructions.
Step #4 Add your new project to the solution
PS C:\dev> dotnet sln FirstProject add FirstProject
Project FirstProject.csproj
added to the solution.
Create a new project using command line instructions.
PS C:\dev> dotnet new globaljson –sdk-version 7.0.401 –output FirstProject
PS C:\dev> dotnet new mvc –no-https –output FirstProject –framework net6.0
PS C:\dev> dotnet new sln -o FirstProject
PS C:\dev> dotnet sln FirstProject add FirstProject
What would be the layout of folders and files created for this new project?
A new solution file (.sln), a new project file (.csproj), a global file (global.json) and folders and a program file (.cs) created by choosing the MVC Framework template
Create a new project using command line instructions.
PS C:\dev> dotnet new globaljson –sdk-version 7.0.401 –output FirstProject
PS C:\dev> dotnet new mvc –no-https –output FirstProject –framework net6.0
PS C:\dev> dotnet new sln -o FirstProject
PS C:\dev> dotnet sln FirstProject add FirstProject
What would opening this solution look in VS 2022 Solution Explorer?
The project FirsProject is included as a C# project within the solution.
Which file determines which HTTP port ASP.NET Core will use to listen for HTTP requests?
launchSettings.json
Look at profiles section, applicationUrl setting!
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:5000", "sslPort": 0 } }, "profiles": { "FirstProject": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
iisSettings shows the same applicationUrl is not required
Run a project using the command line.
PS C:\dev\FirstProject> dotnet run
You can now browse the url for this project http://localhost:5000/
http://localhost:5000/ is defined in launchSettings.json profiles sectio
What is the content of the HomeController.cs file when first created?
HomeController.cs is created when using the MVC template for creating a projecty. It is located in the Controllers folder within the project.
using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using FirstProject.Models; namespace FirstProject.Controllers; public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }
Note: using Microsoft.AspNetCore.Mvc;
What is an endpoint?
An endpoint is an action written in C# and defined in a controller. In ASP.NET Core applications, incoming requests are handled by endpoints.
What is a route?
A route is rule that is used to define how a request is handled. When a project is first created, a default rule is also created to get started.
The ASP.NET Core routing system is responsible for selecting the endpoint that will handle an HTTP request.
Which component processes the contents of views and generates HTML that is sent to the browser?
Razor!
Razor is a view engine and the expressions in views are known as Razor expressions.
What is the standard naming convention for view files that allows Razor to locate a view?
Put view files in a folder whose name matches the controller that contains the action method.
Example: Views/Home folder, since the action method is defined by the Home controller.
HomeController.cs
~~~
public class HomeController : Controller
{
public ViewResult Index() { return View(“MyView”); }
}
~~~
How do you provide data to views?
Action methods provide data to views by passing arguments to the View method. The data provided to the view is known as the view model.
public class HomeController : Controller { public ViewResult Index() { int hour = DateTime.Now.Hour; string viewModel = hour < 12 ? "Good Morning!" : "Good Afternoon!"; return View("MyView", viewModel); } }
Note: return View(“MyView”, viewModel);
What is the difference between expressions @model and @Model?
@model string @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width"/> <title>Index</title> </head> <body> <div> @Model Hello World! (from the view) </div> </body> </html>
@model expression specifies the type of the view model.
@model string
The view modelo value is included in the HTML output using the @Model expression.
@Model Hello World! (from the view)
What happens when you execute the dotnet watch command? >dotnet watch
The comand watch runs a project and when you can make changes to the project, your code is automatically recompiled and any change is automatically displayed in the browser.
What happens when you make a mistake when the dotnet watch is running?
The dotnet watch command indicates that it cannot automatically update the browser. You will have to restart your application.
What is the convention to allocate the definitions of the data model classes?
The data model classes are defined by convention in a folder named Models which was automatically added by the project template when the project was first created.