CodeBehind is a modern full-stack web framework under ASP.NET Core. CodeBehind was developed by Elanat in 2023 and competes with Microsoft's default web frameworks (ASP.NET Core MVC and Razor Pages and Blazor). CodeBehind is an engineering masterpiece that simultaneously provides the possibility of development based on MVC, Model-View, Controller-View, only View and Web-Forms. The type of structure and naming in CodeBehind is a nostalgia that reminds of former Microsoft Web-Forms. CodeBehind has nothing to do with the old web-form in .NET. In CodeBehind, you can use Razor syntax (@Razor) and standard syntax (<%=Standard%>). CodeBehind is .NET Diamond! In many scenarios, CodeBehind performs better than the default frameworks in ASP.NET Core.
$ dotnet add package CodeBehindThis library is a programming model based on the MVC structure, which provides the possibility of creating dynamic aspx files (similar to .NET standard) in .NET Core and has high serverside independence. Soon we will expand this project so that in future versions you can experience both MVC and Code-Behind without coding in the view.
By using Code_behind, we will soon migrate Elanat framework from .NET Standard to .NET Core; during the migration, if we need to have simpler coding and need more maneuvers to do coding, we will add new features to Code_behind.
We added Code_behind in Nuget so that you can access it easily. You can use it in: https://www.nuget.org/packages/CodeBehind
View File: Default.aspx
<%@ Page Controller="YourProjectName.wwwroot.DefaultController" Model="YourProjectName.wwwroot.DefaultModel" %><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><%=model.PageTitle%></title>
</head>
<body>
<%=model.BodyValue%>
</body>
</html>
Model File: Default.aspx.Model.cs
using CodeBehind;
namespace YourProjectName.wwwroot
{
public partial class DefaultModel : CodeBehindModel
{
public string PageTitle { get; set; }
public string BodyValue { get; set; }
}
}
Controler File: Default.aspx.Controller.cs
using CodeBehind;
namespace YourProjectName.wwwroot
{
public partial class DefaultController : CodeBehindController
{
public DefaultModel model = new DefaultModel();
public void PageLoad(HttpContext context)
{
model.PageTitle = "My Title";
model.BodyValue = "HTML Body";
View(model);
}
}
}
Program File: Program.cs
using CodeBehind;
using SetCodeBehind;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
+ CodeBehindCompiler.Initialization();
app.Run(async context =>
{
+ CodeBehindExecute execute = new CodeBehindExecute();
+ await context.Response.WriteAsync(execute.Run(context));
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.Run();You can use the Write method in the model and controller classes; the Write method adds a string value to the ResponseText attribute; you can also change the values of the ResponseText attribute by accessing them directly.
In the controller class, there is an attribute named IgnoreViewAndModel attribute, and if you activate the IgnoreViewAndModel attribute, it will ignore the values of model and view and you will only see a blank page; this feature allows you to display the values you need to the user and avoid multiple redirects and transfers.
To receive the information sent through the form, you can follow the instructions below:
public DefaultModel model = new DefaultModel();
public void PageLoad(HttpContext context)
{
if (!string.IsNullOrEmpty(context.Request.Form["btn_Add"]))
btn_Add_Click();
View(model);
}
private void btn_Add_Click()
{
model.PageTitle = "btn_Add Button Clicked";
}