How ASP.NET Core works

An ASP.NET Core app is essentially a .NET app with a Program.cs file that sets up the web app component features you need and gets it running.

The most basic ASP.NET Core app’s Program.cs file:

C#Copy

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

With the previous code:

  • A basic ASP.NET Core web application is set up that listens for HTTP GET requests at the root URL (“/”) and responds with “Hello World!”.
  • The app is initialized, configures a single route, and starts the web server.

Blazor

You can build interactive web UI with ASP.NET Core using Blazor. Blazor is a component-based web UI framework integrated with ASP.NET Core, used for building interactive web UIs using HTML, CSS, and C#.

A reusable Blazor component, such as the following Counter component is defined in a Counter.razor file:

razorCopy

@page "/counter"
@rendermode InteractiveServer

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

With the previous code:

  • A component is created that displays a counter.
  • The @code block contains the component’s logic using C#, including a method to increment the counter.
  • The counter value is displayed and updated each time the button is clicked.
  • A component approach allows for code reuse across different parts of the application and has the flexibility to be run either in the browser or on the server in a Blazor app.

The Counter component can be added to any web page in the app by adding the <Counter /> element.

razorCopy

@page "/"

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

<Counter />
it security training courses malaysia

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *