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>
...
Standard ASPX Syntax
<%@ Page Segment="true" %>
<!DOCTYPE html>
<html>
...
How Segment Works
If you enable Segment on the following path:
/page/about.aspx
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
If Segment is enabled on a file such as:
/page/about/Default.aspx
You can still access it using both of the following formats:
/page/about/Default.aspx/segment1/segment2/.../segmentN
or
/page/about/segment1/segment2/.../segmentN
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
-
Segment.GetValue(0)returns"segment1" -
Segment.GetValue(1)returns"segment2" -
Segment.GetValue(2)returns"segment3"
In general:
Segment.GetValue(n)
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>
Example in View (Standard ASPX)
<b>segment 1 is: <%= Segment.GetValue(0) %></b>
<b>segment 2 is: <%= Segment.GetValue(1) %></b>
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));
}
}
}
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
Then the following path will no longer be accessible:
/page/about/license/Default.aspx
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>
}
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}
}
}
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
...
Note: You can safely enable this option because this rewrite does not introduce any additional processing load.
Example
Access:
/page/contact.aspx
Using:
/page/contact
And also:
/page/contact/segment1/segment2/.../segmentN
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)