Elanat CMS 2.2, Ability to Replace Pages Before Response
Tags: csharp, dotnet, cms, github
Published: 2024-05-17 22:58:01

One of the powerful features of [Elanat CMS](https://elanat.net) is request management. There are several structures for managing requests in [Elanat CMS](https://github.com/elanatframework/Elanat), one of which is before load path reference; the before load path reference structure checks the request before it is executed and the request can be stopped.

By default, along with the main core of Elanat CMS, there are several before load path references, which we mentioned in the list below:

 - Check attachment count limitation
 - Check attachment size limitation
 - Check login try count limitation
 - Check next search time interval limitation
 - ...

In Elanat CMS, you can add new pages or methods in the before load path reference structure and manage access to each path as desired.

In Elanat CMS version 2.2, we added a new possibility for the before load path reference structure; In version 2.2 and later, a page can be displayed instead of the request.

## Example

**Only registered users over 14 years old can watch videos.**

before load path reference is an xml file located in the following path:
`root\App_Data\before_load_path_reference_list\before_load_path_reference_list.xml`

As below, we added a new reference to the before load path reference file. This reference is located in `root/action/system_access/reference/blocking_video_for_people_under_14_years_old/Default.aspx` and is called before executing any request in the `/video` path; if the response of the executed page is `false`, the request will encounter an access error. In version 2.2 of the Elanat content management system, the `replace` value can also be used; if replace is included in the response, the error page is not displayed and the request is stopped; however, we can send the content we want to the request using the `context.Response.WriteAsync` method.

before_load_path_reference_list.xml file
```xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<before_load_path_reference_root>

  <before_load_path_reference_list>

    <reference type="url" check_type="page" exist="false" start_by="true" end_by="false" regex_match="false" active="true" reason="watching_videos_is_not_possible_for_people_under_14_years_old" name="blocking_video_for_people_under_14_years_old">
        <path_value>/video</path_value>
        <load_value><![CDATA[/action/system_access/reference/blocking_video_for_people_under_14_years_old/Default.aspx]]></load_value>
    </reference>

...
```

Before load path reference View
```html
@page
controller Elanat.ActionBlockingVideoForPeopleUnder14YearsOldController
```

The above code is the reference page. The following codes are also the reference page controller. The following controller checks so that if the user is a guest or a registered user who is under 14 years old, the content will not be displayed and instead the content of the page will be displayed in the following path:
`root/message/watch_video_only_for_over_14_years/Default.aspx`

Controller
```csharp
using CodeBehind;

namespace Elanat
{
    public partial class ActionBlockingVideoForPeopleUnder14YearsOldController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            CurrentClientObjectClass client = new CurrentClientObjectClass();

            if (client.RoleDominantType != "guest")
            {
                DataUse.User user = new DataUse.User();
                user.FillCurrentUser(client.UserId);

                DateTime BirthDate = DateTime.ParseExact(user.UserBirthday, "yyyy/MM/dd", null);
                DateTime CurrentDate = DateTime.Now;

                int Age = CurrentDate.Year - BirthDate.Year;
                if (BirthDate > CurrentDate.AddYears(-Age))
                    Age--;

                if (Age > 14)
                    return;
            }

            context.Response.WriteAsync(PageLoader.LoadWithServer(StaticObject.SitePath + "message/watch_video_only_for_over_14_years/Default.aspx"));

            Write("replace");
        }
    }
}
```

Load path View
```html
@page
@model Elanat.WatchVideoOnlyForOver14YearsModel()

<b>Dear @model.UserSiteName, watching the video is only possible for users over 14 years of age who have registered on the website.</b>
```

The View page above, along with the Model class below, displays the message that it is not possible to show videos to users who are not registered or under 14 years old.

Load path Model
```csharp
using CodeBehind;

namespace Elanat
{
    public string UserSiteName { get; set; }

    public partial class WatchVideoOnlyForOver14YearsModel : CodeBehindModel
    {
        public void CodeBehindConstructor()
        {           
            UserSiteName = (client.RoleDominantType == "guest") ? "Guest" : new CurrentClientObjectClass().UserSiteName;
        }
    }
}
```

Please pay attention again to the code below in the `ActionBlockingVideoForPeopleUnder14YearsOldController` controller class:

```csharp
...
context.Response.WriteAsync(PageLoader.LoadWithServer(StaticObject.SitePath + "message/watch_video_only_for_over_14_years/Default.aspx"));

Write("replace");
...
```

One of the important advantages that the CodeBehind framework has given to the Elanat content management system is the ability to call pages outside of .NET Core. In fact, you can use both the `context.Response.Write` method and the `Write` method belonging to the CodeBehind framework.

## Related links

Elanat CMS on GitHub:
https://github.com/elanatframework/Elanat

Elanat CMS website:
https://elanat.net

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