How to put HTML code into a .resx resource file?
Asked Answered
A

5

9

Strange, that no one asked this before....

I am creating templated HTML emails for 4 languages. I want to put the HTML templates into my .resx files to have easy, internationalized access to them from code. Like so:

.resx file:

<data name="BodyTemplate" xml:space="preserve">
    <value><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
            <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
            <title>Title</title>
            ...
        </body>
        </html>
    </value>
</data>

But, obviously, the compiler complains about the HTML inside the .resx file. Is there a way to use HTML (or XML at all) in .resx files. How?

I am using .NET version 4 and dotLiquid as templating Engine, if that matters.

Alvarado answered 17/2, 2015 at 9:14 Comment(2)
What does the comiler complain about exactly? And how do you add the HTML/XML files to your resource files?Dysfunction
If nobody knows how to do that you could write a "serializer" to escape the code that it is no html code anymore like < to #le# and when getting the data you could restore that to a less sign. Hope that helps if no other solution is found to this problem.Macnamara
B
16

Suggestion: Create the file you want, name it the way you want, e.g "my_template.html" then add this file to your project.

Click on it, then select in the properties window "Build Action" and set it to "Embedded Resource".

Whenever you want to access this file, you can use something like this (no with proper using block:

    public static string ReadTextResourceFromAssembly(string name)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            return new StreamReader( stream ).ReadToEnd();
        }
    }

Tailor it to your needs. The method above obtains the resource, say you have put your resource in your project MyProject in a subdirectory "HtmlTemplates and called it "my_template.html", then you can access it by the name MyProject.HtmlTemplates.my_template.html

You can then write it out to file or use it directly, etc.

This has some major benefits: You see your html file in your project, it has the html extension, so editing it in Visual Studio has syntax highlighting and all tools applied to .html files.

I have a bunch of those methods for my unit tests, this one extracts the data to a file:

    public static void WriteResourceToFile(string name, string destination)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            if ( stream == null )
            {
                throw new ArgumentException( string.Format( "Resource '{0}' not found", name), "name" );
            }

            using ( var fs = new FileStream( destination, FileMode.Create ) )
            {
                stream.CopyTo( fs );
            }
        }
    }
Bovid answered 17/2, 2015 at 9:30 Comment(2)
You should also encapsule the stream within a using-block, in order for it to get disposed correctly.Dysfunction
@Aschratt, thanks for hinting. I missed this somehow on my first method. Fix it now.Bovid
D
7

Put encoded html in the .resx file and then getting back the html using

@Html.Raw(Server.HtmlDecode(...resource...));
Drais answered 17/2, 2015 at 9:35 Comment(1)
I doubt this will work. Encoded HTML is still HTML (with all the angle brackets still sthere). Thus it will get me trouble still.Alvarado
H
4

This worked form me: Put all your html code in value resource file and then retrieve it using @Html.Raw(...) like I did it here

 <td class="red" colspan="2" align="center">
                    <span style="color: #b01c1c;">
                        <strong>@Html.Raw(Resources.Listino.Cassette</strong>
                    </span>
 </td>

in the Listino resource File: Resource File

Hom answered 17/6, 2017 at 9:47 Comment(3)
It seems like not working. Because when you enter html code it's automatically add new string line. In your case, it is String1.Subulate
Delete the new record using standard commandsHom
Thanks for replying back 7 years later :)Subulate
S
1

Well, I found a way like that:

First I created partial view in multiple languages and put the path of this partial views into .resx files. Next, I called @Html.Partial() method with the Resource string.

Please see the images below:

Views

Resx file

Subulate answered 25/7, 2017 at 18:0 Comment(0)
C
0

Using Blazor (at least Server Prerendered mode, not tested with other), you can create a MarkupString (from Microsoft.AspNetCore.Components namespace) with the localizer value as parameter, or cast it.
The html will be rendered properly.

For example:

@inject IStringLocalizer<Resource> localizer

<span>@(new MarkupString(text))</span> // If assigning localizer value to a string 
<span>@((MarkupString)localizer["TextWithHtml"].Value)</span> // If using localizer value directly, need to use .Value before casting

@code {
    private string text;

    private void SetText()
    {
        text = localizer["TextWithHtml"];
    }
}


Constantine answered 27/7, 2023 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.