CodeBehind is a new back-end framework that supports the C# programming language. The CodeBehind framework runs under ASP.NET Core. CodeBehind returns aspx files to .NET Core and supports Razor syntax in aspx pages. CodeBehind framework runs 40% faster than Razor pages in single page mode; also, in MVC mode, it works up to 33% faster than the default ASP.NET Core MVC structure. In addition to the amazing response speed, CodeBehind has a simple structure, it is understandable, it has a unique architecture, it is modular and it is modern.
CodeBehind is .NET Diamond!
CodeBehind 2.3
In version 2.3 of the CodeBehind framework, we focused on making the Controller structure more advanced. In this version, it is possible to run the Controller classes, request the View along with the model, and also prevent the execution of the Default.aspx
routes.
Specify View along with Model from all Controllers
Calling View along with Model in all Controllers is a new feature of CodeBehind framework version 2.3. Previously, you could only add a Model to the View in the Controller that was specified in the View.
The following example shows how to call a View page along with the Model data in the Controller:
using CodeBehind;
namespace YourProjectName
{
public partial class DefaultController : CodeBehindController
{
public void PageLoad(HttpContext context)
{
View("/member.aspx", new MemberModel { PageTitle = "Member: " + UserName, BodyValue = "this is " + UserName + " member page" });
}
}
}
The Controller class in the code above initializes a model and requests it along with the View.
Model in the LoadPage method in View pages
From now on, in the LoadPage method, you can set the created instance of the Model in the View; this possibility can be done in View pages.
Example:
Model
public class MemberModel : CodeBehindModel
{
public string PageTitle { get; set; }
public string BodyValue { get; set; }
}
A View page
<div>
@LoadPage("/member.aspx", new MemberModel{ PageTitle = "Member: " + UserName, BodyValue = "this is " + UserName + " member page" })
</div>
The code above is a LoadPage method located in a View page; this method initializes the Model class on the view page named member.aspx and displays the return values on the current page.
Support strings written from the previous Controller
Consider a situation where a View is called that has a Controller. Now if we decide to change the View in the Controller, if the new View contains the Controller, the values that the previous Controller added on its own View will be ignored.
Example:
View - The View that is requested for the first time
@page
@controller MyControllerClass
Controller (MyControllerClass)
using CodeBehind;
namespace YourProjectName
{
public partial class MyControllerClass: CodeBehindController
{
public void PageLoad(HttpContext context)
{
Write("MyControllerClass writed text");
View("/new_view.aspx");
}
}
}
Called View (new_view.aspx)
@page
@controller NewControllerClass
Controller (NewControllerClass)
using CodeBehind;
namespace YourProjectName
{
public partial class NewControllerClass: CodeBehindController
{
public void PageLoad(HttpContext context)
{
Write("NewControllerClass writed text");
}
}
}
Result in version 2.2 and earlier
NewControllerClass writed text
In version 2.3, values added by previous Controllers are added to the newly called View.
Result in version 2.3 and later
MyControllerClass writed text
NewControllerClass writed text
Prevent access to Default.aspx
View files in CodeBehind framework have aspx extension; if you create a View file named Default.aspx
in a directory, accessing the directory will execute that file. So Default.aspx
is a default View for a directory path.
Example:
If there is a Default.aspx
file in the root, the example.com request executes the example.com/Default.aspx
path.
Similarly, if there is a Default.aspx
file in root/dir, requesting example.com/dir will execute the example.com/dir/Default.aspx
path.
However, the Default.aspx
file path will still be accessible. In version 2.3, we added a new option in the options file, which disables access to the Default.aspx
path.
Options file
[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
show_minor_errors=false
error_page_path=/error.aspx/{value}
+prevent_access_default_aspx=false
Example:
The path example.com/dir/Default.aspx
will not be available, but the path example.com/dir/
will still be accessible.
By activating this option, additional urls are prevented and thus SEO is improved.
Run Controller
From version 2.3 onwards you can run the Controller. Before we explain this section, it is necessary to know that the structure of MVC in the CodeBehind framework is that the View is requested first, and then the View calls the Controller and Model classes. Therefore, the implementation of the Controller is a new feature that may have many uses in some cases. The RunController method executes a Controller class and requests the View path; interestingly, if the requested View has a Controller, the View Controller is also called.
The RunController
method is located in the CodeBehindExecute
class and the input is an instance of the Controller class.
The example below is a change in the configuration type in the Program.cs
class, which calls a controller named ControllerForRoute
when the request route starts with /route/
.
Program.cs
using CodeBehind;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
SetCodeBehind.CodeBehindCompiler.Initialization();
app.Run(async context =>
{
CodeBehindExecute execute = new CodeBehindExecute();
if (context.Request.Path.HasMatching("start_with", "/route/"))
{
ControllerForRoute controller = new ControllerForRoute();
await context.Response.WriteAsync(execute.RunController(new ControllerForRoute(), context));
}
await context.Response.WriteAsync(execute.Run(context));
});
app.Run();
Problems that were solved
- In cases where the current View is wrongly requested from the Controller, the loop is avoided.
Related links
How to Use CodeBehind framework
https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_use_code_behind.md
CodeBehind on GitHub
https://github.com/elanatframework/Code_behind
CodeBehind in NuGet
https://www.nuget.org/packages/CodeBehind/
CodeBehind Website Page
https://elanat.net/page_content/code_behind
Top comments (0)