This week I was focusing more in learn about how to use and program full-stack web applications with the ASP.NET Framework with C# via the CLI console.
Building the application
As I covered in the last week, ASP.NET is Microsoft’s product for competing with other web technologies like NodeJS Express and Java Spring Boot.
To start using this product, you need to download the .NET SDK for your OS, not only Windows is supported but also macOS and Linux.
Once you have the proper tools, the command to start the application is:
>dotnet new mvc
And you can add some scaffolding code, like a simple but robust authentication system along with the SQLite database for the application with the option –auth.
You can read more about those commands in the official documentation for dotnet CLI.
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new-sdk-templates#web-options
As you read before, we are creating an MVC application, with the controllers and models made with C#, the Front-End by default is in something called CSHTML, but you can use another language for the Front-End like Angular, React, or Vue.
Those steps can be done more easily with the Visual Studio IDE, but I like to study how to do it via the CLI to have a better perspective of what’s going on behind the scenes.
This was a wonderful experience and opened the doors to make a Docker container for the application.
Scaffolding models
One of the benefits of using .NET is that you have access to generated code and save you time in development.
But you need to understand those tools, and the proper commands to make them work.
For example, generating a new model-view-controller is crucial in MVC applications.
This can be done via the command of:
>dotnet-aspnet-codegenerator
With this, you only need to write a need model with the properties that you need, and then autogenerate all the code for the rest of the application.
Inclusive it autogenerates the views for the CRUD operations!
More of this can be read in the documentation:
Migrations and Object Relational Mapper
When I was studying ASP.NET this was a whole new topic for me. I always made the databases and the SQL statements myself, but with migrations, you can auto-generate these steps.
This is done via the dotnet Entity Framework and the command:
>dotnet ef migrations add <name>
Once you run that command with the respective flags it will auto-generate everything, the only step left is to update the database:
>dotnet ef database update
This was extremely useful and saved you a lot of time, I’ll continue to study more characteristics of the migrations with dotnet.