ASP.NET Core New BackEnd Framework! CodeBehind 2.5 Released
Tags: csharp, dotnet, backend, news
Published: 2024-05-20 19:36:54

[CodeBehind](https://github.com/elanatframework/Code_behind) is a new back-end framework based on ASP.NET Core. [CodeBehind](https://elanat.net/page_content/code_behind) is owned by the [Elanat team](https://elanat.net) and competes with Microsoft's default web frameworks (Razor Pages, MVC, and Blazor) on .NET Core.

CodeBehind is simpler, more flexible, and much faster than Microsoft's default web frameworks. According to the latest tests of the Elanat team, CodeBehind is up to 40% faster than Microsoft's Razor and MVC pages.

**The link below is an article on the stunning performance of the CodeBehind framework by the Elanat team.**
[Performance test, ASP.NET Core MVC and Razor Pages vs CodeBehind Framework in version 2.2](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/performance_test_asp_dot_net_core_mvc_and_razor_pages_vs_code_behind_framework_version_2.2.md)

**If you want to know more about modularity in CodeBehind framework, please read the article below.**
[Modularity in the default mode](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/modularity_in_the_default_mode.md)

![CodeBehind Framework VS ASP.NET Core MVC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em83g88pc3xpzyonre9o.jpg)

Every project based on CodeBehind automatically supports modularity; it is only necessary to distribute the DLLs in the bin folder and the View pages in the root of the currently active project.

# CodeBehind 2.5

![CodeBehind 2.5 on NuGet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lofuz925suwgkvy5ap9r.png)

In this version, several middleware classes have been added to make [CodeBehind](https://www.nuget.org/packages/CodeBehind/) configuration much easier than before. From now on, you can configure the CodeBehind framework version 2.5 and later according to the following codes in ASP.NET Core:

CodeBehind 2.5 Configure in ASP.NET Core (Program.cs class)
```csharp
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.UseCodeBehind();

app.Run();
```

The above configuration is based on the default MVC architecture in CodeBehind, where requests are first routed to the View, and then the controller and model are specified from the View. This is a revolutionary architecture presented by the Elanat team.

You can leave error handling to CodeBehind automatically by adding the boolean value true to the UseCodeBehind method.

Example:
```csharp
app.UseCodeBehind(true);
```

> Note: Please note that you must specify the error page in the options file located in the `code_behind` directory.

You can also use the controller configuration in Route. The Elanat team has also transformed the structure of this type of configuration with its initiatives, and you don't need to do complex configurations.

The configuration of the controller in Route for the CodeBehind framework is done as follows:

```csharp
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.UseCodeBehindRoute();

app.Run();
```

Adding the UseCodeBehindRoute method will section each section of the URL.

> Note: Section means the strings between slash characters.
> Example: `example.com/section0/section1/section2`

The UseCodeBehindRoute method sets the section value to 0; if there is a Controller with the same name as the value of the first section, the UseCodeBehindRoute method will execute the Controller.

Example:

CodeBehind Controller
```csharp
using CodeBehind;

namespace YourProjectName
{
    public partial class home : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            Write("Route work fine");
        }
    }
}
```

According to the Controller class above, if the path `example.com/home` is requested, the Controller class above is executed and the `Route work fine` string is returned.

If the name of the Controller class matches the section in the url, Regardless of the namespace, the Controller class is executed.

The Controller class name is case sensitive. Therefore, the path `example.com/Home` cannot execute the Controller with the class name home.

This process is modular, so if you have an external dll that contains the Controller class of the CodeBehind framework, you can copy it to `wwwroot/bin` to call the Controller class.

Unlike the weak default structure of MVC in ASP.NET Core, the process of executing the Controller is dynamic and there is no need to create methods with `IActionResult` output; as a result, instead of a hard connection and full dependency, you will have a loose connection and little dependency.

Example:

Dynamic Controller in CodeBehind
```csharp
using CodeBehind;

namespace YourProjectName
{
    public partial class home : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            if (Section.Count() < 2)
            {
                Write("This is main page");
                return;
            }

            switch (Section.GetValue(1))
            {
                case "first": View("/page1.aspx"); break;
                case "second": View("/page2.aspx"); break;
                case "third": View("/page3.aspx"); break;
                case "fourth": View("/page4.aspx"); break;
            }
        }
    }
}
```

The code above shows a Controller that returns the string `This is main page` if there is no other section after section main. If another section named first, second, third, and fourth is requested after the main section, the pages `page1.aspx`, `page2.aspx`, `page3.aspx` and `page4.aspx` will be returned respectively.

Example:

Requesting `example.com/main/first` returns the page `page1.aspx`.

You can leave error handling to CodeBehind automatically by adding the boolean value true to the UseCodeBehindRoute method.

Example:
```csharp
app.UseCodeBehindRoute(true);
```

> Note: Please note that you must specify the error page in the options file located in the `code_behind` directory.

**Which ASP.NET Core framework should we choose?**

![ASP.NET Core framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo6imlfkf4izun1mgj26.png)

Please write us the answer to the above question in the comments section.

### Related links

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

CodeBehind in NuGet:
https://www.nuget.org/packages/CodeBehind/

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