Using HTML inside resource files Using HTML inside resource files asp.net asp.net

Using HTML inside resource files


You can use the @Html.Raw method inside your view, e.g. @Html.Raw(STRING FROM RESX FILE HERE)


When I faced the same question I decided against having paragraphs directly in resources. Separate text block by simple new lines (Shift+Enter if I'm not mistaken). Then create a helper method to wrap blocks into paragraphs.

using System;using System.Text;using System.Web;namespace MyHtmlExtensions{  public static class ResourceHelpers  {    public static IHtmlString WrapTextBlockIntoParagraphs(this string s)    {      if (s == null) return new HtmlString(string.Empty);      var blocks = s.Split(new string[] { "\r\n", "\n" },                            StringSplitOptions.RemoveEmptyEntries);      StringBuilder htmlParagraphs = new StringBuilder();      foreach (string block in blocks)      {        htmlParagraphs.Append("<p>" + block + "</p>");      }      return new HtmlString(htmlParagraphs.ToString());    }  }}

Then in your view you import your extension method namespace:

<%@ Import Namespace="MyHtmlExtensions" %>

And apply it:

<%= Resources.Texts.HelloWorld.WrapTextBlockIntoParagraphs () %>

I think it is a more cleaner way than putting markup tags into text resources where they in principle do not belong.


You can wrap the data for the resx value field in a CDATA block, <![CDATA[ ]]>.

e.g.

<![CDATA[My text in <b>bold</b>]]>

Depending on how you want to use it, you will probably have to strip off the <![CDATA[ and ]]> within your app.

This approach allows you to include any html markup in the resx file. I do this to store formatted text messages to be displayed in a jquery-ui dialog from various views within an asp.net mvc app.