DEV Community

Elanat Framework
Elanat Framework

Posted on

View design with Standard syntax

The CodeBehind framework, pioneered by the Elanat team, brings a fresh perspective to ASP.NET Core development by reviving the classic aspx extension and blending it with modern architectural patterns. This framework supports two primary syntaxes for view design: the standard syntax, which echoes the inline code style of traditional ASP.NET Web Forms and Classic ASP, and the more contemporary Razor syntax. While both are viable, the standard syntax serves as the foundational approach in CodeBehind, offering familiarity for developers transitioning from older .NET ecosystems. In this article, we'll delve into designing views using the standard syntax, highlighting its similarities to legacy systems, key differences, and best practices, including examples for control structures like conditionals and loops. Although the standard syntax provides a robust starting point, the framework recommends Razor for its cleaner integration and modern feel in most scenarios.

Understanding ASPX Extension and View Pages in CodeBehind

In the CodeBehind framework, the aspx extension forms the core of the view layer, where presentation logic is defined through files that support both standard syntax (using constructs like <%= %> for expressions) and Razor syntax (prefixed with @). These aspx pages act as dynamic views that can handle requests independently or in conjunction with controllers and models, promoting a modular structure that separates concerns more effectively than traditional setups. Unlike Microsoft's Razor Pages, which rely on cshtml files, CodeBehind's views in aspx format allow for direct execution, with automatic syntax detection—standard being the default unless Razor directives like @page are present.

Standard Syntax in CodeBehind: Key Concepts

The standard syntax in the CodeBehind framework is implemented within aspx files, where it is automatically distinguished from Razor syntax. Mixing the two syntaxes in a single file is not permitted. It closely resembles the syntax found in Microsoft's former Web Forms in ASP.NET Standard and Classic ASP pages, though minor differences may arise due to CodeBehind's independent development by the Elanat team.

Here's a basic example of an HTML page combined with standard syntax:

<%@ Page Controller="YourProjectName.DefaultController" Model="YourProjectName.DefaultModel" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title><%=model.PageTitle%></title>
</head>
<body>
    <%=model.BodyValue%>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This setup defines the page attributes at the top and embeds dynamic content using expressions like <%=model.PageTitle%>.

Implicit Standard Expressions

Implicit expressions begin with <%= and conclude with %> , enclosing C# code:

<p><%=DateTime.Now%></p>
<p><%=DateTime.IsLeapYear(2024)%></p>
Enter fullscreen mode Exit fullscreen mode

These render the evaluated results directly into the HTML output.

Code Blocks

Code blocks are delimited by <% and %> , allowing multi-line C# statements:

Standard Syntax Example for Code Block:

<%
    string Note = "Elanat CMS was created to be a reliable system in .NET and an honor for .NET programmers and can be compared to other systems under PHP and JAVA.";
%>

<p><%=Note%></p>
Enter fullscreen mode Exit fullscreen mode

This enables variable assignments and logic within the view.

Page Attributes in Standard Syntax

Attributes are specified at the top of the page within <%@ and %> directives, using key-value pairs where values are enclosed in double quotes.

  • Model Attribute: Links to a model class.
  <%@ page model="YourProjectName.DefaultModel" %>
Enter fullscreen mode Exit fullscreen mode
  • Controller Attribute: Specifies the controller class.
  <%@ page controller="YourProjectName.DefaultController" %>
Enter fullscreen mode Exit fullscreen mode
  • Layout Attribute: Sets the layout file path.
  <%@ page layout="/main-layout.aspx" %>
Enter fullscreen mode Exit fullscreen mode
  • Template Attribute: Defines a template file.
  <%@ page template="/templates/template1.aspx" %>
Enter fullscreen mode Exit fullscreen mode
  • Break Attribute: Enables break functionality (set to "true").
  <%@ page break="true" %>
Enter fullscreen mode Exit fullscreen mode
  • IsLayout Attribute: Marks the page as a layout (set to "true").
  <%@ page islayout="true" %>
Enter fullscreen mode Exit fullscreen mode

Multiple attributes can be combined in a single directive, separated by spaces:

<%@ Page Controller="YourProjectName.DefaultController" Model="YourProjectName.DefaultModel" Layout="/main-layout.aspx" %>
Enter fullscreen mode Exit fullscreen mode

Control Structures

The standard syntax supports C# control structures within code blocks, allowing conditional rendering and iteration directly in the view. These are embedded using <% %> for the logic, with HTML markup in between.

If Statement Example:

<% if (IsTrue) { %>
    <p>This condition is true.</p>
<% } else { %>
    <p>This condition is false.</p>
<% } %>
Enter fullscreen mode Exit fullscreen mode

This renders different content based on the boolean value of IsTrue.

For Loop Example:

<% for (int i = 1; i <= 5; i++) { %>
    <p>Iteration number: <%=i%></p>
<% } %>
Enter fullscreen mode Exit fullscreen mode

This loops five times, outputting numbered paragraphs.

Foreach Loop Example:

<% foreach (NameValue nv in NameValues) { %>
    <b>Name: <%=nv.Name%></b>
    <p>Value: <%=nv.Value%></p>
<% } %>
Enter fullscreen mode Exit fullscreen mode

This iterates over a collection (e.g., NameValues), displaying each item's name and value in formatted HTML.

These structures provide flexibility for dynamic content generation, similar to Classic ASP, but integrated seamlessly with CodeBehind's model and controller bindings.

Conclusion

View design with standard syntax in the CodeBehind framework offers a nostalgic yet powerful way to build dynamic web pages under ASP.NET Core, leveraging the familiar inline code patterns of older .NET technologies while benefiting from modern modularity and performance gains. By using aspx files with expressions, code blocks, and control structures like if statements, for loops, and foreach iterations, developers can create responsive views that integrate tightly with backend logic. However, for most contemporary projects, the framework recommends adopting Razor syntax due to its concise, readable format and closer alignment with current .NET best practices, potentially simplifying maintenance and collaboration. Explore both to find the best fit for your application's needs.

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)