8 Important Reasons Why CodeBehind Framework Is Better Than ASP.NET Core
Tags: dotnet, csharp, dotnetcore, backend
Published: 2023-12-04 18:00:35

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

**What is CodeBehind framework?**

[CodeBehind](https://elanat.net/page_content/code_behind) is a new framework based on .NET Core version 7.0. The [CodeBehind framework](https://github.com/elanatframework/Code_behind) inherits all the features of .NET Core and gives it more simplicity and flexibility. CodeBehind framework is owned by [Elanat](https://elanat.net).

**CodeBehind is .NET diamond!**

Please read this article to the end and if you think we have made a mistake somewhere, share it with us in the comments.
We welcome .NET programmers to review the CodeBehind framework and compare it to the default structure of cshtml pages in ASP.NET Core.

## 1- CodeBehind is simpler than ASP.NET Core

Model and controller in ASP.NET Core are complex and difficult to learn. Usually, the difficulty of learning and the complexity of development in ASP.NET Core is one of the negative points that are mentioned in articles that compare back-end frameworks.

Here is an example controller in the CodeBehind framework:

```csharp
using CodeBehind;

namespace YourProjectName
{
    public partial class DefaultController : CodeBehindController
    {
        public DefaultModel model = new DefaultModel();
        public void PageLoad(HttpContext context)
        {
            model.BodyValue = "Hello World!";

            View(model);
        }
    }
}
```

In ASP.NET Core, several methods such as `OnGet()`, `OnPost()` and `Index()` with return values to IActionResult cause confusion. While in CodeBehind there is only one PageLoad method without return. You don't need to return the view and just put the created instance of the model in the view method.

In ASP.NET Core, there is a complication in naming cshtml files and having the same name as controller and view methods (one of the worst ideas in programming!); but in CodeBehind this complexity is not there.

You can easily create MVC in CodeBehind framework.
The following example is an aspx page that has introduced the model and controller in the page attributes section.

View (aspx file)
```diff
@page
+@controller YourProjectName.DefaultController
+@model YourProjectName.DefaultModel
<!DOCTYPE html>
<html>
...
```

In the CodeBehind framework, views have priority over controllers and models. But that doesn't mean you can't call different views! You can add any view in any section of another view.

Add a page in the view section:
```diff
...
<body>
+@LoadPage("/header.aspx")
...
</body>
...
```
Add a page in the controller section:
```csharp
CodeBehindExecute execute = new CodeBehindExecute();
model.HeaderValue = execute.Run(context, "/header.aspx");
```

Note: Adding a page in the model section is the same as the controller section.

To replace pages, you can create an empty view, or in the controller, you can set the IgnoreViewAndModel attribute to false to make the page empty.

Example:
```csharp
IgnoreViewAndModel = true;
CodeBehindExecute execute = new CodeBehindExecute();
Write(execute.Run(context, "/main.aspx"));
```

In the CodeBehind framework (like ASP.NET Core) you can easily create a headless system.

## 2- CodeBehind is modular
Every project built under the CodeBehind framework is modular, and the project itself is a module!

To add a new module to the current project, just create a new project under CodeBehind (no need to set the Program.cs file) and compile it; then it is enough to add the dll file of the new project in the `wwwroot/bin` path in the current project and place its aspx files in the desired path in `wwwroot`. only this!

![A project created under CodeBehind](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0l6dptesfwcf1ijsl06.png)

Implementing a modular structure in ASP.NET Core is complex.

## 3- CodeBehind is faster than ASP.NET Core
According to our latest tests, the CodeBehind framework is faster than the default cshtml framework in ASP.NET Core.

Interestingly, the superiority of CodeBehind over the default structure of ASP.NET Core is not a linear graph, and the higher the number of requests over time, the greater the graph of superiority is drawn towards CodeBehind.

This test was done in CodeBehind framework version 1.5.2. In version 1.5.2, we could not create pages in view only.
We admit right here that we made a mistake in determining the random interval and instead of 10 pages, 9 different pages are called (this has no role in the results and was announced only for your information).

You can see how to test and the results on this page:
[Performance test in only view section in version 1.5.2 (ASP.NET Core VS CodeBehind)](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/performance_test_in_only_view_section_version_1.5.2.md)

In the future, we will retest the latest version of CodeBehind with ASP.NET Core.

## 4- In CodeBehind you have access to the output
You can get the output of any aspx page and add it after editing in any other page.
In the CodeBehind framework, you can add the output of aspx pages or add values directly to the output of Response in ASP.NET Core.

## 5- New ideas and initiatives
The [Elanat team](https://elanat.net/) is constantly adding new ideas to the CodeBehind framework.

**Change the view path**
Set the view path wherever you like. You can put the views in the `wwwroot` directory or a directory outside of root.
You don't need to put the views in the `Shared` directory! Add views wherever you like and remove the aspx page from the direct access list by adding the break and islayout attributes.

Don't worry about the new aspx files in the wwwroot folder; it is transferred automatically!

The possibility of rewriting aspx files as a directory path (by removing the aspx extension).

One of the interesting ideas of the Elanat team for the CodeBehind framework is the possibility of rewriting the path of aspx files as a directory name. If you enable this option, paths leading to an aspx file without the aspx extension will be treated as the name of a directory.

No need to add an underline before the cshtml file name! Just add a file with the extension astx (external template) and add its path to the layout attribute in the page attributes section.

In a revolutionary initiative, the Elanat team provides a return templating structure that guarantees 100% separation of server-side code from the design part (like html).

![Return template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl78auoia55ib4h54034.png)

One of the initiatives of the Elanat team in the CodeBehind framework is to add support for the CodeBehind constructor method for models and controllers. You can open parentheses in front of the model and controller class names and add the desired input arguments that you created in the CodeBehind constructor method.

## 6- You have more choices in CodeBehind than ASP.NET Core
Write in Razor (`@Razor`) or standard (`<%=standard%>`) syntax, you're free.
You can only use razor syntax in ASP.NET Core.
You will have full access to the HttpContext and can edit it in any section.
Build the pages as you like; MVC or model-view or controller-view or only view.
If you want, you can play the aspx pages in the root of the application or keep them in a path outside the root.
In CodeBehind, you can run web parts that make changes to aspx files. You can edit all aspx files during project execution and responding to users.

It is true that there are several free and open source CMS such as [Umbraco](https://umbraco.com) and [Orchard Core](https://orchardcore.net) under ASP.NET Core; but even though CodeBehind is new, it comes with [Elanat](https://elanat.net) powerful CMS.

## 7- CodeBehind is completely based on the Code-Behind pattern
Even for loops and conditionals, you don't need to add server-side code in the view section.
You can use the HtmlData class in CodeBehind to stick with the View section based on the Code-Behind template.

Example for option tag:

View
```html
<select name="framework">
    @model.FrameworkOptionTags
</select>
```

Controller
```csharp
using CodeBehind;
using CodeBehind.HtmlData;

namespace YourProjectName
{
    public partial class DefaultController : CodeBehindController
    {
        public DefaultModel model = new DefaultModel();

        public void PageLoad(HttpContext context)
        {
            OptionTagCollection options = new OptionTagCollection();
            options.Add("code_behind", "CodeBehind", true);
            options.Add("asp_dot_net_core", "ASP.NET Core");
            options.Add("django", "Django");
            options.Add("laravel", "Laravel");
            options.Add("spring_boot", "Spring Boot");
            options.Add("ruby_on_rails", "Ruby on Rails");

            model.FrameworkOptionTags = options.GetString();

            View(model);
        }
    }
}
```

Result
```html
<select name="framework">
    <option value="code_behind" selected>CodeBehind</option>
    <option value="asp_dot_net_core">ASP.NET Core</option>
    <option value="django">Django<option>
    <option value="laravel">Laravel</option>
    <option value="spring_boot">Spring Boot</option>
    <option value="ruby_on_rails">Ruby on Rails</option>
</select>
```

Also you can also use return templates:
```html
<div class="page-list">
  @#Tags={<a href="@#Href">@#PageName</a>}
</div>


@#Tags{
@foreach (PageItem page in model.PageItemList)
{
    @#Tags
}
}

@#PageName{@page.Title}
@#Href{@(((page.Path == "main")? "/" : page.Path))}
```

## 8- With CodeBehind, you can easily create high-level technologies
In CodeBehind, you can add as many middleware as you want without having to compile the project from scratch.

At the time of .NET Core presentation, Microsoft provided many articles to introduce middleware and their usage.

The middleware in CodeBehind is flexible and dynamic. Just run an aspx page!

![Middleware in CodeBehind framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucce0vgk1fvnmwyqa85x.png)

The following resources show how to create high-level systems using the CodeBehind framework; please read these resources and think about implementing them in ASP.NET Core!

[How to create modular systems by CodeBehind framework?](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_create_modular_systems_by_code_behind_framework.md)

[How to create scheduled task system by CodeBehind framework?](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_create_scheduled_task_by_code_behind_framework.md)

[How to create startup system by CodeBehind framework?](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_create_startup_system_by_code_behind_framework.md)

[How to create dynamic middleware system by CodeBehind framework?](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_create_dynamic_middleware_by_code_behind_framework.md)

### Conclusion
The CodeBehind framework is a powerful alternative to default programming in ASP.NET Core. In the default structure of ASP.NET Core, a lot of attention is paid to solving errors, async programming, maintaining security and accessibility; but the CodeBehind framework focuses on maintaining simplicity, Improve performance, modular structure and new ideas.

### 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