DEV Community

Cover image for CodeBehind 4.5 Released; Advanced Async Capability
Elanat Framework
Elanat Framework

Posted on

CodeBehind 4.5 Released; Advanced Async Capability

As we mentioned in previous articles, version 4.5 is the latest version of the CodeBehind framework based on .NET 7, and the next version, 4.6, is developed directly on .NET 10 by the Elanat team.

In these versions, the framework structure has been completely rewritten based on async to be more responsive and perform faster than before.

Elanat's CodeBehind Framework is a full-stack, open-source (MIT licensed) framework for ASP.NET Core, introduced in 2023, with the main goal of combining the simplicity and familiarity of Web Forms (using .aspx files and the Code-Behind pattern) with the power and modern architecture of ASP.NET Core. By introducing WebForms Core as a modern, ViewStateless technology, this framework allows for direct server-side UI management. It also offers the ability to add features dynamically without recompiling the application with its modular architecture. This framework is a competitor to the default ASP.NET frameworks, filling the gap left by Microsoft's former WebForms in .NET Core by solving its problems and presenting itself as a flexible and high-performance alternative for developers looking for greater simplicity and reduced routing complexity.

CodeBehind framework 4.5

In addition to adding the async structure, in this version we have done significant work to increase response performance to make the framework faster than before.

Changes in the structure of controllers

Previously, controllers were defined as follows:

public partial class home : CodeBehindController
{
    public void PageLoad(HttpContext context)
    {
...
Enter fullscreen mode Exit fullscreen mode

But depending on the type of framework configuration, the new structure of controllers is different:

1- Controller configuration in Route
In this case, you can use async Task instead of void automatically:

public async Task PageLoad(HttpContext context)
{
...
Enter fullscreen mode Exit fullscreen mode

Here you need to configure "Program.cs" using the app.UseCodeBehindRouteAsync(); middleware.

2- Default configuration (first-view MVC based on aspx pages)

In this case, to perform async processing in the controller, you must use the await keyword and specify it in the view controller metadata section.

Razor syntax example:

@page
@controller await MyController
<!DOCTYPE html>
<html>
...
Enter fullscreen mode Exit fullscreen mode

Standard syntax example:

<%@ Page Controller="await MyController" %>
<!DOCTYPE html>
<html>
...
Enter fullscreen mode Exit fullscreen mode

Here you need to configure "Program.cs" using the app.UseCodeBehindAsync(); middleware.

Practical example

View

@page
@controller await MyController
<h1>Test Async</h1>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • This view is defined using Razor syntax.
  • @controller await MyController specifies that the MyController should run asynchronously (async).

Controller

using CodeBehind;

public partial class MyController : CodeBehindController
{
    public async Task PageLoad(HttpContext context)
    {
        string result = await DoSomethingAsync();
        Write(result);
    }

    private async Task<string> DoSomethingAsync()
    {
        await Task.Delay(2000);

        return "Hello CodeBehind! (after async work)";
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • MyController inherits from CodeBehindController.
  • The PageLoad method is now async and returns a Task, not void.
  • Inside PageLoad, the DoSomethingAsync method is called, and we await its result.
  • DoSomethingAsync simulates an asynchronous operation (Task.Delay(2000) waits for 2 seconds) and then returns a string.
  • Write(result) prints the result to the view.

In other words: This controller processes data asynchronously and then displays it on the page, without blocking the entire application.

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCodeBehind();

var app = builder.Build();

app.UseCodeBehindAsync();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • In Program.cs, the CodeBehind service is added to the application.
  • app.UseCodeBehindAsync(); tells the app to handle controllers asynchronously.
  • The application is then started with app.Run().

Together, these three components demonstrate how CodeBehind 4.5 processes a web page asynchronously, improving performance and responsiveness without blocking the app.

Summary

CodeBehind 4.5 brings full async support to both route-based and ASPX-based MVC controllers, improving responsiveness and performance. Developers can now implement async operations easily using await without adding complexity to their code.

Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

Get CodeBehind from NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind

Top comments (0)