Category: Uncategorized

  • Using route templates

    When the user makes a request for a page from your web app, they can specify what they want to see with information in the URI. For example:

    http://www.contoso.com/pizzas/margherita?extratopping=pineapple

    After the protocol and website address, this URI indicates that the user wants to know about margherita pizzas. Also, the query string after the question mark shows that they’re interested in an extra topping of pineapple. In Blazor, you use routing to ensure that each request is sent to the component that can best respond. You also use routing to ensure that the component has all the information it needs to display what the user wants. In this case, you might want to send the request to the Pizzas component and for that component to display a margherita pizza with information about adding pineapple to it.

    Blazor routes requests with a specialized component called the Router component. The component is configured in App.razor like this:

    razorCopy

    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        </Found>
        <NotFound>
            <p>Sorry, we haven't found any pizzas here.</p>
        </NotFound>
    </Router>
    

    When the app starts, Blazor checks the AppAssembly attribute to find out which assembly it should scan. It scans that assembly for components that have the RouteAttribute present. Blazor uses these values to compile a RouteData object that specifies how requests are routed to components. When you code the app, you use the @page directive in each component to fix the RouteAttribute.

    In the preceding code, the <Found> tag specifies the component that handles the routing at runtime: the RouteView component. This component receives the RouteData object and any parameters from the URI or query string. It then renders the specified component and its layout. You can use the <Found> tag to specify a default layout, which is used when the selected component doesn’t specify a layout with the @layout directive. You learn more about layouts later in this module.

    In the <Router> component, you can also specify what is returned to the user when there isn’t a matching route, by using the <NotFound> tag. The preceding example returns a single <p> paragraph, but you can render more complex HTML. For example, it might include a link to the home page or a contact page for site administrators.

    Using the @page directive

    In a Blazor component, the @page directive specifies that the component should handle requests directly. You can specify a RouteAttribute in the @page directive by passing it as a string. For example, this attribute specifies that the page handles requests to the /Pizzas route:

    razorCopy

    @page "/Pizzas"
    

    If you want to specify more than one route to the component, use two or more @page directives, like in this example:

    razorCopy

    @page "/Pizzas"
    @page "/CustomPizzas"
    
    juniper networks training courses malaysia
  • Access platform features in Blazor Hybrid

    We’re building hybrid apps with .NET, which means we have access to all of the .NET class libraries. In addition to these APIs, building Blazor Hybrid apps with .NET MAUI not only allows you to deploy to multiple platforms, it also allows access to each platform’s native APIs. This means that if you need to integrate platform capabilities of iOS, Android, macOS, or Windows, you can do it all in C#. You can access these APIs directly from your Blazor components or create shared .NET MAUI class libraries.

    Platform integration

    Each platform that .NET MAUI supports offers unique operating system and platform APIs that you can access from C#. .NET MAUI provides cross-platform APIs to access much of this platform functionality, which includes access to sensors, accessing information about the device on which an app is running, checking network connectivity, storing data securely, and initiating browser-based authentication flows.

    .NET MAUI separates these cross-platform APIs into different areas of functionality:

    • Application Model: App functionality, including app actions, application information, opening the browser, opening URIs, opening maps, handling permissions, and version tracking
    • Communication: Access to contacts, email, networking, phone dialer, sms messaging, and web authentication
    • Device Features: Information and access to battery, display info, device info, sensors, flashlight, geocoding, geolocation, haptic feedback, and vibration
    • Media: Including media picker, screenshots, text to speech, and unit converters
    • Sharing: Including access to the clipboard and sharing files or text to other applications
    • Storage: APIs for picking files, file system helpers, preferences, and secure storage
    kubernetes training courses malaysia
  • Data binding and events in Blazor Hybrid

    You’ve defined the UI for your web app. Now, let’s explore how to add logic to the app. In a Blazor app, you can add C# code in separate .cs files or inline in your Razor components.

    C# inline in components

    It’s common practice to mix HTML and C# in a single Razor component file. For simple components with lighter code requirements, this approach works well. To add code into a Razor file, you’ll use Razor syntax.

    What are Razor directives?

    Razor directives are component markup used to add C# inline with HTML. With directives, developers can define single statements, methods, or larger code blocks.

    Code directives

    Code directives should be familiar to developers who have used Razor in MVC or Pages.

    You can use @expression() to add a C# statement inline with HTML. If you require more code, use the @code directive to add multiple statements enclosed by parentheses.

    You can also add an @functions section to the template for methods and properties. They’re added to the top of the generated class, where the document can reference them.

    lean it certification training courses malaysia
  • What is Blazor Hybrid?

    Companies that build web apps and client apps commonly hire developers for different roles. Some developers create back-end, server-side logic. Some build client-side web apps. Others build native-client apps for mobile and desktop platforms. These developers often use different development languages and technologies.

    C# and .NET are popular choices for building server-side logic. Client-side web apps are often built with web UI frameworks using JavaScript. When it comes to native-client apps for desktop and mobile, there are several options available, including many for .NET and C#. Using multiple languages and toolsets requires multiple sets of skills and often requires two separate teams. Also, code to transfer and represent data must be built in both languages and kept in sync. Blazor Hybrid can simplify your development team’s tasks, code, and processes by allowing you to use your existing skills and code in building web applications in C# and .NET to build native-client applications using these same technologies.

    In this unit, you’ll start with an introduction to Blazor Hybrid, .NET MAUI, and Razor Components.

    What is Blazor?

    Blazor apps are composed of reusable web UI components built using C#, HTML, and CSS. With Blazor, developers can build client and server code with C#. They can also share code and libraries with the front-end client code and back-end logic. Using C# for all code simplifies sharing data between the front end and back end, enables code reuse to accelerate development, and reduces maintenance.

    jboss enterprise application platform training courses malaysia
  • When to use Blazor

    Blazor is a fully featured web UI framework designed to handle the needs of most modern web apps. But whether Blazor is the right framework for you depends on many factors.

    You should consider using Blazor for web development if:

    • You’re looking for a highly productive full stack web development solution.
    • You need to deliver web experiences quickly without the need for a separate frontend development team.
    • You’re already using .NET, and you want to apply your existing .NET skills and resources on the web.
    • You need a high-performance and highly scalable backend to power your web app.
    iot training courses malaysia
  • How Blazor works

    Blazor provides many features to help you get started and deliver your next web app project fast. Let’s take a tour of the core capabilities of Blazor to help you decide whether you should use Blazor for your next great web app.

    Blazor components

    Blazor apps are built from components. A Blazor component is a reusable piece of web UI. A Blazor component encapsulates both its rendering and UI event handling logic. Blazor includes various built-in components for form handling, user input validation, displaying large data sets, authentication, and authorization. Developers can also build and share their own custom components, and many prebuilt Blazor components are available from the Blazor ecosystem.

    Use standard web technologies

    You author Blazor components using Razor syntax, a convenient mixture of HTML, CSS, and C#. A Razor file contains plain HTML and then C# to define any rendering logic, like for conditionals, control flow, and expression evaluation. Razor files are then compiled into C# classes that encapsulate the component’s rendering logic. Because Blazor components authored in Razor are just C# classes, you can call arbitrary .NET code from your components.

    UI event handling and data binding

    Interactive Blazor components can handle standard web UI interactions using C# event handlers. Components can update their state in response to UI events and adjust their rendering accordingly. Blazor also includes support for two-way data binding to UI elements as a way to keep component state in sync with UI elements.

    The following example is a simple Blazor counter component implemented in Razor. Most of the content is HTML, while the @code block contains C#. Every time the button is pressed the IncrementCount C# method is invoked, which increments the currentCount field, and then the component renders the updated value:

    razorCopy

    <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++;
        }
    }
    
    checkpoin certification malaysia
  • 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:

    • A 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