BIG surprise! aspx files in .NET Core
Tags: dotnet, dotnetcore, csharp, news
Published: 2023-07-01 21:30:27

## .NET Core issues

In .NET Core, there are problems such as the bad complexity of web programming and the vagueness of coding in the controller and the destruction of the web programming structure on the server side.

Also, one of the negative points of .NET Core is the lack of support for aspx files. After the initial release of .NET Core, we at [Elanat](https://elanat.net) team were very disappointed and we had decided to leave the Microsoft ecosystem forever and choose a stable platform to migrate Elanat framework.

In the past years, we have done complete research on CLR and JVM virtual machines, and we have examined all static processes and physical accesses and caching of virtual machines, and we have also examined the complete process of compilation in .NET languages, as well as research on We did several CMS and portals and site builders and how to interact with Add-ons (they may say plugins or modules or extensions) and how to create Add-ons for these systems by developers; we decided to reconsider leaving the Microsoft ecosystem for now; then we created a new style for programming under .NET Core using aspx files called Code-Behind.

## Introducing Code-Behind

![migrating to .NET Core](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sybbrjqpnnx2hvdbfutz.png)

This style is completely based on MVC, and in the near future we will expand it in such a way that there is no need for coding the view part, such as for, foreach, and while loops.

We will also try to add support for a Web-Form structure in Code-Behind in such a way that it does not have the past problems of the standard .NET Web-Form and avoids generating additional codes and does not generate additional overhead on the server; in fact, this new Web-Form structure will not be different from the MVC model in terms of performance, bandwidth, and server overhead; therefore, our Code-Behind can support MVC, Code-Behind and Web-Form at the same time.

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

We will use Code-Behind to migrate the Elanat framework from .NET Standard to the latest .NET Core versions; during the migration, if we need more convenient coding and more maneuvers to do coding, we will add new features to Code-Behind and provide newer versions of Code-Behind with more features.

We added Code-Behind in Nuget so you can easily access it.
You can access it in the following link:
[https://www.nuget.org/packages/CodeBehind](https://www.nuget.org/packages/CodeBehind)
Github link:
[https://github.com/elanatframework/Code_behind](https://github.com/elanatframework/Code_behind)

## Write code with Code-Behind

You can add aspx files in the **wwwroot** directory and its subdirectories

An example of an aspx file based on Code-Behind:

```html
<%@ 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>
```

An example of a controller class that is based on Code-Behind:

```csharp
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);
        }
    }
}
```

An example of a model class that is based on Code-Behind:

```csharp
using CodeBehind;

namespace YourProjectName.wwwroot
{
    public partial class DefaultModel : CodeBehindModel
    {
        public string PageTitle { get; set; }
        public string BodyValue { get; set; }
    }
}
```

Program file and additional Code-Behind items:

```csharp
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();
```

In the Program.cs class codes above, the three values marked with the + character must be added.
We show the codes separately for you:

```csharp
CodeBehindCompiler.Initialization();
```

```csharp
CodeBehindExecute execute = new CodeBehindExecute();
await context.Response.WriteAsync(execute.Run(context));
```

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:

```csharp
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";
}
```

Please comment about the use of Code-Behind and aspx pages or the default use of cshtml pages in .NET Core.