简体   繁体   中英

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

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.

Suggestion: Create the file you want, name it the way you want, eg "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 );
            }
        }
    }

encoded html.resx文件,然后又回到了html使用

@Html.Raw(Server.HtmlDecode(...resource...));

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: 资源文件

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:

观看次数

Resx 文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM