DEV Community

Elanat Framework
Elanat Framework

Posted on

Using the Segment Feature

Segment is an attribute that can be applied to ASPX pages. When this feature is enabled, all path parts that appear after the aspx file path are treated as segments and routed to the same executing page. Segment is one of the innovative ideas of the Elanat team. By enabling Segment, you gain full control over URL paths.


Enabling Segment

Razor Syntax

@page
+@segment
<!DOCTYPE html>
<html>
...
Enter fullscreen mode Exit fullscreen mode

Standard ASPX Syntax

<%@ Page Segment="true" %>
<!DOCTYPE html>
<html>
...
Enter fullscreen mode Exit fullscreen mode

How Segment Works

If you enable Segment on the following path:

/page/about.aspx
Enter fullscreen mode Exit fullscreen mode

Any additional path parts after this address will be considered segments, and the executable file /page/about.aspx will still be executed.

Example

/page/about.aspx/segment1/segment2/.../segmentN
Enter fullscreen mode Exit fullscreen mode

If Segment is enabled on a file such as:

/page/about/Default.aspx
Enter fullscreen mode Exit fullscreen mode

You can still access it using both of the following formats:

/page/about/Default.aspx/segment1/segment2/.../segmentN
Enter fullscreen mode Exit fullscreen mode

or

/page/about/segment1/segment2/.../segmentN
Enter fullscreen mode Exit fullscreen mode

Accessing Segment Values

You can access Segment values in all three layers: View, Controller, and Model.

Segments are zero-based indexed.

Given the URL:

/page/about/segment1/segment2/segment3
Enter fullscreen mode Exit fullscreen mode
  • Segment.GetValue(0) returns "segment1"
  • Segment.GetValue(1) returns "segment2"
  • Segment.GetValue(2) returns "segment3"

In general:

Segment.GetValue(n)
Enter fullscreen mode Exit fullscreen mode

returns the segment at index n (starting from 0).


Example in View (Razor)

<b>segment 1 is: @Segment.GetValue(0)</b>
<b>segment 2 is: @Segment.GetValue(1)</b>
Enter fullscreen mode Exit fullscreen mode

Example in View (Standard ASPX)

<b>segment 1 is: <%= Segment.GetValue(0) %></b>
<b>segment 2 is: <%= Segment.GetValue(1) %></b>
Enter fullscreen mode Exit fullscreen mode

Example in Controller

using CodeBehind;

namespace YourProjectName
{
    public partial class DefaultController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
+           Write(Segment.GetValue(0));
+           Write(Segment.GetValue(1));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

By activating Segment, you no longer need to use query strings to pass parameters through the URL.


Important Behavior

If you enable Segment on a specific path, ASPX view files located in its subdirectories will not be executed.

Example

If Segment is enabled on:

/page/about/Default.aspx
Enter fullscreen mode Exit fullscreen mode

Then the following path will no longer be accessible:

/page/about/license/Default.aspx
Enter fullscreen mode Exit fullscreen mode

Checking Segment Existence

You can use the Exist method to check whether a segment value exists before accessing it.

@if (!Segment.Exist(0))
{
    <b>Value does not exist</b>
}
Enter fullscreen mode Exit fullscreen mode

Segment in Default MVC Configuration

In the default MVC configuration, the Segment feature is enabled automatically in controller routing.

using CodeBehind;

public partial class user : CodeBehindController
{
    public void PageLoad(HttpContext context)
    {   
        Write(Segment.GetValue(0)); // path: /user/{id}
    }
}
Enter fullscreen mode Exit fullscreen mode

Rewrite ASPX Path as Directory

One of the interesting ideas introduced by the Elanat team in the CodeBehind framework is the ability to rewrite an aspx file path as a directory name.

When this option is enabled, routes leading to an aspx file without the .aspx extension will be treated as directory paths.

[CodeBehind options]; do not change order
...
rewrite_aspx_file_to_directory=true
...
Enter fullscreen mode Exit fullscreen mode

Note: You can safely enable this option because this rewrite does not introduce any additional processing load.

Example

Access:

/page/contact.aspx
Enter fullscreen mode Exit fullscreen mode

Using:

/page/contact
Enter fullscreen mode Exit fullscreen mode

And also:

/page/contact/segment1/segment2/.../segmentN
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)