ASP.net HTMLDecode not working for Resource entries - asp.net

I have a ASP.net MVC project, which utilizes resource entries (.resx) through out the project.
Few the resources fed, have HTML in it
example: Hello <b>World!</b>
With paragraphs href and more. As the resources are stored in an XML, the entries are HTMLEncoded
i.e the above example looks like this
eg: Hello <b>World!</b>
Due to this, wherever the resources are displayed, the HTML formatting does not render, and instead the HTML is displayed as visible text.
I tried to use HttpUtility.HTMLDecode and Server.HTMLDecode, but both wont work.
What is wrong? Any other work around resources?

Both of the following work fine for me:
<%= Resource.MyResource %><br />
<asp:Label runat="server" Text="<%$ Resources:Resource, MyResource %>" /><br />
A Resource entry such as <b>Text</b> is displayed in bold by the browser.
Some Controls do automatic HTML encoding of their inputs. Could that be what's happening for you?

Related

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control

asp.net expression builder within a larger string

Is there a way to embed an expression builder within a larger string?
I'm trying to create a hyperlink where the URL includes an embedded expression. I tried writing
<asp:hyperlink id="add" runat="server" text="Add" NavigateUrl="~/admin/customer.aspx?code=<%$ AppSettings:salecode %>&action=add" />
But this didn't work -- the "<%$" and all just got included as text, no substitution was done.
Of course I could build the URL in code, it's no big deal, but I just wonder if it's possible to do this in the ASPX file somehow.
PS Just as a test, I tried putting
<asp:label id="test" runat="server" text="<%$ AppSettings:salecode %>" />
and that worked fine, so it's not that I'm mis-spelling the setting name or something dumb like that.
Try using single quotes around the NavigateUrl property:
NavigateUrl='~/admin/customer.aspx?code=<%$ AppSettings:salecode %>&action=add'

~/ not starting at app root

I know this may be a basic question, but it is driving me crazy. I have an asp.net (4.0 framework) application that I have been working on. I have a masterpage in the root directory, and a secondary masterpage in a subdirectory. The secondary masterpage inherits the site master.
The issue is that even though I used ~/ to describe the location of the resource ("<img src="~/Images/myImage.jpg" />) they are not loading.
Using the console on firebug, I get this error: "NetworkError: 404 Not Found - http://localhost:4601/Account/~/Images/myImage.jpg"
What do I need to do to correctly translate resources from masterpage to masterpage across subfolders? And what is it that I am misunderstanding about '~/'?
Using
<img src="~/Images/myImage.jpg" />
Is mixing HTML code with .Net ASP code. The tilde (~) is not something of the HTML markup and this is why it does not produce what you want.
To make it works, you need to change the source with the <% %> tag that will let you add ASP code that will be translated into HTML code when processing.
<img src="<%= Page.ResolveUrl("~/Images/myImage.jpg") %>" />
Inside ASP.NET tag, you should use the ResolveURL that will transform the URL into something that the HTML will be able to understand.
If you do not want to use this trick, you can also use instead of the HTML img tag the ASP.NET image control. This will automatically execute the ResolveUrl
<asp:Image runat="server" ID="imgHelp" ImageUrl="~/Images/myImage.jpg" />
<img src="<%= Page.ResolveUrl("~/Images/myImage.jpg") %>" />
or
<img src="<%= Control.ResolveUrl("~/Images/myImage.jpg") %>" />

ASP.NET localization translating HTML Content

I am localizing an ASP.NET site using automatic feature , where it creates a local resource file and adds the meta keyword to asp.net controls. However I have a lot of HTML like below
<h2> Welcome to our page"</h2>
<li> Option one </li>
Is there a way to get these automatically translated using the automatic localize utility ?
I tried adding the runat="server" for these tags but to no avail.
Also instead of localizing page by page is there a way to localize bulk - it a directory or a site at one go
thanks
You need to use Localize control for static text - for example,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="Welcome to our page" meta:resourcekey="WelcomeMessage" />
</h2>
Alternatively,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="<%$ Resources:WebResources, WelcomeMessage %>" />
</h2>
You can also use syntax such as
<h2><%= Resources.WebResources.WelcomeMessage %></h2>
where Resources.WebResources is strongly typed resource class generated by Visual Studio resource generator. For across page resources, you can create global resources and then refer then using syntax as shown above (meta key will not work for global resources).
See MSDN for more information.

asp.net - Use ascx as a layout template

This is my layout template (ascx without code behind)
<%# Control Language="C#" AutoEventWireup="true" Inherits="ws.helpers.LayoutUC" %>
<div>blah blah blah</div>
<ws:Panel runat="server" ID="left"></ws:Panel>
<ws:Panel runat="server" ID="main"></ws:Panel>
<ws:Panel runat="server" ID="right"></ws:Panel>
Modules will be added into ws:Panel later.
I also allow my user create their own ascx file to custom their page layout. And because of this i do a string replace all dangerous part like script tag (runat="server"), all asp.net html tag, <%, <%#, <#.... from their custom.
Im not worry about XSS, so dont comment on it, and ask why?
I want know your thinking about this. Is is safe? Is it scalable? Is it standard or a bad way?
Have a look at the INaminingContainer Interface http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx.
<asp:YourControl>
<LeftColumn>
<asp:Literal ID="literal1" runat="server" Text="User created literal" />
</LeftColumn>
</asp:YourControl>
In the .ascx from the users, they register your control and insert asp.net code into properties. In the 'YourControl' class you create placeholders and insert the markup set to a specific property into these placeholders. (e.g. everything between <LeftColumn> and </LeftColumn> will the inserted into
<asp:Placeholder ID="PlaceholderLeftColumn" runat="server"/>
Edit: I summed some of the TemplateContainer issue up and posted it here: http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
You are allowing user-uploaded content; this is inherently unsafe and there are whole books dedicated to best practices. Given that you are doing it anyway, as long as you make sure you scrub the input, is it scalable? You are allowing creation of user-uploaded files on your site. How many will there be? How many users? What about load-balancing? This solution will not scale for many users, files, or servers.
It sounds like you are trying to create a simple CMS. Why not use one that exists currently, or adopt parts of an open source solution?

Resources