CodeBehind 4.7 will be released by Elanat in the coming days. This release introduces several important improvements that enhance support for and integration with WebForms Core (WFC). It also upgrades WebForms Core to version 2.1. WFC 2.1 is a major update that introduces many new features and improvements.
CodeBehindConstructor has been removed
In CodeBehind 4.7, the CodeBehindConstructor method has been completely removed and merged into the PageLoad method. This change simplifies the MVC programming model.
In the new syntax, when the controller name is specified without parentheses, HttpContext is automatically passed to PageLoad.
Controller Examples
Example 1
View
@page
@controller MyController
Controller
using CodeBehind;
public partial class MyController : CodeBehindController
{
public void PageLoad(HttpContext context)
{
}
}
Example 2
View
@page
@controller MyController()
Controller
using CodeBehind;
public partial class MyController : CodeBehindController
{
public void PageLoad()
{
}
}
Example 3
View
@page
@controller MyController(123, "value")
Controller
using CodeBehind;
public partial class MyController : CodeBehindController
{
public void PageLoad(int i, string s)
{
}
}
For abstract models, specifying the model name without parentheses automatically passes HttpContext to PageLoad. For non-abstract models declared inside curly braces ({}), PageLoad is invoked only when parentheses are specified.
Model Examples
Example 1
View
@page
@model MyModel
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
using CodeBehind;
public partial class MyModel : CodeBehindModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public void PageLoad(HttpContext context)
{
}
}
Example 2
View
@page
@model MyModel()
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
using CodeBehind;
public partial class MyModel : CodeBehindModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public void PageLoad()
{
}
}
Example 3
View
@page
@model MyModel("ABC")
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
using CodeBehind;
public partial class MyModel : CodeBehindModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public void PageLoad(string s)
{
}
}
Example 4
View
@page
@model {MyModel}
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
public class MyModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
Example 5
View
@page
@model {MyModel()}
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
public class MyModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public void PageLoad()
{
}
}
Example 6
View
@page
@model {MyModel(123, 'c')}
<h1>@Value1</h1>
<h2>@Value2</h2>
Model
public class MyModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public void PageLoad(int i, char c)
{
}
}
Constructor Behavior
Controller Behavior
| Syntax | Behavior |
|---|---|
@controller MyController |
Creates the controller and automatically passes HttpContext to PageLoad(HttpContext context). |
@controller MyController() |
Creates the controller and calls PageLoad() with no arguments. |
@controller MyController(args) |
Creates the controller and calls PageLoad(args). |
Abstract Model Behavior
| Syntax | Behavior |
|---|---|
@model MyModel |
Creates an abstract model and automatically passes HttpContext to PageLoad(HttpContext context). |
@model MyModel() |
Creates an abstract model and calls PageLoad() with no arguments. |
@model MyModel(args) |
Creates an abstract model and calls PageLoad(args). |
Model Behavior
| Syntax | Behavior |
|---|---|
@model {MyModel} |
Creates a plain model only. PageLoad is not called. |
@model {MyModel()} |
Creates a plain model and calls PageLoad() with no arguments. |
@model {MyModel(args)} |
Creates a plain model and calls PageLoad(args). |
Write Output at the End of the Page
Beginning with CodeBehind 4.7, data written by calling the Write method from the Controller or Model is appended to the end of the View instead of being inserted at the beginning.
This change ensures compliance with HTML standards, as an HTML document is expected to begin with <!DOCTYPE html>.
It also improves compatibility with WFC technology when HTML is sent together with Action Control data.
The following example shows the difference between the behavior of Write in CodeBehind 4.7 and earlier versions.
View
@page
@controller TextBoxController
<!DOCTYPE html>
<html>
<head>
<title>CodeBehind Framework - Using WFC Technology</title>
<script type="module" src="/script/web-forms.js" defer></script>
</head>
<body>
<input id="TextBox1" type="text" placeholder="Write Color" />
</body>
</html>
Controller
using CodeBehind;
public partial class TextBoxController : CodeBehindController
{
public void PageLoad(HttpContext context)
{
WebForms form = new WebForms();
form.SetGetEvent("TextBox1", HtmlEvent.OnKeyUp, "/keyup");
Write(form.ExportToHtmlComment());
}
}
HTML output in CodeBehind 4.7
<!DOCTYPE html>
<html>
<head>
<title>CodeBehind Framework - Using WFC Technology</title>
<script type="module" src="/script/web-forms.js" defer></script>
</head>
<body>
<input id="TextBox1" type="text" placeholder="Write Color" />
</body>
</html>
<!--[web-forms]
EgTextBox1=onkeyup?keyup-->
HTML output in CodeBehind version 4.6 and earlier
<!--[web-forms]
EgTextBox1=onkeyup?keyup-->
<!DOCTYPE html>
<html>
<head>
<title>CodeBehind Framework - Using WFC Technology</title>
<script type="module" src="/script/web-forms.js" defer></script>
</head>
<body>
<input id="TextBox1" type="text" placeholder="Write Color" />
</body>
</html>
Automatic Content-Type
A new option automatically sets the response Content-Type header to text/html; charset=utf-8 for PostBack requests.
In some cases, when the server response is not valid XML, the browser attempts to parse it as XML and logs the following error in the console:
XML Parsing Error: no root element found
This error typically occurs when the response is empty or when it does not contain a valid XML document.
To prevent this, enable the following option:
...
ignore_layout_for_post_back=false
+set_text_html_content_type_for_post_back=true
sse_interval=1000
...
When enabled, the framework sets the response Content-Type header to text/html; charset=utf-8, preventing the browser from attempting to parse the response as XML and eliminating the parsing error.
The set_text_html_content_type_for_post_back option is enabled by default.
Separate DLL Paths
DLL and View paths can now be configured independently.
This allows View files to remain under `wwwroot while DLLs are stored in a secure directory.
Two new options have been added to the Options file (options.ini) to manage the DLL path.
diff
[CodeBehind-options]
view_path=wwwroot
move_view_from_wwwroot=true
+dll_path=wwwroot/bin
+move_dll_from_wwwroot_bin=true
rewrite_aspx_file_to_directory=false
access_aspx_file_after_rewrite=false
...
When move_dll_from_wwwroot_bin is enabled, DLL files are automatically moved from wwwroot/bin to the directory specified by dll_path after the View files are recompiled.
Conclusion
CodeBehind 4.7 is a significant step forward in refining the framework. Rather than focusing solely on new features, this release improves the overall developer experience by simplifying the API, making framework behavior more consistent, introducing a cleaner MVC programming model, improving integration with WebForms Core (WFC), and providing more flexible project deployment options.
These improvements make CodeBehind more intuitive, maintainable, and better suited for building modern web applications.


Top comments (0)