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:

one method which is , this gives us the ability to put our own custom services into ASP.NET Core, and then have those services injected into pages, and controllers, and anywhere we need them.

And then the other method ASP.NET Core will invoke on the Startup class is the method. The Configure method is going to establish what middleware will execute for each incoming HTTP message.

In this article we are going through some of basic concepts of ASP.NET Core.

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‑page: tag helper changes the href on an anchor tag to point to a specific page in the system.
  • 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‑ equals and then some value. And what asp‑route can do is figure out how it can pass 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 that application is going to use. And that would be the method AddDbContext in ConfigureServices method.

There’s actually two of these here, and , which is new for this version of ASP.NET Core, and I would suggest using AddDbContextPool. This will pool our DbContexts in an attempt to reuse DbContexts that have been created while the application is alive, which can lead to better performance and scalability.

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.

One of the most important difference between ViewComponent and Razor page that, ViewComponent doesn’t responds to HTTP request. It resides in Razor page or Partial View. To use the ViewComponent, we need to register the it _ViewImports are tagHelpers.

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 . 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.

The execution of middleware in the pipeline is determined by the order in which we have added middleware components from top to bottom in code. There are 4 methods exposed by IApplicationBuilder, which allow to write basic middleware components inline and how to control the order of that middleware on your pipeline.

  • Run : can be used if we’re going to terminate the chain in middleware.
  • 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.

ASP. NET Core comes with the following middleware components built in, authentication, CORS, routing, session, and static files.

HttpHandler: what is commonly referred to as the endpoint, is a process which runs a to a being made to your ASP. NET application. Common HTTP handlers is the one that processes ASPX files and another could be used to emit RSS feeds.

HttpModules could be seen as points in the road which the request visits to reach the handler. Modules can short circuit the processing of a request.

Both HttpHandler and Modules can be migrated to ASP.Net Core Middleware.

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.

A good way to do it is to add these configurations with a secret manager tool. Right‑click the project and select Manage User Secrets. This will also edit the file, but that file will only be available on this machine and will not be checked into source control. To add secrete manager file by command line:

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