Skip to main content

ASP.NET – Insert HTML code in Resource Files (.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:

html-in-resource-files-resx

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.

Razor

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.

ASP.NET

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!

 

RELATED POSTS

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.

Close
Read previous post:
Disable Windows 10 Tracking, an application to stop Microsoft’s personal data gathering

We all know that Redmond loves to know more about us: Windows 10 makes no exceptions here, introducing a revamped user...

Close