DEV Community

Cover image for Elanat CMS 2.2, Ability to Replace Pages Before Response
elanatframework
elanatframework

Posted on

Elanat CMS 2.2, Ability to Replace Pages Before Response

One of the powerful features of Elanat CMS is request management. There are several structures for managing requests in Elanat CMS, 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 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>

...
Enter fullscreen mode Exit fullscreen mode

Before load path reference View

@page
controller Elanat.ActionBlockingVideoForPeopleUnder14YearsOldController
Enter fullscreen mode Exit fullscreen mode

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

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");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Load path View

@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>
Enter fullscreen mode Exit fullscreen mode

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

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;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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

Write("replace");
...
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)