Welcome to Thrill of .NET Core
Instructors
Table of contents
- Installing environment in VSCode
- Understanding project structure
- How HTTP requests work
- How MVC is working on the browser
- Creating models
- Understanding data annotations
- Scaffolding from the .NET Command Line Interface
- Working with NuGet packages from CLI
- Asynchronous programming
Installing environment in VSCode
Download VSCode
Visual Studio Code (VSCode) is a source code editor developed by Microsoft for Windows, Linux, macOS. It is free to download. You can download it from its official website. Download Link
Setup extension in VSCode
After successful installation click on widgets this icon from the left bar or
simply
press Ctrl+Shift+X
to go to extensions. In search bar type C# and an extension
will appear.
Install that extension. This extension will provide the following features inside VSCode -
- Lightweight development tools for .NET Core
- Great C# editing support including Syntax Highlighting, IntelliSense, Go to Definition, Find All References etc.
- Debugging support for .NET Core (CoreCLR)
- Support for project.json and csproj projects on Windows, Linux and macOS
You can also install another extension named as C# Extension. It will provide extensions to the IDE that will hopefully speed up your development workflow. Now you are good to go.
Project structure
Create a Project
Open the terminal from the folder or directory in which you want to create the project.
Lets say you want to create a project named musicPlayer. The following commnad will create a
project for you. dotnet new mvc -o musicPlayer
Then you need to change your directory to
that project and open it in VSCode. After opening the project you will see a notification on the right
bottom corner. Click on Yes button.
Project structure
- .csproj file
- Model
- View
- Controller
- wwwroot
Creating models
To create a model click new c# class from Model folder. A file will be creted in model folder with .cs extension. Example: abc.cs
Data annotation
What is Data Annotation?
Data annotations are nothing but certain validations that we put in our models to validate the input from
the user. ASP.NET MVC provides a unique feature in which we can validate the models using Data annotation
attribute. Import the following namespace to use data annotation in the application.
System.ComponentModel.DataAnnotations
It is very easy to use and the code becomes much cleaner as
compared to normal ASP.NET validators.
Types of data annotation
Data annotation | Description | Syntax |
---|---|---|
Required | This attribute specifies that the value is mandatory and cannot be skipped. | [Required(ErrorMessage="Please enter name"),MaxLength(30)] |
DataType | This attribute is used to specify the datatype of the model. | [DataType(DataType.Text)] |
Range | Using this attribute we can set a range between two numbers. | [Range(100,500,ErrorMessage="Please enter correct value")] |
StringLength | Using this attribute we can specify maximum and minimum length of the property. | [StringLength(30,ErrorMessage="Do not enter more than 30 characters")] |
DisplayName | Using this attribute we can specify property name to be displayed on view. | [Display(Name="Student Name")] |
MaxLength | Using this attribute we can specify maximum length of property. | [MaxLength(3)] |
Bind | This attribute specify fields to include or exclude for model binding. | [Bind(Exclude = "StudentID")] |
DisplayFormat | This attribute allows us to set date in the format specified as per the attribute. | [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")] |
RegularExpression | We can set a regex pattern for the property. For ex: Email ID. | [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid.")] |
Scaffolding from CLI
Why Scaffold?
Use the scaffolding tool to produce Create, Read, Update and Delete (CRUD) pages.
Scaffold code and parameters
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Parameter | Description |
---|---|
-m | The name of the model |
-dc | The data context |
-udl | Use the default layout |
-relativeFolderPath | The relative output folder path to create the files |
-useDefaultLayout | The default layout should be used for the views |
-referenceScriptLibraries | Adds _ValidationScriptPartial to Edit and Create pages |
Database Migration
After successful scaffolding you need EF Core Migration to create database. Migration is a set of
tools that let you create and update a database to match your data model. For migration use the
following commands - dotnet ef migrations add InitialCreate
dotnet ef database update
-
ef migrations add InitialCreate: Generates an Migrations/{timestamp}_InitialCreate.cs migration file. The InitialCreate argument is the migration name. Any name can be used, but by convention, a name is selected that describes the migration. Because this is the first migration, the generated class contains code to create the database schema. The database schema is based on the model specified in the MvcMovieContext class (in the Data/MvcMovieContext.cs file).
-
ef database update:
Updates the database to the latest migration, which the previous command created. This command runs the Up method in the Migrations/{time-stamp}_InitialCreate.cs file, which creates the database.