<?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>WiredPrairie &#187; ASP.NET</title>
	<atom:link href="http://www.wiredprairie.us/blog/index.php/archives/tag/asp-net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wiredprairie.us/blog</link>
	<description>A little bit of everything: software, apps, usability, programming, design and whatever else</description>
	<lastBuildDate>Sun, 05 Feb 2012 23:36:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Custom RouteHandler in ASP.NET 4.0</title>
		<link>http://www.wiredprairie.us/blog/index.php/archives/921</link>
		<comments>http://www.wiredprairie.us/blog/index.php/archives/921#comments</comments>
		<pubDate>Fri, 12 Mar 2010 01:34:00 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.wiredprairie.us/blog/index.php/archives/921</guid>
		<description><![CDATA[It’s great that some of the innovations from ASP.NET MVC 1.0 were moved into the ASP.NET 4.0 platform. One of those was the RouteTable. I hadn’t written a custom RouteHandler before, so I thought I’d do a simple one as a demo for myself (and any others who are interested). This is just an example [...]]]></description>
			<content:encoded><![CDATA[<p>It’s great that some of the innovations from ASP.NET MVC 1.0 were moved into the ASP.NET 4.0 platform. One of those was the <strong>RouteTable</strong>. I hadn’t written a custom RouteHandler before, so I thought I’d do a simple one as a demo for myself (and any others who are interested).</p>
<p>This is just an example of how to build one – <strong>it’s not secure</strong>. </p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Web;
<span style="color: blue">using </span>System.Web.Security;
<span style="color: blue">using </span>System.Web.SessionState;
<span style="color: blue">using </span>System.Web.Routing;
<span style="color: blue">using </span>System.IO;

<span style="color: blue">namespace </span>TestWebApplication1
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Global </span>: System.Web.<span style="color: #2b91af">HttpApplication
    </span>{
        <span style="color: blue">void </span>Application_Start(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)
        {
            <span style="color: #2b91af">RouteTable</span>.Routes.Add(<span style="color: blue">new </span><span style="color: #2b91af">Route</span>(<span style="color: #a31515">&quot;Assets/{locale}/{assetID}&quot;</span>, <span style="color: blue">new </span><span style="color: #2b91af">CustomRouteHandler</span>()));
        }
    }

    <span style="color: blue">public class </span><span style="color: #2b91af">CustomRouteHandler </span>: <span style="color: #2b91af">IRouteHandler
    </span>{
        <span style="color: blue">public </span><span style="color: #2b91af">IHttpHandler </span>GetHttpHandler(<span style="color: #2b91af">RequestContext </span>requestContext)
        {
            <span style="color: blue">return new </span><span style="color: #2b91af">SimpleFileHttpHandler</span>(requestContext);
        }
    }

    <span style="color: blue">public class </span><span style="color: #2b91af">SimpleFileHttpHandler </span>: <span style="color: #2b91af">IHttpHandler
    </span>{
        <span style="color: blue">private </span><span style="color: #2b91af">RequestContext </span>_requestContext;
        <span style="color: blue">private </span><span style="color: #2b91af">HttpContext </span>_httpContext;

        <span style="color: blue">public </span><span style="color: #2b91af">RequestContext </span>RequestContext { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; }

        <span style="color: blue">public </span>SimpleFileHttpHandler(<span style="color: #2b91af">RequestContext </span>requestContext)
        {
            _requestContext = requestContext;
        }

        <span style="color: blue">public bool </span>IsReusable
        {
            <span style="color: blue">get </span>{ <span style="color: blue">return false</span>; }
        }

        <span style="color: blue">public void </span>ProcessRequest(<span style="color: #2b91af">HttpContext </span>context)
        {
            _httpContext = context;

            <span style="color: blue">string </span>assetID = _requestContext.RouteData.Values[<span style="color: #a31515">&quot;assetID&quot;</span>].ToString();
            <span style="color: blue">string </span>locale = _requestContext.RouteData.Values[<span style="color: #a31515">&quot;locale&quot;</span>].ToString();

            <span style="color: blue">if </span>(<span style="color: blue">string</span>.IsNullOrWhiteSpace(assetID) || <span style="color: blue">string</span>.IsNullOrWhiteSpace(locale)) {
                <span style="color: blue">throw new </span><span style="color: #2b91af">ArgumentException</span>();
            }
            <span style="color: green">// this is not adaquate and definitely not secure as it would allow any file to be selected and downloaded
            // needs to prevent any sort of hack attempts using encoded paths, etc. should just be a relative path within the
            // folder that contains the content and no more.
            </span><span style="color: blue">string </span>path = _httpContext.Request.MapPath(<span style="color: #a31515">&quot;~/Content/&quot; </span>+ assetID, <span style="color: #a31515">&quot;&quot;</span>, <span style="color: blue">false</span>);
            <span style="color: blue">if </span>(<span style="color: #2b91af">File</span>.Exists(path))
            {
                <span style="color: green">// hard coded to the image/jpg type (obviously needs to adjust)
                </span>context.Response.AddHeader(<span style="color: #a31515">&quot;Content-Type&quot;</span>, <span style="color: #a31515">&quot;image/jpg&quot;</span>);
                context.Response.AddHeader(<span style="color: #a31515">&quot;Content-Length&quot;</span>, <span style="color: blue">new </span><span style="color: #2b91af">FileInfo</span>(path).Length.ToString());
                context.Response.WriteFile(path);
            }
        }
    }

}</pre>
<p>To use it, I created a folder called Content and copied one of the sample photos included with Windows into the folder, and then modified Default.aspx:</p>
<pre class="code"><span style="background: yellow">&lt;%</span><span style="color: blue">@ </span><span style="color: maroon">Page </span><span style="color: red">Title</span><span style="color: blue">=&quot;Home Page&quot; </span><span style="color: red">Language</span><span style="color: blue">=&quot;C#&quot; </span><span style="color: red">MasterPageFile</span><span style="color: blue">=&quot;~/Site.master&quot; </span><span style="color: red">AutoEventWireup</span><span style="color: blue">=&quot;true&quot;
    </span><span style="color: red">CodeBehind</span><span style="color: blue">=&quot;Default.aspx.cs&quot; </span><span style="color: red">Inherits</span><span style="color: blue">=&quot;TestWebApplication1._Default&quot; </span><span style="background: yellow">%&gt;

</span><span style="color: blue">&lt;</span><span style="color: maroon">asp</span><span style="color: blue">:</span><span style="color: maroon">Content </span><span style="color: red">ID</span><span style="color: blue">=&quot;HeaderContent&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">ContentPlaceHolderID</span><span style="color: blue">=&quot;HeadContent&quot;&gt;
&lt;/</span><span style="color: maroon">asp</span><span style="color: blue">:</span><span style="color: maroon">Content</span><span style="color: blue">&gt;
&lt;</span><span style="color: maroon">asp</span><span style="color: blue">:</span><span style="color: maroon">Content </span><span style="color: red">ID</span><span style="color: blue">=&quot;BodyContent&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">ContentPlaceHolderID</span><span style="color: blue">=&quot;MainContent&quot;&gt;
    &lt;</span><span style="color: maroon">h2</span><span style="color: blue">&gt;
        </span>Welcome to the Route Table Demonstrator
    <span style="color: blue">&lt;/</span><span style="color: maroon">h2</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">h3</span><span style="color: blue">&gt;</span>This one should work as it's using the not-so-magical route table and a custom iroutehandler.<span style="color: blue">&lt;/</span><span style="color: maroon">h3</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">p</span><span style="color: blue">&gt;
        </span><span style="color: red">&amp;lt;</span>img src=<span style="color: red">&amp;quot;</span>/Assets/en-us/Penguins.jpg<span style="color: red">&amp;quot; </span>width=<span style="color: red">&amp;quot;</span>320<span style="color: red">&amp;quot; </span>height=<span style="color: red">&amp;quot;</span>200<span style="color: red">&amp;quot; </span>/<span style="color: red">&amp;gt;
    </span><span style="color: blue">&lt;/</span><span style="color: maroon">p</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">p</span><span style="color: blue">&gt;
        &lt;</span><span style="color: maroon">img </span><span style="color: red">src</span><span style="color: blue">=&quot;/Assets/en-us/Penguins.jpg&quot; </span><span style="color: red">width</span><span style="color: blue">=&quot;320&quot; </span><span style="color: red">height</span><span style="color: blue">=&quot;200&quot; /&gt;
    &lt;/</span><span style="color: maroon">p</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">h3</span><span style="color: blue">&gt;</span>THis one shouldn't work due to security in web.config for the folder<span style="color: blue">&lt;/</span><span style="color: maroon">h3</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">p</span><span style="color: blue">&gt;</span><span style="color: red">&amp;lt;</span>img src=<span style="color: red">&amp;quot;</span>/Content/Penguins.jpg<span style="color: red">&amp;quot; </span>/<span style="color: red">&amp;gt;</span><span style="color: blue">&lt;/</span><span style="color: maroon">p</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">p</span><span style="color: blue">&gt;
        &lt;</span><span style="color: maroon">img </span><span style="color: red">src</span><span style="color: blue">=&quot;/Content/Penguins.jpg&quot; /&gt;
    &lt;/</span><span style="color: maroon">p</span><span style="color: blue">&gt;
&lt;/</span><span style="color: maroon">asp</span><span style="color: blue">:</span><span style="color: maroon">Content</span><span style="color: blue">&gt;

</span></pre>
<p><a href="http://11011.net/software/vspaste"></a>In the Content Folder, I added a web.config file to prevent direct access to the content within the folder:</p>
<pre class="code"><span style="color: blue">&lt;?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue">?&gt;
&lt;</span><span style="color: #a31515">configuration</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">authorization</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">deny </span><span style="color: red">users</span><span style="color: blue">=</span>&quot;<span style="color: blue">*</span>&quot;<span style="color: blue">/&gt;
    &lt;/</span><span style="color: #a31515">authorization</span><span style="color: blue">&gt;

  &lt;/</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">system.webServer</span><span style="color: blue">&gt;
     &lt;</span><span style="color: #a31515">modules </span><span style="color: red">runAllManagedModulesForAllRequests</span><span style="color: blue">=</span>&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">/&gt;
  &lt;/</span><span style="color: #a31515">system.webServer</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">configuration</span><span style="color: blue">&gt;

</span></pre>
<p>This should work without modification on IIS7+ and Visual Studio 2010.</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.wiredprairie.us/blog/wp-content/uploads/2010/03/image3.png" width="400" height="291" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiredprairie.us/blog/index.php/archives/921/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disabling automatic Sys.UI.Control attachment</title>
		<link>http://www.wiredprairie.us/blog/index.php/archives/890</link>
		<comments>http://www.wiredprairie.us/blog/index.php/archives/890#comments</comments>
		<pubDate>Sun, 17 Jan 2010 18:26:54 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.wiredprairie.us/blog/index.php/archives/890</guid>
		<description><![CDATA[If you’re using the Microsoft Ajax Library (learn), you may not always want to start the automatic “attach” process that takes place when the page loads. It’s easy to disable, but not yet documented any place I could find easily. &#60;script src=&#34;Scripts/MicrosoftAjax/Start.debug.js&#34; type=&#34;text/javascript&#34;&#62;&#60;/script&#62; &#60;script type=&#34;text/javascript&#34;&#62; var ajaxPath = &#34;&#34;; Sys.activateDom = false; All you must [...]]]></description>
			<content:encoded><![CDATA[<p>If you’re using the Microsoft Ajax Library (<a href="http://www.asp.net/ajaxlibrary/learn.ashx" target="_blank">learn</a>), you may not always want to start the automatic “attach” process that takes place when the page loads. It’s easy to disable, but not yet documented any place I could find easily.</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">script </span><span style="color: red">src</span><span style="color: blue">=&quot;Scripts/MicrosoftAjax/Start.debug.js&quot; </span><span style="color: red">type</span><span style="color: blue">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">script </span><span style="color: red">type</span><span style="color: blue">=&quot;text/javascript&quot;&gt;

    var </span>ajaxPath = <span style="color: #a31515">&quot;&quot;</span>;

    Sys.activateDom = <span style="color: blue">false</span>;</pre>
<p>All you must do is set <strong>Sys.activateDom</strong> to <strong>false</strong> as shown above (make sure this is set <strong>after</strong> the new Start.js JavaScript file loads, otherwise your code will crash when you try to set the Sys object before it has been properly constructed). </p>
<p>Then, to begin the attach process, just call <strong>Sys.activateElements</strong>:</p>
<pre class="code">Sys.activateElements(document.documentElement);</pre>
<p>In the code line above, though I’ve specified that I want the entire HTML document activated, you could provide any element you want as a starting point (for example to optimize the use of the library and prevent unnecessary DOM searching for example).</p>
<p>I’m adding the delay in some JavaScript code because I wanted to set up a few variables in advance of the attach occurring. I tend to write my JavaScript code in an object oriented fashion these days (using the prototype pattern), including code that is interacting with the DOM. In this case, I’ll create a class that represents the logic of the page rather than following the typical purely functional model that is done on many JavaScript pages. But, when using the “<strong>eval</strong>” syntax of the Microsoft Ajax library “{{ code }}”, occasionally, I’ll need to delay the <strong>eval</strong> or the page will crash. </p>
<p>From my recent post on making a simple <a href="http://www.wiredprairie.us/blog/index.php/archives/885" target="_blank">command extension to the Microsoft Ajax library</a>, I wanted to make that more object oriented by referring to an instance of my class, rather than pointing directly to a function:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">body sys</span><span style="color: blue">:</span><span style="color: red">attach</span><span style="color: blue">=&quot;wpc&quot;
    </span><span style="color: #a31515">wpc</span><span style="color: blue">:</span><span style="color: red">onbubbleevent</span><span style="color: blue">=&quot;{{$view.onCommand}}&quot;
    </span><span style="color: #a31515">xmlns</span><span style="color: blue">:</span><span style="color: red">sys</span><span style="color: blue">=&quot;javascript:Sys&quot; </span><span style="color: #a31515">xmlns</span><span style="color: blue">:</span><span style="color: red">wpc</span><span style="color: blue">=&quot;javascript:WiredPrairie.Commanding&quot;&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>$view </strong>represents the instance of my page’s behavior. However, if the attach were to occur too early, this variable is not yet set. I’m using the slick <a href="http://www.asp.net/ajaxlibrary/HOW%20TO%20Load%20a%20Custom%20Script%20with%20Dependencies.ashx">script loading functionality</a> of the ajax library, specifying the various JavaScript libraries and their dependencies, including my page’s behavior. It’s not until that JavaScript code is loaded that the code can create an instance – and that could be AFTER the page has already done the attach logic. The attach happens before <strong>Sys.onReady</strong> for example. (Sys.<strong>onDomReady</strong> happens before <strong>onReady</strong>, but not all JavaScript files may have been downloaded).</p>
<pre class="code">Sys.onReady(<span style="color: blue">function</span>() {
    $view = <span style="color: blue">new </span>WiredPrairie.MainView();

    Sys.activateElements(document.documentElement);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>When using the <strong>sys:attach</strong> attribute, note that the attach and instantiation process happens <strong>before</strong> any code you’ve specified in <strong>onReady</strong> is executed (Microsoft currently uses the same method for determining when everything is ready by adding a function call to <strong>onReady</strong> – but their call is first in the queue). </p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiredprairie.us/blog/index.php/archives/890/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

