Blog

  • What is Blazor?

    Blazor is a modern frontend web framework based on HTML, CSS, and C# that helps you build web apps faster. With Blazor, you build web apps using reusable components that can be run from both the client and the server so that you can deliver great web experiences. Blazor is part of .NET, a developer platform for building anything. .NET is free, open-source, and runs cross-platform.

    Some of the benefits of using Blazor include:

    • Build web UI fast with reusable components: Blazor’s flexible component model makes it easy to build reusable components that you can use to assemble apps quickly.
    • Add rich interactivity in C#: Handle arbitrary UI events from the browser and implement component logic all in C#, a modern type-safe language that is easy to learn and highly versatile.
    • One development stack: Build your entire web app from the frontend to the backend using a single development stack and share code for common logic on the client and server.
    • Efficient diff-based rendering: As components render, Blazor carefully tracks what parts of the DOM changed, so that UI updates are fast and efficient.
    • Server and client-side rendering: Render components from both the server and the client to implement various web app architectures and deliver the best possible web app experience.
    • Progressively enhanced server rendering: Use built-in support for enhanced navigation & form handling and streaming rendering to progressively enhance the user experience of server rendered web apps.
    • Interop with JavaScript: Use the ecosystem of JavaScript libraries and browser APIs from your C# code.
    • Integrate with existing apps: Integrate Blazor components with an existing MVC, Razor Pages, or JavaScript based apps.
    • Great tooling: Use Visual Studio or Visual Studio Code to get started in seconds and stay productive with great code editing support.
    • Web, mobile, and desktop: Blazor components can also be used to build native mobile & desktop apps using a hybrid of native and web, called Blazor Hybrid.
    cisco certification malaysia

  • When to use ASP.NET Core

    ASP.NET Core is a cross-platform, high-performance framework for building modern web applications. Whether ASP.NET Core is the right web development framework for you depends on many factors.

    When to use ASP.NET Core

    ASP.NET Core for web development is ideal when your web app has any of these requirements:

    • Rich user interfaces: You want to build interactive and dynamic web applications. With support for Blazor and popular front-end JavaScript frameworks, ASP.NET Core allows you to create rich user interfaces.
    • API development: You need to develop robust API services. ASP.NET Core supports both RESTful APIs and gRPC, providing flexibility for different communication needs.
    • Microservices architecture: You’re building a microservices-based architecture. ASP.NET Core’s lightweight and modular design is well-suited for microservices.
    • High performance: Your application demands high performance and scalability. ASP.NET Core is designed to handle high traffic and large-scale applications efficiently.
    • Modern development practices: You prefer modern development practices such as dependency injection, asynchronous programming, and modular architecture. ASP.NET Core supports these practices out of the box.
    • Cross-platform requirements: You need to develop applications that run on Windows, macOS, Linux and Docker. ASP.NET Core’s cross-platform capabilities make it an excellent choice for diverse environments.
    • Cloud integration: You plan to deploy your applications to the cloud. ASP.NET Core integrates seamlessly with Azure and other cloud platforms, simplifying deployment and management.
    • Security and compliance: You require strong security features and compliance with industry standards. ASP.NET Core provides built-in support for HTTPS, data protection, and other security best practices.
    citrix certification malaysia 2
  • Configuration

    ASP.NET Core supports accessing configuration data from a variety of sources, like JSON files, environment variables, and command-line arguments.

    Configuring a connection string in an appsetting.json file:

    JSONCopy

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"
      }
    }
    

    In the Program.cs file:

    C#Copy

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddDbContext<MyDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
    
    var app = builder.Build();
    
    app.Run();
    

    With the previous code:

    • The connection string is configured in the appsettings.json file.
    • The WebApplicationBuilder is used to configure the app.
    • The AddDbContext method registers the DbContext with the dependency injection container.
    • The connection string is retrieved from the configuration and used to set up the database context.
    comptia certification malaysia
  • 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 />
    

    APIs

    ASP.NET Core provides frameworks for building APIs, gRPC services, and real-time apps with SignalR to instantly push data updates to clients.

    Basic Minimal API:

    C#Copy

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    
    app.MapGet("/hello", () => "Hello, World!");
    
    app.Run();
    

    With the previous code:

    • A minimal API is set up that listens for HTTP GET requests at the /hello URL and responds with “Hello, World!”.
    • The WebApplicationBuilder is used to configure the app.
    • The MapGet method defines a route and a handler for GET requests.

    Middleware

    ASP.NET Core uses a pipeline of middleware components to handle HTTP requests and responses. This modular approach provides flexibility, allowing you to customize and extend your application’s functionality by adding or removing middleware components as needed.

    The middleware pipeline processes HTTP requests in a sequential manner, ensuring that each component can perform its designated task before passing the request to the next component in the pipeline.

    Adding built-in middleware in the Program.cs file:

    C#Copy

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    
    app.UseHttpsRedirection();
    
    app.UseRouting();
    
    app.MapStaticAssets();
    
    app.UseAuthentication();
    
    app.UseAuthorization();
    
    app.MapGet("/", () => "Hello World!");
    
    app.Run();
    

    In the previous code, several common middleware components were added:

    • UseHttpsRedirection: Redirects HTTP requests to HTTPS.
    • UseRouting: Enables routing to map requests to endpoints.
    • MapStaticAssets: Optimizes the delivery of static files such as HTML, CSS, JavaScript, images and other assets.
    • UseAuthentication: Adds authentication capabilities.
    • UseAuthorization: Adds authorization capabilities.
    • app.MapGet: This is a simple endpoint to demonstrate that the application is running.

    Dependency Injection

    ASP.NET Core includes built-in support for dependency injection (DI) for configuring services that are used by the app and its various framework components.

    For example, you might want to centrally configure a service using a framework like EntityFramework Core that other parts of your app depend on to access a database. You can configure a database context from EntityFramework Core as a service using dependency injection like this:

    C#Copy

    public class MyDbContext : DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
        
        public DbSet<Product> Products { get; set; } = default!;
    }
    
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddDbContext<MyDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
    
    var app = builder.Build();
    
    app.Run();
    

    With the previous code:

    • DbContext is configured as a service using dependency injection.
    • The WebApplicationBuilder is used to configure the app.
    • The AddDbContext method registers the DbContext with the dependency injection container.
    • The connection string is retrieved from the configuration and used to set up the database context.
    dell emc certification malaysia
  • What is ASP.NET Core?

    ASP.NET Core is a cross-platform, high-performance framework for building modern web applications. This open-source framework allows developers to create web applications, services, and APIs that can run on Windows, macOS, and Linux. It is built for large-scale app development and can handle any size workload, making it a robust choice for enterprise-level applications.

    Full stack web development

    ASP.NET Core is a full stack web framework that seamlessly integrates front-end and back-end development needs within a single consistent framework:

    • For front-end development, ASP.NET Core includes Blazor, a component-based web UI framework based on C# that supports both server-side rendering and client-side rendering via WebAssembly.
    • Alternatively, ASP.NET Core can be integrated with JavaScript front-end frameworks like Angular, React, and Vue.

    API development

    ASP.NET Core is a powerful framework for API development:

    • It supports creating JSON-based APIs, gRPC services, and real-time services using SignalR.
    • With built-in OpenAPI support, developers can easily generate and visualize API documentation, simplifying the design and consumption of APIs.
    • You can use ASP.NET Core to build back-end APIs for a variety of apps, including web apps and native mobile apps.

    Modular architecture

    ASP.NET Core’s modular architecture offers a flexible approach to modern web development:

    • This architecture includes support for dependency injection, middleware, configuration, and logging.
    • Middleware components can be configured to handle requests and responses, including built-in middleware for authentication, routing, session management, and static file serving.
    • The dependency injection design pattern enhances testability and maintainability.

    Security built-in

    ASP.NET Core helps you build secure applications thanks to its robust built-in security features for authentication and authorization. These features help applications manage user identities and protect sensitive data effectively.

    google cloud certification malaysia
  • Bind controls to data in Blazor applications

    Blazor lets you bind HTML controls to properties so that changing values are automatically displayed in the user interface (UI).

    Suppose you’re developing a page that collects information from customers about their pizza preferences. You want to load the information from a database and enable customers to make changes, such as recording their favorite toppings. When there’s a change from the user or an update in the database, you want the new values to display in the UI as quickly as possible.

    In this unit, you learn how to use data binding in Blazor to tie UI elements to data values, properties, or expressions.

    What is data binding?

    If you want an HTML element to display a value, you can write code to alter the display. You need to write extra code to update the display when the value changes. In Blazor, you can use data binding to connect an HTML element to a field, property, or expression. This way, when the value changes, the HTML element is automatically updated. The update usually happens quickly after the change, and you don’t have to write any update code.

    To bind a control, you would use the @bind directive:

    razorCopy

    @page "/"
    
    <p>
        Your email address is:
        <input @bind="customerEmail" />
    </p>
    
    @code {
        private string customerEmail = "user@contoso.com"
    }
    ibm certification malaysia
  • Share information by using cascading parameters

    Component parameters work well when you want to pass a value to the immediate child of a component. Things become awkward when you have a deep hierarchy with children of children and so on. Component parameters aren’t automatically passed to grandchild components from ancestor components or further down the hierarchy. To handle this problem elegantly, Blazor includes cascading parameters. When you set the value of a cascading parameter in a component, its value is automatically available to all descendant components to any depth.

    In the parent component, using the <CascadingValue> tag specifies the information that will cascade to all descendants. This tag is implemented as a built-in Blazor component. Any component rendered within that tag is able to access the value.

    razorCopy

    @page "/specialoffers"
    
    <h1>Special Offers</h1>
    
    <CascadingValue Name="DealName" Value="Throwback Thursday">
        <!-- Any descendant component rendered here will be able to access the cascading value. -->
    </CascadingValue>
    

    In the descendant components, you can access the cascading value by using component members and decorating them with the [CascadingParameter] attribute.

    razorCopy

    <h2>Deal: @DealName</h2>
    
    @code {
        [CascadingParameter(Name="DealName")]
        private string DealName { get; set; }
    }
    isaca certification malaysia
  • Share data in Blazor applications

    Blazor includes several ways to share information between components. You can use component parameters or cascading parameters to send values from a parent component to a child component. The AppState pattern is another approach you can use to store values and access them from any component in the application.

    Suppose you’re working on the new pizza delivery website. Multiple pizzas should be displayed on the home page in the same way. You want to display the pizzas by rendering a child component for each pizza. Now, you want to pass an ID to that child component that determines the pizza it displays. You also want to store and display a value on multiple components that shows the total number of pizzas you sold today.

    In this unit, you learn three different techniques you can use to share values between two or more Blazor components.

    Sharing information with other components by using component parameters

    In a Blazor web app, each component renders a portion of HTML. Some components render a complete page but others render smaller fragments of markup, such as a table, a form, or a single control. If your component renders only a section of markup, you must use it as a child component within a parent component. Your child component can also be a parent to other smaller components that render within it. Child components are also known as nested components.

    In this hierarchy of parent and child components, you can share information between them by using component parameters. Define these parameters on child components, and then set their values in the parent. For example, if you have a child component that displays pizza photos, you could use a component parameter to pass the pizza ID. The child component looks up the pizza from the ID and obtains pictures and other data. If you want to display many different pizzas, you can use this child component multiple times on the same parent page, passing a different ID to each child.

    Start by defining the component parameter in the child component. You define it as a C# public property and decorate it with the [Parameter] attribute:

    razorCopy

    <h2>New Pizza: @PizzaName</h2>
    
    <p>@PizzaDescription</p>
    
    @code {
        [Parameter]
        public string PizzaName { get; set; }
        
        [Parameter]
        public string PizzaDescription { get; set; } = "The best pizza you've ever tasted."
    }
    

    Because the component parameters are members of the child component, you can render them in your HTML by using Blazor’s reserved @ symbol, followed by their name. Also, the preceding code specifies a default value for the PizzaDescription parameter. This value is rendered if the parent component doesn’t pass a value. Otherwise, the value passed from the parent overrides it.

    You can also use custom classes in your project as component parameters. Consider this class that describes a topping:

    C#Copy

    public class PizzaTopping
    {
        public string Name { get; set; }
        public string Ingredients { get; set; }
    }
    

    You can use that as a component parameter in the same way as a parameter value to access individual properties of the class by using dot syntax:

    razorCopy

    <h2>New Topping: @Topping.Name</h2>
    
    <p>Ingredients: @Topping.Ingredients</p>
    
    @code {
        [Parameter]
        public PizzaTopping Topping { get; set; }
    }
    

    In the parent component, you set parameter values by using attributes of the child component’s tags. You set simple components directly. With a parameter based on a custom class, you use inline C# code to create a new instance of that class and set its values:

    razorCopy

    @page "/pizzas-toppings"
    
    <h1>Our Latest Pizzas and Topping</h1>
    
    <Pizza PizzaName="Hawaiian" PizzaDescription="The one with pineapple" />
    
    <PizzaTopping Topping="@(new PizzaTopping() { Name = "Chilli Sauce", Ingredients = "Three kinds of chilli.
    istqb certification malaysia

  • Access data from a Blazor component

    Engaging websites need to display dynamic content that might change all the time. Learning how to obtain data from a dynamic source, such as a database or web service, is a fundamental technique in web development.

    Suppose you’re working for a pizza delivery firm on its updated customer-facing website. You have a range of webpages laid out and designed as Blazor components. Now, you want to populate those pages with information about pizzas, toppings, and orders that you want to obtain from a database.

    In this unit, you learn how to access data and render it within HTML markup for display to the user.

    Creating a registered data service

    If you want to create a dynamic website that shows changing information to users, you must write code to get that data from somewhere. For example, suppose you have a database that stores all the pizzas your company sells. Because the pizzas are always changing, it’s a bad idea to hardcode them into the website HTML. Instead, use C# code and Blazor to query the database, and then format the details as HTML so that the user can pick their favorite.

    juniper certification malaysia
  • Create a user interface with Blazor components

    Blazor components let you define webpages or portions of HTML that include dynamic content by using .NET code. In Blazor, you can formulate dynamic content by using C#, instead of using JavaScript.

    Suppose you’re working for a pizza delivery company to create a new modern website. You’re starting with a welcome page that is going to become the landing page for most site users. You want to display special deals and popular pizzas on that page.

    In this unit, you learn how to create components in Blazor and write code that renders dynamic content on those components.

    Understand Blazor components

    Blazor is a framework that developers can use to create a rich interactive user interface (UI) by writing C# code. With Blazor, you can use the same language for all your code, both server-side and client-side. You can render it for display in many different browsers, including browsers on mobile devices.

    microsoft certification malaysia