Dynamics 365 Business Central: How to encode and decode URL/HTML (UrlEncode, UrlDecode, HtmlEncode, and HtmlDecode)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how to encode and decode URL/HTML in Business Central.

First of all, UrlEncode and HtmlEncode do fundamentally different things.
UrlEncode replaces special characters with characters that can be understood by web browsers/web servers for the purpose of addressing, hence URL. For example,

"hello+world = hello world"

to

"hello%2bworld+%3d+hello+world"

HtmlEncode replaces special characters with character strings that are recognised by the HTML engine itself. For example,

"<hello>world</hello>"

to

"&lt;hello&gt;world&lt;/hello&gt;"

In other development languages, there are easy ways to do this, for example, HttpUtility.UrlEncode in C#, so what about in AL Language?
Of course, there are standard methods for this too😁.

codeunit 10 “Type Helper”:

Let me do a simple test. I have created a test page below with four actions.

URL Encode:

URL Decode:

HTML Encode:

HTML Decode:

Test video:

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

page 50111 DecoderEncoder
{
    Caption = 'Decoder/Encoder';
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(InputString; InputString)
                {
                    Caption = 'Input String';
                    ApplicationArea = All;

                    trigger OnValidate()
                    begin
                        if InputString = '' then
                            ClearResults();
                    end;
                }
            }
            group(Results)
            {
                field(URLDescoderEncoder; URLDescoderEncoder)
                {
                    Caption = 'URL Decoder/Encoder';
                    Editable = false;
                    MultiLine = true;
                    ApplicationArea = All;
                }
                field(HTMLDescoderEncoder; HTMLDescoderEncoder)
                {
                    Caption = 'HTML Decoder/Encoder';
                    Editable = false;
                    MultiLine = true;
                    ApplicationArea = All;
                }
            }
        }
    }

    actions
    {
        area(Processing)
        {
            action(URLEncode)
            {
                Caption = 'URL Encode';
                ApplicationArea = All;
                Image = Process;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    ClearResults();
                    ConvertedString := InputString;
                    URLDescoderEncoder := TypeHelper.UrlEncode(ConvertedString);
                end;
            }
            action(URLDescoder)
            {
                Caption = 'URL Decode';
                ApplicationArea = All;
                Image = Process;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    ClearResults();
                    ConvertedString := InputString;
                    URLDescoderEncoder := TypeHelper.UrlDecode(ConvertedString);
                end;
            }
            action(HTMLEncode)
            {
                Caption = 'HTML Encode';
                ApplicationArea = All;
                Image = Process;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    ClearResults();
                    ConvertedString := InputString;
                    HTMLDescoderEncoder := TypeHelper.HtmlEncode(ConvertedString);
                end;
            }
            action(HTMLDescoder)
            {
                Caption = 'HTML Decode';
                ApplicationArea = All;
                Image = Process;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    ClearResults();
                    ConvertedString := InputString;
                    HTMLDescoderEncoder := TypeHelper.HtmlDecode(ConvertedString);
                end;
            }
        }
    }

    var
        InputString: Text;
        ConvertedString: Text;
        URLDescoderEncoder: Text;
        HTMLDescoderEncoder: Text;

    local procedure ClearResults()
    begin
        ConvertedString := '';
        URLDescoderEncoder := '';
        HTMLDescoderEncoder := '';
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL