CodeBehind 2.0 is Released; ASP.NET Core Alternative
Tags: news, dotnet, csharp, backend
Published: 2023-12-23 22:28:22

### Is there an alternative to ASP.NET Core?

Yes, the [Elanat team's](https://elanat.net) [CodeBehind framework](https://elanat.net/page_content/code_behind) is a competitor to the default structure of cshtml pages in ASP.NET Core. The CodeBehind framework supports the C# programming language and still runs on .NET Core. The CodeBehind framework is modular and is faster than the default ASP.NET Core.

### Version 2.0
We at Elanat team did a lot of testing on the CodeBehind framework after the release of version 1.9 and fixed the existing defects before releasing version 2.0 so that version number 2.0 would be a stable version.

### ViewData in Controller and Model

One of the most important new features in CodeBehind framework version 2.0 is the ability to add values to ViewData through the controller and model.

Example of adding data to ViewData in the controller section:
```csharp
using CodeBehind;

namespace YourProjectName
{
    public partial class AboutPageController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            ViewData.Add("title", "About");
        }
    }
}
```

Example of receiving data from ViewData in the controller section:

Razor syntax
```html
@page
controller YourProjectName.AboutPageController
<!DOCTYPE html>
<html>
<head>
    <title>@ViewData.GetValue("title")</title>
</head>
<body>
...
```

Standard syntax
```html
<%@ Page Controller="YourProjectName.AboutPageController" %>
<!DOCTYPE html>
<html>
<head>
    <title><%=ViewData.GetValue("title")%></title>
</head>
<body>
...
```

### Download API

From version 2.0 onwards, it is possible to download files from the executive pages.

You can use the Download method to download files after users request them on the view pages; the Download method has an input argument of the file path and can be accessed from all three sections: view, controller, and model.

Example:
```csharp
Download("C:\\image.png");
```

### Global template

Global template are a new feature of the CodeBehind framework. All view pages call this template.

It is recommended to use only template blocks in the global format.

This template is called in `code_behind/global_template.astx` path.

### possibility of adding more templates

From now on, you can add more templates; It is enough to separate the paths of the templates with the semicolon character (;).

Razor syntax example:
```html
@page
@template "/page/template/template1.astx;/page/template/template2.astx;/page/template/template3.astx"
```

Standard syntax example:
```html
<%@ Page Template="/page/template/template1.astx;/page/template/template2.astx;/page/template/template3.astx" %>
```

### cshtml files

In the options file, we added an option to support cshtml files so that users of the CodeBehind framework can easily distinguish the codes of the view section with code highlighters.

Options file in CodeBehind 2.0
```diff
[CodeBehind options]; do not change order
view_path=wwwroot
move_view_from_wwwroot=true
rewrite_aspx_file_to_directory=false
access_aspx_file_after_rewrite=false
ignore_default_after_rewrite=true
start_trim_in_aspx_file=true
inner_trim_in_aspx_file=true
end_trim_in_aspx_file=true
set_break_for_layout_page=true
+convert_cshtml_to_aspx=false
```

You can code in these files with razor syntax and standard syntax.
cshtml files will be available with aspx extension after compilation.
For default cshtml pages, the name should be Default.cshtml; So pages named Index.cshtml will not be the default route.

Please note that these files must not be added to projects in Visual Studio; The reason for this is that, in addition to the CodeBehind framework, these pages are also compiled in the default .NET mode and can create unstable situation and security risk conditions.
Also note that the default code highlighter of Visual Studio may in some cases have unnecessary errors from cshtml pages based on the CodeBehind framework.

### Default pages after first run

In this version, if you run CodeBehind for the first time without the wwwroot directory, a ready default page will be created including a layout, header and footer.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrnohcr5n1ooqu2l5o5x.png)

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