DEV Community

Cover image for @BigBomb, #Revolution, Independence, Security, Options on the table. CodeBehind version 1.8 has been released!
elanatframework
elanatframework

Posted on • Edited on

@BigBomb, #Revolution, Independence, Security, Options on the table. CodeBehind version 1.8 has been released!

Introduction

CodeBehind is a back-end framework built on .NET Core. CodeBehind was created to compete with the default cshtml structure in .NET Core. Version 1.8 was released on November 26, 2023. Version 1.8 is the biggest update of CodeBehind and many features have been added to it. You don't have to follow the MVC pattern in CodeBehind and it's not mandatory in MVC. However, note that MVC is very simple in CodeBehind.

CodeBehind is .NET Diamond!

Razor syntax support

This is a big bomb! CodeBehind version 1.8 supports Razor syntax. You can either develop your programs based on the Razor syntax or use the standard syntax.
Please note that the Razor syntax will also be created in the aspx files and the Razor syntax will be automatically determined from the standard syntax. In addition, it is not possible to combine Razor syntax and standard syntax.
Razor syntax in CodeBehind is very similar to Razor syntax in cshtml pages in .NET Core, but in some cases there may be slight differences. Also note that if there is an error in aspx pages that are created with Razor syntax, it is different from Razor syntax errors in .NET Core cshtml pages. The Elanat team doesn't know the Microsoft approach, and the support for Razor pages in CodeBehind was created from the ground up by the Elanat team.

Razor syntax example 1

@page
@controller YourProjectName.DefaultController
@model YourProjectName.DefaultModel
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@model.PageTitle</title>
</head>
<body>
    @model.BodyValue
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Razor syntax example 2

@{
    string Note = "Elanat CMS was created to be a reliable system in .NET and an honor for .NET programmers and can be compared to other systems under PHP and JAVA.";
}

<p>@Note</p>
Enter fullscreen mode Exit fullscreen mode

Razor syntax example 3

@foreach (NameValue nv in NameValues)
{
    <b>Name: @nv.Name</b>
    <p>Value: @nv.Value</p>
}
Enter fullscreen mode Exit fullscreen mode

Razor Syntax in CodeBehind supports @for, @foreach, @while, and @do while iterative loops as well as @if, else if, else, and @switch conditions and more; soon, we will provide full explanations about Razor syntax in CodeBehind.

Template support

100% Code-Behind support guarantee!
The Elanat team has introduced a wonderful Template structure with version 1.8. Templates form parts of an aspx page.
Please consider the following example:

standard syntax

<%@ Page Controller="YourProjectName.DefaultController" Model="YourProjectName.DefaultModel" %>
<!DOCTYPE html>
<html>
<head>
+   <#GlobalTags#>
    <title><%=model.PageTitle%></title>
</head>
<body>
    <%=model.BodyValue%>
</body>
</html>

+<#GlobalTags
+<meta charset="utf-8" />
+<meta name="viewport" content="width=device-width, initial-scale=1.0" />
+<meta http-equiv="Content-Type" content="text/html; charset=utf-16" />
+<meta http-equiv="content-language" content="en">
+<script type="text/javascript" src="/client/script/global.js" ></script>
+<link rel="alternate" type="application/rss+xml" title="rss feed" href="/rss/" />
+<link rel="shortcut icon" href="/favicon.ico" />
+<link rel="stylesheet" type="text/css" href="/client/style/global.css" />
+#>
Enter fullscreen mode Exit fullscreen mode

In this example, a Template section is called in the Template variable and the output is converted as follows.

<%@ Page Controller="YourProjectName.DefaultController" Model="YourProjectName.DefaultModel" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-16" />
    <meta http-equiv="content-language" content="en">
    <script type="text/javascript" src="/client/script/global.js" ></script>
    <link rel="alternate" type="application/rss+xml" title="rss feed" href="/rss/" />
    <link rel="shortcut icon" href="/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/client/style/global.css" />
    <title><%=model.PageTitle%></title>
</head>
<body>
    <%=model.BodyValue%>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

But this structure seems to be very simple and basic! Why did we talk about a wonderful structure?

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).

Usually, for repetitive parts such as loops and conditions, you should either add the codes of the design part in the server side codes or add the server side codes in the design part.

An example of server side coding in the design section

<table>
    <thead>
        <tr>
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Column1</td>
                <td>@item.Column2</td>
                <td>@item.Column3</td>
            </tr>
        }
    </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

Example of integration of the design part in the server side codes

model.ListValue = "<ul>";

foreach(string s in List)
    model.ListValue += "<li>" + s + "</li>";

model.ListValue += "</ul>";
Enter fullscreen mode Exit fullscreen mode

Please note that things like the presence of a variable between the codes of the design section are still considered Code-Behind, unless the codes of the server section are added.

Example for Razor syntax

<h1>@model.PageName</h1>
Enter fullscreen mode Exit fullscreen mode

Example for standard syntax

<h1><%=model.PageName%></h1>
Enter fullscreen mode Exit fullscreen mode

In the two examples above, the @model.PageName and <%=model.PageName%> values are easily recognized in the html tags; So the Code-Behind is still in place and designers or client-side developers can easily recognize these values.

One of the most famous frameworks that guaranteed the separation of server-side codes from the design department was Microsoft's web-form; but this guarantee was a heavy price, such as additional codes and lack of full control of the designers over the design department.

Currently, the CodeBehind framework supports 100% Code-Behind-based design structure; if you know another framework that supports Code-Behind 100%, please share it with us by showing an example in the comments section.

Please note: The Elanat team introduces the framework created by itself as CodeBehind and calls the coding structure for the complete separation of the server part codes from the design part Code-Behind (uses the dash character to separate Code and Behind). If the name CodeBehind is confusing for you, you can call it Elanat CodeBehind or Elanat CodeBehind framework!

Example (Razor syntax)
View section before pasting the template

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
    @#Tags={<a href="@#Href">@#PageName</a>}
  </div>
</div>

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

@#PageName{@page.Title}
@#Href{@(((page.Path == "main")? "/" : page.Path))}
Enter fullscreen mode Exit fullscreen mode

pasting template auto step 1

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
    @#Tags={<a href="@#Href">@#PageName</a>}
  </div>
</div>

@#Tags{
@foreach (PageItem page in model.PageItemList)
{
    <a href="@#Href">@#PageName</a>
}
}

@#PageName{@page.Title}
@#Href{@(((page.Path == "main")? "/" : page.Path))}
Enter fullscreen mode Exit fullscreen mode

pasting template auto step 2

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
    @foreach (PageItem page in model.PageItemList)
    {
        <a href="@#Href">@#PageName</a>
    }
  </div>
</div>

@#PageName{@page.Title}
@#Href{@(((page.Path == "main")? "/" : page.Path))}
Enter fullscreen mode Exit fullscreen mode

pasting template auto step 3 (finally)

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
    @foreach (PageItem page in model.PageItemList)
    {
        <a href="@((page.Path == "main")? "/" : page.Path)">@page.Title</a>
    }
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Enjoy beautiful structer in returned template in CodeBehind framework.

You can introduce the template externally.
Elanat team has considered astx file extension for external files of templates.

Elanat Framework company provides astx extension

Example for set external template

Razor syntax example

@page
@template "/page/template/page_template.astx"
Enter fullscreen mode Exit fullscreen mode

Standard syntax example

<%@ Page Template="/page/template/page_template.astx" %>
Enter fullscreen mode Exit fullscreen mode

There is no need to add the astx file extension and the following path is also correct.
"/page/template/page_template"

In the template placement example, if you add the template externally, you will have the following html in the design section.

View section before pasting the template

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
+    @#Tags={<a href="@#Href">@#PageName</a>}
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Without a return template, at best you will have the following html in the design section.

View section without use return template

<div class="header">
  <a href="#default">@model.Title</a>
  <div class="header-right">
+ @foreach (PageItem page in model.PageItemList)
+ {
+    <a href="@((page.Path == "main")? "/" : page.Path)">@page.Title</a>
+ }
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Do you understand the beauty of working with a return template?

You can also use template for standard syntax. The previous example is unclear in the standard syntax in the following codes.

<div class="header">
  <a href="#default"><%=model.Title%></a>
  <div class="header-right">
    <#Tags=<a href="<#Href#>"><#PageName#></a>#>
  </div>
</div>

<#Tags 
<% foreach (PageItem page in model.PageItemList)
{
    <#Tags#>
}
%>
#>

<#PageName <%=page.Title%>#>
<#Href <%=((page.Path == "main")? "/" : page.Path)%>#>
Enter fullscreen mode Exit fullscreen mode

If the return value is placed between the standard syntax tags, the syntax will be automatically closed before the return value and the syntax will be opened after it.

pasting template auto step finally for standard syntax

<div class="header">
  <a href="#default"><%=model.Title%></a>
  <div class="header-right">
    <% foreach (PageItem page in model.PageItemList)
    {
        %><a href="<%=((page.Path == "main")? "/" : page.Path)%>"><%=page.Title%></a><%
    }
    %>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

The template feature that was added in version 1.8 of the CodeBehind framework has many features and we cannot add its description in this article, so we will definitely give a complete explanation about the template in the CodeBehind framework in the future.

Constructor method

One of the new 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.

Example for standard syntax

<%@ Page Controller="YourProjectName.DefaultController(1)" Model="YourProjectName.DefaultModel(context)" %>
Enter fullscreen mode Exit fullscreen mode

Example for Razor syntax

@page
@controller YourProjectName.DefaultController(1)
@model YourProjectName.DefaultModel(context)
Enter fullscreen mode Exit fullscreen mode

Controller class

using CodeBehind;

namespace YourProjectName
{
    public partial class DefaultController : CodeBehindController
    {
        public DefaultModel model = new DefaultModel();
        public void PageLoad(HttpContext context)
        {
            View(model);
        }

        public void CodeBehindConstructor(int Index)
        {
            Write(Index.ToString());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Model class

using CodeBehind;

namespace YourProjectName
{
    public partial class DefaultModel : CodeBehindModel
    {
        public void CodeBehindConstructor(HttpContext context)
        {
            Write(context.Request.Query["name"].ToString());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

As shown in the above example, from now on you can access the HttpContext in the model without a controller, just add the context value as an input argument in front of the model class name. Then you can add the CodeBehindConstructor method in the model class.

Added option

From now on, there is an options file in the code_behind/options.ini path. This file will contain many options for customization.

Currently, the options file in CodeBehind version 1.8 is as follows:

[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
Enter fullscreen mode Exit fullscreen mode

Added the possibility to load aspx page names as directory names

Change view Path
(view_path=wwwroot)
CodeBehind framework users can now remove the path of aspx files from wwwroot and add them to their desired directory. If you use server-side code in view (aspx) pages, this option leads to more security of your applications.

Move views from wwwroot path
(move_view_from_wwwroot=true)
If you have selected the path of the view file in a path other than the wwwroot path, if the option to move from the wwwroot path is enabled, automatically after recompile, all aspx files will be moved from the wwwroot path to your chosen path.

Rewrite aspx path as directory
(rewrite_aspx_file_to_directory=false)
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.

Note: You can safely activate this option because this rewrite will not create any extra load for processing.

Example:
access /page/aboue.aspx in /page/about path

Also access aspx file after rewrite as directory
(access_aspx_file_after_rewrite=false)
If you have enabled rewriting the path of aspx files as a directory name, enabling this option will allow you to still access the path of the aspx file.

Ignore Default.aspx after rewrite
(ignore_default_after_rewrite=true)
Naming the aspx file as Default.aspx makes it unnecessary to add the name of this file after the last directory, and this file is recognized automatically; if you have enabled rewriting the path of aspx files as a directory name, enabling this option will make the Default.aspx files be considered as the default file of the previous directory path. Therefore, the Default directory will not be executed.

From now on, don't worry about new lines and extra tabs and spaces.

Trim in start
(start_trim_in_aspx_file=true)
If this option is active, every new line and extra tab and space will be deleted from the top of the aspx files.
No need to worry about <!DOCTYPE html> (or ) from now on! Add it further down and feel free to have an extra line.

Example:

You don't have to do this anymore
standard syntax

<%@ Page %><!DOCTYPE html>
Enter fullscreen mode Exit fullscreen mode

This way there will be no extra lines

<%@ Page %>
<!DOCTYPE html>
Enter fullscreen mode Exit fullscreen mode

You don't have to do this

@page<!DOCTYPE html>
Enter fullscreen mode Exit fullscreen mode

This way there will be no extra lines

@page
<!DOCTYPE html>
Enter fullscreen mode Exit fullscreen mode

Trim in inner aspx
(inner_trim_in_aspx_file=true)
If this option is enabled, your coding that is placed after the next line will also delete the next line and there will be no extra line between the html tags in the design section.

Code blocks will not leave an extra line after the new line.

Example for Razor syntax
@{ code }

Example for standard syntax
<% code %>

Trim in end
(end_trim_in_aspx_file=true)
If this option is active, every new line and extra tab and space will be deleted from the bottom of the aspx files.

In the future, more customization options will be added to the options file.

Added HtmlData classes

From now on you can use classes located in the HtmlData namespace.
The Elanat team currently supports three commonly used and recurring data types and will add more data types to the HtmlData namespace in the future.

Attribute and AttributeCollection
This data type adds one or more attributes to the html tag.

OptionTag and OptionTagCollection
This type of data is used for drop-down and fixed lists, and you can call and add this type of data inside the select tag.

CheckBoxItem and CheckBoxItemCollection
Sometimes it happens that you need a single list where the user can select more than one data; this data type adds lists of checkbox tags.

Example (for option tag)

view (aspx page) (Razor syntax)

<select name="framework">
    @model.FrameworkOptionTags
</select>
Enter fullscreen mode Exit fullscreen mode

view (aspx page) (standard syntax)

<select name="framework">
    <%=model.FrameworkOptionTags%>
</select>
Enter fullscreen mode Exit fullscreen mode

Controller

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Result after response

<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>
Enter fullscreen mode Exit fullscreen mode

Namespace and dll for CodeBehind view class

From now on, you can add namespaces to the view class aggregating aspx files. Add the namespaces to the namespace_import_list file at this path: code_behind/namespace_import_list.ini.

The default content of the namespace_import_list.ini file

[CodeBehind namespace import list]
namespace=System.IO
namespace=System.Collections
namespace=System.Collections.Generic
namespace=System.Linq
namespace=System.Threading
namespace=System.Threading.Tasks
Enter fullscreen mode Exit fullscreen mode

You can also add necessary dlls to the view class that aggregates aspx files. This file is located in: code_behind/dll_import_list.ini path.

The default content of the dll_import_list.ini file

[CodeBehind dll import list]
dll_path={run_time_path}/System.IO.dll
dll_path={run_time_path}/System.Collections.dll
dll_path={run_time_path}/System.Linq.dll
dll_path={run_time_path}/System.Threading.dll
Enter fullscreen mode Exit fullscreen mode

You can use the variables {run_time_path}, {asp_run_time_path} and {base_directory_path} to determine the path.

Note: The dll_import_list.ini file is only for the dlls that the view class aggregating aspx files need, and these dlls have nothing to do with the wwwroot/dll path.

Problems that were solved

  • The problem of executing the path with extra characters after the slash character was solved.
  • Fixed the problem of replacing the class file with failed compilation in the last successful compilation.

astx extension icons

astx extension are external template files for the CodeBehind framework belonging to the Elanat team.

The icons below are all in png format and are prepared with a ratio of 512x512 pixels.

astx icon 1

astx icon 2

astx icon 3

astx icon 4

astx icon 5

What will the next version (CodeBehind 1.9) bring?

In the next version of CodeBehind framework, you will be able to add layout page.
We will be able to add data on the current page to the layout page; this allows you to call the page title and various tags in the layout page in addition to loading the current page in the layout page. We also add the possibility of loading pages in the view section of layouts so that you can easily manage pages such as header and footer separately in layouts.
The possibility of preventing the direct execution of some pages (such as separate header and footer) will also be added in this version.

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)