<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fredrik Haglund's blog &#187; ToWebString</title>
	<atom:link href="http://blog.fredrikhaglund.se/blog/tag/towebstring/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fredrikhaglund.se</link>
	<description>Chatter about EPiServer, ASP.NET, CSS and Web Development.</description>
	<lastBuildDate>Tue, 28 Jun 2011 13:37:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>EPiPattern: How to render valid xhtml (part 1/2)</title>
		<link>http://blog.fredrikhaglund.se/blog/2008/12/08/epipattern-how-to-render-valid-xhtml-part-12/</link>
		<comments>http://blog.fredrikhaglund.se/blog/2008/12/08/epipattern-how-to-render-valid-xhtml-part-12/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 12:39:50 +0000</pubDate>
		<dc:creator>Fredrik Haglund</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[EPiPattern]]></category>
		<category><![CDATA[EPiServer CMS]]></category>
		<category><![CDATA[HtmlEncode]]></category>
		<category><![CDATA[script injection]]></category>
		<category><![CDATA[ToWebString]]></category>

		<guid isPermaLink="false">http://blog.fredrikhaglund.se/?p=182</guid>
		<description><![CDATA[How do you inject dynamic content into your web page? Following is a list of the most common methods I find when doing quality and code reviews with some comments and dangerous pitfalls. #1: Inline Expressions with Code Render Blocks Using inline expressions is a shortcut for calling the Write method. Example &#8211; DONT: &#60;h2&#62;&#60;%= [...]]]></description>
			<content:encoded><![CDATA[<p>How do you inject dynamic content into your web page? Following is a list of the most common methods I find when doing quality and code reviews with some comments and dangerous pitfalls.</p>
<h3>#1: Inline Expressions with Code Render Blocks</h3>
<p>Using <a href="http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx" target="_blank">inline expressions</a> is a shortcut for calling the <a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.write.aspx">Write</a> method.</p>
<h4>Example &#8211; DONT:</h4>
<pre class="csharpcode">&lt;h2&gt;&lt;%= CurrentPage.PageName %&gt;&lt;/h2&gt;
&lt;img src=<span class="str">"&lt;%= CurrentPage["</span>ImageUrl<span class="str">"] %&gt;"</span> alt=<span class="str">"&lt;%= CurrentPage["</span>MainIntro<span class="str">"] %&gt;"</span> /&gt;
&lt;div <span class="kwrd">class</span>=<span class="str">"mainarea"</span>&gt;

     &lt;%= CurrentPage[<span class="str">"MainBody"</span>] %&gt;
&lt;/div&gt;</pre>
<p>Since xhtml is just text, writing some more in-between existing static blocks is fast and simple. Probably the easiest way to inject dynamic content but it is also dangerous!</p>
<h4>Example – DO always use HtmlEncode:</h4>
<pre class="csharpcode">&lt;h2&gt;&lt;%= HttpUtility.HtmlEncode(CurrentPage.PageName) %&gt;&lt;/h2&gt;</pre>
<p>What happens if our PageName property contains a &#8220;&lt;&#8221; or &#8220;&amp;&#8221; character in the example above? Your page will not validate and you risk that it breaks down. It can also be exposed to <a title="How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings" href="http://msdn.microsoft.com/en-us/library/a2a4yykt.aspx" target="_blank">script injection</a> if the content is user generated.</p>
<p>To be on the safe side you should always call <em><a href="http://msdn.microsoft.com/en-us/library/73z22y6h.aspx" target="_blank">HttpUtility.HtmlEncode</a></em> on all strings that you inject with inline expressions.</p>
<h4>Example – DO always use a fallback for src-attributes</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">img</span> <span class="attr">src</span><span class="kwrd">="&lt;%= CurrentPage["</span><span class="attr">ImageUrl</span><span class="kwrd">"] ?? "</span>/<span class="attr">Missing</span>.<span class="attr">png</span><span class="kwrd">" %&gt;"</span> <span class="kwrd">/&gt;</span></pre>
<p>If your ImageUrl property is empty you would get a img-tag with an empty src-attribute. This can lead to the page being rendered twice. <a title="How Firefox Handles Empty SRC tags" href="http://geekswithblogs.net/bcaraway/archive/2007/08/24/114945.aspx" target="_blank">FireFox interpret a null src-attribute a relative url and evaluate it using the page url as base returning the same url</a>.</p>
<h3>#2: With EPiServer:Property web control</h3>
<p>Using the EPiServer Property Web Controls instead of inline expressions has several benefits. It could be used for rendering the PageName and MainBody in the example above.</p>
<h4>Example – DO use EPiServer:Property web control</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">h2</span><span class="kwrd">&gt;&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">PropertyName</span><span class="kwrd">="PageName"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;&lt;/</span><span class="html">h2</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">CssClass</span><span class="kwrd">="mainarea"</span> <span class="attr">PropertyName</span><span class="kwrd">="MainBody"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;</span></pre>
<p>1) When rendering EPiServer&#8217;s <em>ToWebString()</em> is used. This does almost the same job as always calling <em>HtmlEncode</em> yourself.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> CreateDefaultControls()
{
    Label target = <span class="kwrd">new</span> Label();
    target.Text = PropertyData.ToWebString();
    CopyWebAttributes(target);
    Controls.Add(target);
}</pre>
<p>2) It enables the Simple Edit feature for the editors without any extra work. (If you do not know what &#8220;Simple Edit&#8221; is, you should read the <a title="EPiServer Documentation" href="http://world.episerver.com/en/Documentation/Categories/Documentation-Type/Manuals/" target="_blank">Editors Manual for EPiServer</a> before doing any more development.)</p>
<p>3) Different types have get different rendering automatically. Commonly used for <em>PageReferences</em> and Url&#8217;s that are rendered as a Hyperlink.</p>
<h4>Example – DO use EPiServer:Property for rendering links if possible</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">EPiServer:PageList</span> <span class="attr">PageLinkProperty</span><span class="kwrd">="NewsArchivePage"</span> <span class="attr">MaxCount</span><span class="kwrd">="5"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">HeaderTemplate</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">h3</span><span class="kwrd">&gt;&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">PropertyName</span><span class="kwrd">="PageLink"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;&lt;/</span><span class="html">h3</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">ul</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">HeaderTemplate</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">ItemTemplate</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">li</span><span class="kwrd">&gt;&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">PropertyName</span><span class="kwrd">="PageLink"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;&lt;/</span><span class="html">li</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">ItemTemplate</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">FooterTemplate</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">ul</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">FooterTemplate</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">EPiServer:PageList</span><span class="kwrd">&gt;</span></pre>
<p>4) Use <em>PageLinkProperty</em> to follow a Page-property to another page and render a value. Takes care of all hassle and calls <em>GetPage()</em> and check for nulls for you.</p>
<h4>Example – DO use EPiServer:Property to render values from another page</h4>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">h3</span><span class="kwrd">&gt;&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">PageLinkProperty</span><span class="kwrd">="FeatureArticlePage"</span> <span class="attr">PropertyName</span><span class="kwrd">="PageLink"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;&lt;/</span><span class="html">h3</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;&lt;</span><span class="html">EPiServer:Property</span> <span class="attr">PageLinkProperty</span><span class="kwrd">="FeatureArticlePage"</span> <span class="attr">PropertyName</span><span class="kwrd">="MainIntro"</span> <span class="attr">runat</span><span class="kwrd">="server"</span> <span class="kwrd">/&gt;&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.fredrikhaglund.se/blog/2008/12/08/epipattern-how-to-render-valid-xhtml-part-12/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

