Why should we use Code-Behind?

elanatframework - Jul 12 '23 - - Dev Community

A respected .NET developer asked Elanat a question. Our reply to him was longer than can be said in a comment; So we decided to create a new article to help other developers as well.

His question was:

Hi there! I have a small question about this section of your article:

The powerful Code-Behind library allows Elanat to have more freedom in presenting new features compared to the default dot net core MVC structure, which includes cshtml pages, and avoids the unnecessary limitations of Microsoft's aspdotnet core that are used by similar dot net systems.

Can you explain what kind of unnecessary limitations you experience that code behind does not have? I personally have been using MVC approach with razor most of the time and I feel quite the opposite. Not to mention, razor pages seem quite similar in the sense that you have a razor page coupled with a C# file with a similar page load method. They just have somewhat different syntax. I'm curious about how aspx pages with code behind are better in your opinion.

Dear Dennis
Thank you for your opinion

We didn't talk about the Razor syntax and how it compares to the syntax of the <% %> and <%= %> tags that were or are still used in Java JSP or Microsoft asp and aspx web pages. It is even possible to add Razor syntax support to the Code-Behind infrastructure in the future.

In that paragraph (which you quoted) it was blind to the use of the words cshtml pages to emphasize the default structure of .NET.

Existence of executable physical file (aspx) in the root makes your program more structured. Note that after running the program and compiling the aspx pages by Code-Behind, your program will no longer refer to any of the aspx files.

If the scale of the program you are building is high or you need to act dynamically, using Code-Behind will definitely give you more freedom.
If the scale of the program is low, using Code-Behind will simplify your program and you will generate faster and more understandable code.

The following example shows the power of Code-Behind:

aspx page

<%@ Page Controller="YourProjectName.wwwroot.DefaultController" Model="YourProjectName.wwwroot.DefaultModel" %><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title><%=model.PageTitle%></title>
</head>
<body>
    <%=model.LeftMenuValue%>
    <div class="main_content">
        <%=model.MainContentValue%>
    </div>
    <%=model.RightMenuValue%>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Controller class

using CodeBehind;

namespace YourProjectName.wwwroot
{
    public partial class DefaultController : CodeBehindController
    {
        public DefaultModel model = new DefaultModel();

        public void PageLoad(HttpContext context)
        {
            model.PageTitle = "My Title";

            CodeBehindExecute execute = new CodeBehindExecute();

            // Add Left Menu Page
            context.Request.Path = "/menu/left.aspx";
            model.LeftMenuValue = execute.Run(context);


            // Add Right Menu Page
            context.Request.Path = "/menu/right.aspx";
            model.RightMenuValue = execute.Run(context);


            // Add Main Content Page
            context.Request.Path = "/pages/main.aspx";
            model.MainContentValue = execute.Run(context);

            View(model);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Each of the pages left.aspx, right.aspx and main.aspx can also call several other aspx files; these calls can definitely be dynamic and an add-on can be executed that the kernel programmers don't even know about.

Enjoy Code-Behind, but be careful not to loop the program! (Don't call pages that call the current page)

What power does Code-Behind give you while running the program?

Accessing hypertext contents of pages and replacing some values before calling in other pages.

Microsoft usually ties your hands so that you cannot create a dynamic system.

By using the default architecture of Microsoft's ASP.NET Core, you will face some very big challenges. Creating a system with the ability to support plugins that both provides security and does not loop and can call other pages on your pages is very challenging.

We overcame many challenges in standard .NET versions to create a add-on-oriented structure using .NET.

You can refer to the following pages to see a corner of our struggle with the limitations of .NET:
https://github.com/elanatframework/Elanat/blob/elanat_framework/PathHandlersLoader.aspx.cs
https://github.com/elanatframework/Elanat/blob/elanat_framework/PageHandlersLoader.aspx.cs

Suppose you have created an application using the default ASP.NET Core cshtml that has a main page that includes a right menu and a left menu. As we have shown in the code above, can you change the values of these menus to Fill the dynamic form with cshtml pages and replace the values obtained from the pages? It is definitely possible, but it is difficult.
Code-Behind will not even refer to the physical aspx file to call the aspx pages and will only call a method.

How do you manage events in ASP.NET Core?
For example, a route from your program that requires several methods to be executed and these methods do not exist in the core of your program! This work can be blinded with the default cshtml of .NET, but it is difficult.
For example, we should have an event before the request to the search page and not allow the user to do more than 2 searches per minute. Using Code-Behind, we only need to check an aspx page, then reject or allow the search request.

Have you ever tried to create a plugin, module or a dynamic page for .NET systems?
Have you ever built a .NET system that supports a plugin, module, or a dynamic page add-on?
Have you ever wondered why this process is so difficult in .NET!

In the future, we will try to prepare an article on the criticism of the controller structure and the default model of ASP.NET Core.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player