DEV Community

Cover image for CodeBehind 2.1; aspx Back to ASP.NET Core
elanatframework
elanatframework

Posted on

CodeBehind 2.1; aspx Back to ASP.NET Core

aspx extension are files of the view section of the CodeBehind framework. These files used to be Microsoft Web-Forms that supported the standard (<%=Standard%>) syntax. aspx files now support both standard and razor syntax in the CodeBehind framework. aspx files are currently competing with cshtml files in the default structure of ASP.NET Core.

Note: We at Elanat team named the former aspx syntax as standard after adding the Razor syntax support feature.

By studying the CodeBehind framework GitHub repository, you can get enough information about the structure of this framework and compare it with ASP.NET Core.

New Version

Version 2 of the CodeBehind framework was a stable version, with this in mind, for the release of version 2.1 we decided to start rewriting some methods and code; in addition, we added two new features in version 2.1. We performed many tests on this version and fixed the existing bugs; after the release of version 2.1, we discovered a new bug, this was a small bug and it only occurred in the situation that in the Razor syntax, one of the attributes of the page is terminated by the less-than (<) character. After fixing this bug, we also released sub version 2.1.1 very quickly.

Change view in controller

In the CodeBehind framework, we could add one or more views anywhere on the page. In order to make the CodeBehind framework more structured, we decided to provide the possibility to change the view in the controller.

In version 2.1, an overload for the View method has been added, now you can determine the path of a view in the controller through the View method; this will make the current view not run and the new view will be called; of course, the new view can still include the new controller and model.

Changing the view in the controller allows you to create headless systems.

Example:

using CodeBehind;

namespace YourProjectName
{
    public partial class DefaultController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
+            View("/page1.aspx");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Transfer template block data in ViewData

The Elanat team has again taken a new initiative and created the feature of sending template blocks through ViewData. You can now enclose template variables within {@#TempName} brackets so that double-quote ("{@#TempName}") characters do not cause problems in code blocks.

Template variables placed between open and closed brackets make the double-quote and \n characters of the block suitable for placement in a string. This will help you add template blocks in ViewData and call them in other pages (like Layout).

Example:

@page
@layout "/layout.aspx"
@{
    @ViewData.Add("title", "Company name");
    @ViewData.Add("script", "{@#TempName}");
}
...
@#TempName{
    <script>
        $(window).load(function() {
            // init Isotope
            var $projects = $('.projects').isotope({
                itemSelector: '.project',
                layoutMode: 'fitRows'
            });
            $(".filter-btn").click(function() {
                var data_filter = $(this).attr("data-filter");
                $projects.isotope({
                    filter: data_filter
                });
                $(".filter-btn").removeClass("active");
                $(".filter-btn").removeClass("shadow");
                $(this).addClass("active");
                $(this).addClass("shadow");
                return false;
            });
        });
    </script>
}
Enter fullscreen mode Exit fullscreen mode

After placing the template blocks in the template variables, the values of the @#TempName block in the above codes are added to the ViewData as below.

@ViewData.Add("script", "\n    <script>\n        $(window).load(function() {\n            // init Isotope\n            var $projects = $('.projects').isotope({\n                itemSelector: '.project',\n                layoutMode: 'fitRows'\n            });\n            $(\".filter-btn\").click(function() {\n                var data_filter = $(this).attr(\"data-filter\");\n                $projects.isotope({\n                    filter: data_filter\n                });\n                $(\".filter-btn\").removeClass(\"active\");\n                $(\".filter-btn\").removeClass(\"shadow\");\n                $(this).addClass(\"active\");\n                $(this).addClass(\"shadow\");\n                return false;\n            });\n        });\n    </script>\n");
Enter fullscreen mode Exit fullscreen mode

Rewriting part of the codes and methods

In previous versions, if you used the \n character in view pages, the CodeBehind framework considered a new line; in version 2.1, the \n character is no longer considered a newline and \n is displayed in the final output.

In this version, we have completely rewritten the codes for creating new files.

Problems that were solved

  • Deleting unused ex variable from the final view class.

Sub version 2.1.1

In this version, the Razor syntax problem of page attributes that end with the less-than (<) character is resolved.

Sub version 2.1.2

In this version, we completely rewrote the codes related to recognizing the page attribute in the Razor syntax.

Related links

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

Get CodeBehind from NuGet:
https://www.nuget.org/packages/CodeBehind/

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

Top comments (0)