ASP.NET Core : What I learned!

ASP.NET Core is the latest web framework from Microsoft, and it’s engineered to be fast, easy, and work across platforms.

Internals of ASP.NET Core

One of the most important part of ASP.NET core project is Startup.cs and what makes this class files so special: ASP.NET Core instantiate this class and invoke two methods:

1. aspnet-codegenerator :

If we aren’t using Visual Studio, the scaffolding shown in this clip is something you can also achieve with the dotnet-aspnet-codegenerator tool discussed in module 3. The command would look like:

dotnet aspnet-codegenerator controller -api -name ControllerName
--model ModelName --dataContext Data.Context

2. Tag Helpers:

Tag helper helps us to render HTML on the server. Tag helpers that Microsoft provides out of the box start with asp‑ or at least most of them do. Some of them are:

  • asp‑for: is a way of saying this input is for the following property that bind on the model with [BindProperty] attribute.
  • asp-route: It’s a dynamic tag helper. With another dash at end of this helper and then include the name of the parameter that we want to pass along. So in other words, if I wanted to pass a search term parameter to some page, then I could say asp‑route‑name equals and then some value. And what asp‑route can do is figure out how it can pass name to the other page, knowing what it knows about that page.
  • asp-items: asp‑items takes an IEnumerable of SelectListItem for dropdownlist. A SelectListItem is an object that describes one of the options in a select, so it has two significant properties.
  • asp‑validation‑for: will do is look in the ModelState, see if there’s any errors for property, and if so, display the associated error message for that particular error.
  • partial: Partial is a tag helper that appears as a full element, and when we use the partial tag helper, there’s a couple parameters that need to pass along, and can be passed those parameters in what looks like HTML attributes, which are name and model.
  • environment: is tag helper that, instead of looking like an HTML attribute, it looks like an HTML tag, but it is processed on the server, is look at the current operating environment and will only emit the HTML inside of here if a condition is met.

3. TempData Attribute:

When ASP.NET Core sees the TempData attribute, it’s going to go into the TempData structure and look for something with the key of Property Name, whatever this property name is. If it finds a value for that key in temp data, it’s going to take the value and assign it to this property assuming the types match. Example :

public string Message {get; set;}

4. Entity Framework:

The Entity Framework provides some methods that use to describe the DbContexts that application is going to use. And that would be the method AddDbContext in ConfigureServices method.

5. View Components:

View components are independent and autonomously go out and build whatever model is needed and render a piece of UI that can be placed into the layout view. In ASP.NET Core derives from a base class, which is the ViewComponent class.

6. Client-Side Validation:

ASP.NET Core supports a client side validation during editing of form controls to the page by adding RenderSection of _ValidationScriptPartial.cshtml which by default includes jquery‑validate and jquery‑validation‑unobtrusive. It’s all about the asp‑for tag helper working on the server side in conjunction with jquery‑validation on the client side.

7. Publishing the Self-Contained App:

To publish the Application as Self-Contained, we need to specify self-contained option with RID. RID is short for Runtime IDentifier. RID values are used to identify target platforms where the application runs. They’re used by .NET packages to represent platform-specific assets in NuGet packages. The following values are examples of RIDs: linux-x64, ubuntu.14.04-x64, win7-x64, or osx.10.12-x64. Example for OSx:

dotnet publish —runtime osx.10.11-x64 —-self-contained false

8. Database Migrations

To create migration with SQL Server database on start of the application the all migration need to executed so that, model entities and database object are in sync. This can be done in the Main method by passing IWebHost object.

private static void MigrateDatabase(IWebHost host)        
using(var scope = host.Services.CreateScope())
var db = scope

9. Middleware

Middleware components are pieces of code, which are added to application’s pipeline and have the job of handling each request and response.

  • Use : gives the option of passing the request onto the next component in the chain or handling the request itself terminating the chain and returning a response.
  • Map : The Map method simply matches the start of the request path against the string provided.
  • MapWhen: The MapWhen method allowing to pass a predicate in its parameter and execute in the branch based on the result of that predicate.

10. Secret Manager:

To configure our application, we put the configuration settings in appsettings.json. That is great, but it still isn’t great because that file is also checked into source control and that may exposes very important ID or secrets.

dotnet user-secrets init
dotnet user-secrets set "Google:ClientId" "SomeSpecialValue ..."

An Azurer, Web developer, Technologist, Writer, Poet, Runner. Opinions are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store