ASP.NET - Insert HTML code in Resource Files (.resx)

ASP.NET - Inserire codice HTML nei Resource File (.resx)

We already mentioned the ASP.NET Resource Files a while ago, praising their usefulness to grant the developers an handy, centralized access to literal and media contents troughout our application projects. This is expecially convenient when developing a multilanguage website, as explained in this post). That being said, it's not uncommon to end up having resource items containing HTML code, like shown in the following screenshot:


As you might already know, if you use this HTML_Response resource in your webpage UI - be it aspx, ascx, cshtml - you will have the exact same output shown by the screenshot, including the HTML elements: that's why the ASP.NET web publishing engine will automatically escape, for security reasons, the resource file textual content, including HTML code, entities and tags.

Luckily enough, there's way to disable the auto-escape, depending on which view engine you've chose to adopt.


If you're using Razor - which I personally hope for you - all you need to do is to wrap the resource reference inside the @Html.Raw method, just like shown by the following example:

Please notice that, in the latter example, the whole String.Format result - not the resource string by itself - must be used as a parameter of @Html.Raw.


If you're using ASP.NET you can achieve the same result in two possible ways.

Using the Response.Write ASP.NET shortcut

Using the <asp:Literal> Control

Unfortunately, the latter won't work: you'll have to programmatically set it in code-behind like this:

That's it: happy coding!


About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.