Configuring the CodeBehind Framework in the ASP.NET Core Project
Tags: tutorial, dotnet, beginners, backend
Published: 2024-05-27 14:39:14

This tutorial teaches the basics of building a [CodeBehind framework](https://github.com/elanatframework/Code_behind) web application under ASP.NET Core.

![Config CodeBehind in Visual Studio Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9txq4gb9m6687q9wqqwd.png)

## Installation of Visual Studio Code and necessary items

In this training series, we use the Visual Studio Code code editor. To download and install it, refer to the following link:
https://code.visualstudio.com/download

After installing Visual Studio Code, you need to install the latest .NET SDK package. Currently, version 8.0 is the latest version of .NET; you can download and install it from the following link:
https://dotnet.microsoft.com/en-us/download/dotnet/8.0

In the last step, download and install the C# Dev Kit package from the link below:
https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit

## Creating a new .NET project

In order to create a .NET 8.0 project, first open the Visual Studio Code editor that you installed earlier.
In the menu section, first select the file option and then Open Folder, and then create a directory named `MyProjects` in the desired path.

Then, enter the following value in the above console to create a new ASP.NET Core project in the `MyProjects` directory.
`>.NET: New Project`
`ASP.NET Core Empty`
Then name the project `MyCodeBehindProjct` and select and confirm the next items in the console (Then select `Default directory` and then `Create project`.).

The image below shows the location of the console section in Visual Studio Code.

![Console Section in Visual Studio Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4lj6tc9cffdxb33zsvd.png)

## Add CodeBehind Framework package

To install the CodeBehind framework package in the console, enter the following value:
`> NuGet: Add NuGet Package`
Then search for the name `CodeBehind` and after the new box opens, select and install the latest version of the CodeBehind framework.

## Configure the CodeBehind framework in the Program.cs class

To access the files and directories of the project you created, select View and then Explorer from the menu (or you can select the combination keys Ctrl + Shift + E).

According to the image below, Explorer is added in the right part of Visual Studio Code.

![Explorer in Visual Studio Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twludtdes3yu668uwizu.png)

Open the `Program.cs` class in the Explorer section and then replace the previous values of this class with the following values.

```csharp
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.UseCodeBehind();

app.Run();
```

The code in the above section is the configuration of the CodeBehind framework in ASP.NET Core.

> Note: In order to have automatic error management, you can use `app.UseCodeBehind();` Set the value to `true`.

Example: `app.UseCodeBehind(true);`

## Run project

Then select `Run` and then `Start Debugging` or `Start Without Debugging` from the menu. Then the console menu will open and you have to select the `C#` option and then select the project name.

The image below you are viewing is the default template of the CodeBehind framework. If you see this page, it means that you have completed all the steps successfully.

![CodeBehind Framework Default Template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yyitvluhyjppx0vi4ju.png)

Back to Visual Studio Code; if you configure the CodeBehind framework for the first time and run the project, the default template of the CodeBehind framework will be added to the `wwwroot` path of your project. For the next time, the default template will be added when there is no `wwwroot` directory and in the option file. CodeBehind, `view_path` should be according to `wwwroot`.
Also, a directory called `code_behind` will be added in your project.
In the next tutorials, we will fully teach how to set the options of the options file. We also introduce the files in the `code_behind` directory.

![CodeBehind Framework First Run After Configuring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwfj08liy884eow0msyu.png)

### Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

CodeBehind in NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind