<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jay Harris is Cpt. LoadTest - ASP.Net</title>
    <link>http://www.cptloadtest.com/</link>
    <description>a .net developers blog on improving user experience of humans and coders</description>
    <language>en-us</language>
    <copyright>Jason Harris</copyright>
    <lastBuildDate>Tue, 26 Apr 2011 23:45:23 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>jharris@harrisdesigns.com</managingEditor>
    <webMaster>jharris@harrisdesigns.com</webMaster>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=245e0356-23d5-4212-99bf-583840b1e904</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=245e0356-23d5-4212-99bf-583840b1e904</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The first three parts of this series discuss the events that make up the ASP.NET Page
Life Cycle, the order that these events execute on the page and on the controls, and
that necessary evil called Data Binding, through which you add dynamic content to
your new ASP.NET page. Raising those events happens automatically, without any developer
oversight, but tapping in to those events—making them work for you—requires wiring
special methods called Event Handlers to those events. This wiring can happen automatically
by the system if you follow a few simple steps, or it can happen manually, with developers
connecting each appropriate event to its handler. Both sides have their advantages
as well as their drawbacks, but learning the nuances of event wiring can only strengthen
your ASP.NET repertoire.
</p>
        <blockquote>
          <h3>About the Series
</h3>
          <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
          <p>
Part 1: <a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx" target="_blank">Events
of the ASP.NET Page Life Cycle</a><br />
Part 2: <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">ASP.NET
Page &amp; WebControl Event Execution Order</a><br />
Part 3: <a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx">Getting
Started with ASP.NET DataBinding</a><br />
Part 4: Wiring Events to your Page
</p>
        </blockquote>
        <h3>Automatic Event Wireup
</h3>
        <p>
Page events can be automatically wired to their event handlers. That is, the code
to assign the event handler to your event can be made optional, with the system automatically
making the connection for the page, and thus, saving the developer a little bit of
work. Automatic event wiring is enabled through a boolean property called AutoEventWireup
on the ASP.NET Page Directive.
</p>
        <pre class="xml:nocontrols" name="code">&lt;%@ Page AutoEventWireup="true" %&gt;</pre>
        <p>
With this attribute set in the page directive, ASP.NET will automatically search for
and wire up any event handlers that match the specific naming convention of Page_<em>EventName</em>,
such as Page_Init or Page_Load. These methods can exist with or without the sender
and EventArgs method arguments, though do note they must both be absent or both be
present from the method signature; you cannot have one without the other.
</p>
        <pre class="csharp:nocontrols" name="code">protected void Page_Init()
{
  //Do some stuff
}

protected void Page_Load(object sender, EventArgs e)
{
  //Do some stuff
}</pre>
        <h3>Drawbacks to Event Auto-Wire
</h3>
        <p>
There is no fancy compiler magic to wire these event handlers to the event; under
AutoEventWireup, ASP.NET wires each event at run time when executing the page. This
means that for every event in the ASP.NET Page Life Cycle, the runtime engine reflects
through your code to search for an appropriate method for that event, just in case
it happens to exist, whether you create a handler method or not. To make matters worse,
it is reflecting for each event twice, once for a method with handler method arguments
(object sender, EventArgs e), and once for a method without the arguments.
</p>
        <p>
There are ten Life Cycle events on the Page that are eligible for auto-wire (counting
the Pre-and Post-Events such as PreInit and InitComplete) and four on the Control;
for a single page with five user controls, .NET Reflection is searching for up to
60 different methods. That is a lot of run-time overhead! Needless to say, AutoEventWireup
can have a significant negative impact on page performance.
</p>
        <h3>Manual Event Wireup
</h3>
        <p>
Page events can also be wired manually. This is similar to wiring up any other .NET
event. Using manual wire, you must explicitly wire the events you need to the appropriate
event handler. However, since you controls the wiring, this allows you to follow any
naming convention you wish and if needed, to wire multiple handlers to an event. Just
remember to turn AutoEventWireup off.
</p>
        <pre class="xml:nocontrols" name="code">&lt;%@ Page AutoEventWireup="false" %&gt;</pre>
        <p>
Page Events can be wired to their handler within your page's constructor.
</p>
        <pre class="csharp:nocontrols" name="code">public MyPageClass()
{
	Init += InitializePage;
	Load += LoadPage;
}

protected void InitializePage(object sender, EventArgs e)
{
  //Do some stuff
}

protected void LoadPage(object sender, EventArgs e)
{
  //Do some stuff
}</pre>
        <h3>Disadvantages to Manual Event Wireup
</h3>
        <p>
There is a little extra work involved to manually wire events. For the two events
from the auto-wire example, add a constructor and two lines of code. However, this
can be a bit of a daunting task if performed against an existing ASP.NET application
with dozens of pages and dozens more user controls. Even on new applications, it can
be easy to forget to wire the event, though it is easily identified in testing since
without the wireup, the handler does not execute, and in most cases, the page does
not function properly.
</p>
        <p>
Another disadvantage is that AutoEventWireup is enabled by default in C# (it is turned
off by default in Visual Basic). Even if you manually wire the events to handlers
in the page constructor, you must remember to disable wireup in the Page Directive.
Fail to do so will cause ASP.NET to wire up the events automatically in addition to
the manual hookup, and the event handler will be executed twice. So, again, don't
forget to turn off AutoEventWireup.
</p>
        <h3>Balancing Effort with Performance
</h3>
        <p>
Using AutoEventWireup essentially comes down to the value of developer effort versus
end-user performance. Auto-wire will save developers from one line of code per event,
but at the cost of system performance due to the reflection that occurs to have the
system wire the events rather than the developer. For a small utility web site or
a demo for a presentation, I can understand using auto-wire. However, being a performance
guy, I want to extract every ounce of horsepower I can out of my web applications,
so I will always opt for manual wiring; with it only costing me one line of code,
I feel that it is a good habit to have.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=245e0356-23d5-4212-99bf-583840b1e904" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</guid>
      <link>http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx</link>
      <pubDate>Tue, 26 Apr 2011 23:45:23 GMT</pubDate>
      <description>&lt;p&gt;
The first three parts of this series discuss the events that make up the ASP.NET Page
Life Cycle, the order that these events execute on the page and on the controls, and
that necessary evil called Data Binding, through which you add dynamic content to
your new ASP.NET page. Raising those events happens automatically, without any developer
oversight, but tapping in to those events—making them work for you—requires wiring
special methods called Event Handlers to those events. This wiring can happen automatically
by the system if you follow a few simple steps, or it can happen manually, with developers
connecting each appropriate event to its handler. Both sides have their advantages
as well as their drawbacks, but learning the nuances of event wiring can only strengthen
your ASP.NET repertoire.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;About the Series
&lt;/h3&gt;
&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;p&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx" target="_blank"&gt;Events
of the ASP.NET Page Life Cycle&lt;/a&gt; 
&lt;br /&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;ASP.NET
Page &amp;amp; WebControl Event Execution Order&lt;/a&gt; 
&lt;br /&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx"&gt;Getting
Started with ASP.NET DataBinding&lt;/a&gt; 
&lt;br /&gt;
Part 4: Wiring Events to your Page
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h3&gt;Automatic Event Wireup
&lt;/h3&gt;
&lt;p&gt;
Page events can be automatically wired to their event handlers. That is, the code
to assign the event handler to your event can be made optional, with the system automatically
making the connection for the page, and thus, saving the developer a little bit of
work. Automatic event wiring is enabled through a boolean property called AutoEventWireup
on the ASP.NET Page Directive.
&lt;/p&gt;
&lt;pre class="xml:nocontrols" name="code"&gt;&amp;lt;%@ Page AutoEventWireup=&amp;quot;true&amp;quot; %&amp;gt;&lt;/pre&gt;
&lt;p&gt;
With this attribute set in the page directive, ASP.NET will automatically search for
and wire up any event handlers that match the specific naming convention of Page_&lt;em&gt;EventName&lt;/em&gt;,
such as Page_Init or Page_Load. These methods can exist with or without the sender
and EventArgs method arguments, though do note they must both be absent or both be
present from the method signature; you cannot have one without the other.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;protected void Page_Init()
{
  //Do some stuff
}

protected void Page_Load(object sender, EventArgs e)
{
  //Do some stuff
}&lt;/pre&gt;
&lt;h3&gt;Drawbacks to Event Auto-Wire
&lt;/h3&gt;
&lt;p&gt;
There is no fancy compiler magic to wire these event handlers to the event; under
AutoEventWireup, ASP.NET wires each event at run time when executing the page. This
means that for every event in the ASP.NET Page Life Cycle, the runtime engine reflects
through your code to search for an appropriate method for that event, just in case
it happens to exist, whether you create a handler method or not. To make matters worse,
it is reflecting for each event twice, once for a method with handler method arguments
(object sender, EventArgs e), and once for a method without the arguments.
&lt;/p&gt;
&lt;p&gt;
There are ten Life Cycle events on the Page that are eligible for auto-wire (counting
the Pre-and Post-Events such as PreInit and InitComplete) and four on the Control;
for a single page with five user controls, .NET Reflection is searching for up to
60 different methods. That is a lot of run-time overhead! Needless to say, AutoEventWireup
can have a significant negative impact on page performance.
&lt;/p&gt;
&lt;h3&gt;Manual Event Wireup
&lt;/h3&gt;
&lt;p&gt;
Page events can also be wired manually. This is similar to wiring up any other .NET
event. Using manual wire, you must explicitly wire the events you need to the appropriate
event handler. However, since you controls the wiring, this allows you to follow any
naming convention you wish and if needed, to wire multiple handlers to an event. Just
remember to turn AutoEventWireup off.
&lt;/p&gt;
&lt;pre class="xml:nocontrols" name="code"&gt;&amp;lt;%@ Page AutoEventWireup=&amp;quot;false&amp;quot; %&amp;gt;&lt;/pre&gt;
&lt;p&gt;
Page Events can be wired to their handler within your page's constructor.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public MyPageClass()
{
	Init += InitializePage;
	Load += LoadPage;
}

protected void InitializePage(object sender, EventArgs e)
{
  //Do some stuff
}

protected void LoadPage(object sender, EventArgs e)
{
  //Do some stuff
}&lt;/pre&gt;
&lt;h3&gt;Disadvantages to Manual Event Wireup
&lt;/h3&gt;
&lt;p&gt;
There is a little extra work involved to manually wire events. For the two events
from the auto-wire example, add a constructor and two lines of code. However, this
can be a bit of a daunting task if performed against an existing ASP.NET application
with dozens of pages and dozens more user controls. Even on new applications, it can
be easy to forget to wire the event, though it is easily identified in testing since
without the wireup, the handler does not execute, and in most cases, the page does
not function properly.
&lt;/p&gt;
&lt;p&gt;
Another disadvantage is that AutoEventWireup is enabled by default in C# (it is turned
off by default in Visual Basic). Even if you manually wire the events to handlers
in the page constructor, you must remember to disable wireup in the Page Directive.
Fail to do so will cause ASP.NET to wire up the events automatically in addition to
the manual hookup, and the event handler will be executed twice. So, again, don't
forget to turn off AutoEventWireup.
&lt;/p&gt;
&lt;h3&gt;Balancing Effort with Performance
&lt;/h3&gt;
&lt;p&gt;
Using AutoEventWireup essentially comes down to the value of developer effort versus
end-user performance. Auto-wire will save developers from one line of code per event,
but at the cost of system performance due to the reflection that occurs to have the
system wire the events rather than the developer. For a small utility web site or
a demo for a presentation, I can understand using auto-wire. However, being a performance
guy, I want to extract every ounce of horsepower I can out of my web applications,
so I will always opt for manual wiring; with it only costing me one line of code,
I feel that it is a good habit to have.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=245e0356-23d5-4212-99bf-583840b1e904" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,245e0356-23d5-4212-99bf-583840b1e904.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=15ae3174-d0bd-4639-9d9c-10d2feeaab35</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=15ae3174-d0bd-4639-9d9c-10d2feeaab35</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
"There was a time when everything was moving towards the desktop. This Internet
thing was new and cool, but there was no way it would ever last. And no one knew how
to code for the web, at least not anything beyond animated lava lamps and cute "Under
Construction" images. So, to make coding for the web easier, they made ASP.NET
to be just like coding for a desktop, using the same patterns, the same event-based
model, and the same stateful approach. But the web isn't stateful, its only events
are GET and POST, and is nothing like a desktop, so we tortured ourselves for years
forcing a square peg through a round hole. The time has come for redemption, and its
name is ASP.NET MVC. Spend an hour discovering how coding for the web is supposed
to be--how it is today--and end your misery. Salvation awaits."
</p>
        <div style="text-align: center">
          <div style="margin: 0px auto; width: 425px" id="__ss_4091487">
            <object id="__sse4091487" width="425" height="355">
              <param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" />
              <param name="allowFullScreen" value="true" />
              <param name="allowScriptAccess" value="always" />
              <embed name="__sse4091487" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355">
              </embed>
            </object>
          </div>
          <a title="Slides for &quot;ASP.NET MVC 2: A (Microsoft) Web Coder's Salvation&quot; on SlideShare" href="http://www.slideshare.net/jayharris/aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487">Slides
on SlideShare</a> | <a title="Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2" href="http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx">Code
Walkthrough</a></div>
        <p>
At various user groups throughout Southeast Michigan and Northwest Ohio, I have been
presenting this topic since the last quarter of 2009. For those that are interested,
I have made the slide deck available on SlideShare. The code demo used during the
talk is available as a walkthrough via one of my installments of Learn to Code, where
you can create, step-by-step, the same ASP.NET MVC 2 application as was built during
the presentation.
</p>
        <p>
If you attend one of my presentations for this topic, I would appreciate your feedback.
If you are willing to tolerate a small registration process, SpeakerRate will allow
you to give feedback and anonymous ratings to the session you attended. If you are
interested in having me present this topic at your user group or conference, please
contact me.
</p>
        <h3>Past presentations on this topic: 
</h3>
        <p>
          <a title="Rate my ASP.NET MVC 2 Talk at AADND, May 2010" href="http://speakerrate.com/talks/3235-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
Ann Arbor .NET Developers, Ann Arbor, Michigan, May 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk from GLUGnet Lansing, March 2010" href="http://speakerrate.com/talks/2865-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
GLUGnet, Lansing, Michigan, March 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at A2&lt;DIV&gt;, February 2010" href="http://speakerrate.com/talks/2175-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
A2&lt;DIV&gt;, Ann Arbor, Michigan, February 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at NWNUG, February 2010" href="http://speakerrate.com/talks/2134-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
NWNUG, Toledo, Ohio, February 2010 
<br /><a title="Rate my ASP.NET MVC 2 Talk at GLUGnet Flint, February 2010" href="http://speakerrate.com/talks/3327-asp-net-mvc-2-a-microsoft-web-coder-s-salvation">Rate</a> –
GLUGnet, Flint, Michigan, February 2010
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:97df05d2-ed4c-49b3-af36-91ee1db68b2a" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/Speaking" rel="tag">Speaking</a>,<a href="http://technorati.com/tags/Presentation" rel="tag">Presentation</a>,<a href="http://technorati.com/tags/Slides" rel="tag">Slides</a>,<a href="http://technorati.com/tags/Demo" rel="tag">Demo</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=15ae3174-d0bd-4639-9d9c-10d2feeaab35" />
      </body>
      <title>Presenting ASP.NET MVC 2: A (Microsoft) Web Coder's Salvation</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</guid>
      <link>http://www.cptloadtest.com/2010/05/23/Presenting-ASPNET-MVC-2-A-Microsoft-Web-Coders-Salvation.aspx</link>
      <pubDate>Sun, 23 May 2010 22:40:39 GMT</pubDate>
      <description>&lt;p&gt;
&amp;quot;There was a time when everything was moving towards the desktop. This Internet
thing was new and cool, but there was no way it would ever last. And no one knew how
to code for the web, at least not anything beyond animated lava lamps and cute &amp;quot;Under
Construction&amp;quot; images. So, to make coding for the web easier, they made ASP.NET
to be just like coding for a desktop, using the same patterns, the same event-based
model, and the same stateful approach. But the web isn't stateful, its only events
are GET and POST, and is nothing like a desktop, so we tortured ourselves for years
forcing a square peg through a round hole. The time has come for redemption, and its
name is ASP.NET MVC. Spend an hour discovering how coding for the web is supposed
to be--how it is today--and end your misery. Salvation awaits.&amp;quot;
&lt;/p&gt;
&lt;div style="text-align: center"&gt;
&lt;div style="margin: 0px auto; width: 425px" id="__ss_4091487"&gt;
&lt;object id="__sse4091487" width="425" height="355"&gt;
&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowScriptAccess" value="always" /&gt;&lt;embed name="__sse4091487" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=asp-netmvc-awebcoderssalvationslideshare-100513220448-phpapp02&amp;amp;stripped_title=aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"&gt;&lt;/embed&gt;
&lt;/object&gt;
&lt;/div&gt;
&lt;a title="Slides for &amp;quot;ASP.NET MVC 2: A (Microsoft) Web Coder&amp;#39;s Salvation&amp;quot; on SlideShare" href="http://www.slideshare.net/jayharris/aspnet-mvc-2-a-microsoft-web-coders-salvation-4091487"&gt;Slides
on SlideShare&lt;/a&gt; | &lt;a title="Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2" href="http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx"&gt;Code
Walkthrough&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;
At various user groups throughout Southeast Michigan and Northwest Ohio, I have been
presenting this topic since the last quarter of 2009. For those that are interested,
I have made the slide deck available on SlideShare. The code demo used during the
talk is available as a walkthrough via one of my installments of Learn to Code, where
you can create, step-by-step, the same ASP.NET MVC 2 application as was built during
the presentation.
&lt;/p&gt;
&lt;p&gt;
If you attend one of my presentations for this topic, I would appreciate your feedback.
If you are willing to tolerate a small registration process, SpeakerRate will allow
you to give feedback and anonymous ratings to the session you attended. If you are
interested in having me present this topic at your user group or conference, please
contact me.
&lt;/p&gt;
&lt;h3&gt;Past presentations on this topic: 
&lt;/h3&gt;
&lt;p&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at AADND, May 2010" href="http://speakerrate.com/talks/3235-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
Ann Arbor .NET Developers, Ann Arbor, Michigan, May 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk from GLUGnet Lansing, March 2010" href="http://speakerrate.com/talks/2865-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
GLUGnet, Lansing, Michigan, March 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at A2&amp;lt;DIV&amp;gt;, February 2010" href="http://speakerrate.com/talks/2175-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
A2&amp;lt;DIV&amp;gt;, Ann Arbor, Michigan, February 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at NWNUG, February 2010" href="http://speakerrate.com/talks/2134-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
NWNUG, Toledo, Ohio, February 2010 
&lt;br /&gt;
&lt;a title="Rate my ASP.NET MVC 2 Talk at GLUGnet Flint, February 2010" href="http://speakerrate.com/talks/3327-asp-net-mvc-2-a-microsoft-web-coder-s-salvation"&gt;Rate&lt;/a&gt; –
GLUGnet, Flint, Michigan, February 2010
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:97df05d2-ed4c-49b3-af36-91ee1db68b2a" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Speaking" rel="tag"&gt;Speaking&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Presentation" rel="tag"&gt;Presentation&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Slides" rel="tag"&gt;Slides&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Demo" rel="tag"&gt;Demo&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=15ae3174-d0bd-4639-9d9c-10d2feeaab35" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,15ae3174-d0bd-4639-9d9c-10d2feeaab35.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>MVC</category>
      <category>Speaking</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=4faf4135-777f-41e2-9239-dce95df5e2a7</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=4faf4135-777f-41e2-9239-dce95df5e2a7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
</p>
        <blockquote>
          <p style="color: maroon">
            <em>The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.</em>
          </p>
        </blockquote>
        <p>
The following solution applies to the ASP.NET MVC 2 framework. If you are looking
for a solution in the first version of the ASP.NET MVC framework, try last November's
post on <a href="http://www.cptloadtest.com/2009/12/01/Validating-ASPNET-MVC-Route-Values-With-ActionFilterAttribute.aspx">Validating
ASP.NET MVC Route Values with ActionFilterAttribute</a>.
</p>
        <p>
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is an optional URL Parameter (UrlParameter.Optional),
as defined in the default routing tables, but the value can be parsed to other types
such as an integer for a default Details action. The problem stems from when invalid
values or missing values are passed to these integer-expecting actions; MVC handles
the inability to parse the value into an integer, but then throws an exception trying
to pass a null value to a Controller Action expecting a value type for a method argument.
</p>
        <p>
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
</p>
        <p>
Under ASP.NET MVC 2, the solution lies with a DefaultValueAttribute. Prior to executing
a Controller Action, the DefaultValueAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this allows me to keep that method argument as a value-type integer and avoid
Nullable&lt;int&gt;.HasValue.
</p>
        <p>
The attribute is a member of the System.ComponentModel namespace, and accepts any
one of many different base values as the default to assign to the route value should
the parsing fail. Unlike the ActionFilterAttribute used to solve this problem in the
first version of the ASP.NET MVC framework, there are no route redirects, which also
means we do not modify the browser URL. (Using the ActionFilterAttribute, the browser
location is redirected from ~/Widgets/Details/ or ~/Widgets/Details/Foo to ~/Widgets/Details/0,
but with the DefaultValueAttribute, no such redirect occurs.)
</p>
        <h3>Usage
</h3>
        <pre class="csharp:nocontrols" name="code">public class WidgetsController : Controller
{
  public ActionResult Details([DefaultValue(0) int id)
  {
    return View();
  }
}</pre>
        <p>
There is very little code to make this all happen. With one attribute added to the
method argument, MVC validates that my identity is an integer or otherwise provides
a replacement default value. I can depend on my user input without having to laden
my code with unnecessary value checks and without risk of unhandled exceptions. However,
the DefaultValueAttribute is just for providing a default value for your value-type
method arguments. Unlike the ActionFilterAttribute, the DefaultValueAttribute will
not perform any filtering, such as making sure a string argument begins with "Foo"
or that a decimal input only contains two decimal places; for this type of logic,
continue to use the ActionFilterAttribute. DefaultValueAttribute is a perfect fit
for eliminating Nullable&lt;int&gt; in Action arguments, making the code clean, simple,
and elegant. Eliminate the extra code, and let the framework do the work for you.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a0a44014-ffba-4b02-b952-0ae0181262d7" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/DefaultFilterAttribute" rel="tag">DefaultFilterAttribute</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4faf4135-777f-41e2-9239-dce95df5e2a7" />
      </body>
      <title>Validating ASP.NET MVC 2 Route Values with DefaultValueAttribute</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</guid>
      <link>http://www.cptloadtest.com/2010/04/05/Validating-ASPNET-MVC-2-Route-Values-With-DefaultValueAttribute.aspx</link>
      <pubDate>Mon, 05 Apr 2010 02:08:03 GMT</pubDate>
      <description>&lt;p&gt;
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p style="color: maroon"&gt;
&lt;em&gt;The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The following solution applies to the ASP.NET MVC 2 framework. If you are looking
for a solution in the first version of the ASP.NET MVC framework, try last November's
post on &lt;a href="http://www.cptloadtest.com/2009/12/01/Validating-ASPNET-MVC-Route-Values-With-ActionFilterAttribute.aspx"&gt;Validating
ASP.NET MVC Route Values with ActionFilterAttribute&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is an optional URL Parameter (UrlParameter.Optional),
as defined in the default routing tables, but the value can be parsed to other types
such as an integer for a default Details action. The problem stems from when invalid
values or missing values are passed to these integer-expecting actions; MVC handles
the inability to parse the value into an integer, but then throws an exception trying
to pass a null value to a Controller Action expecting a value type for a method argument.
&lt;/p&gt;
&lt;p&gt;
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
&lt;/p&gt;
&lt;p&gt;
Under ASP.NET MVC 2, the solution lies with a DefaultValueAttribute. Prior to executing
a Controller Action, the DefaultValueAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this allows me to keep that method argument as a value-type integer and avoid
Nullable&amp;lt;int&amp;gt;.HasValue.
&lt;/p&gt;
&lt;p&gt;
The attribute is a member of the System.ComponentModel namespace, and accepts any
one of many different base values as the default to assign to the route value should
the parsing fail. Unlike the ActionFilterAttribute used to solve this problem in the
first version of the ASP.NET MVC framework, there are no route redirects, which also
means we do not modify the browser URL. (Using the ActionFilterAttribute, the browser
location is redirected from ~/Widgets/Details/ or ~/Widgets/Details/Foo to ~/Widgets/Details/0,
but with the DefaultValueAttribute, no such redirect occurs.)
&lt;/p&gt;
&lt;h3&gt;Usage
&lt;/h3&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public class WidgetsController : Controller
{
  public ActionResult Details([DefaultValue(0) int id)
  {
    return View();
  }
}&lt;/pre&gt;
&lt;p&gt;
There is very little code to make this all happen. With one attribute added to the
method argument, MVC validates that my identity is an integer or otherwise provides
a replacement default value. I can depend on my user input without having to laden
my code with unnecessary value checks and without risk of unhandled exceptions. However,
the DefaultValueAttribute is just for providing a default value for your value-type
method arguments. Unlike the ActionFilterAttribute, the DefaultValueAttribute will
not perform any filtering, such as making sure a string argument begins with &amp;quot;Foo&amp;quot;
or that a decimal input only contains two decimal places; for this type of logic,
continue to use the ActionFilterAttribute. DefaultValueAttribute is a perfect fit
for eliminating Nullable&amp;lt;int&amp;gt; in Action arguments, making the code clean, simple,
and elegant. Eliminate the extra code, and let the framework do the work for you.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a0a44014-ffba-4b02-b952-0ae0181262d7" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DefaultFilterAttribute" rel="tag"&gt;DefaultFilterAttribute&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4faf4135-777f-41e2-9239-dce95df5e2a7" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,4faf4135-777f-41e2-9239-dce95df5e2a7.aspx</comments>
      <category>ASP.Net</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=6682b44d-7660-4b5e-8746-bdcfdae81f84</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6682b44d-7660-4b5e-8746-bdcfdae81f84</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The previous installments of this series cover the core events of the ASP.NET Page
class, of the ASP.NET Controls, and how the page and controls work together to co-exist
in the same sandbox. We've gotten to the point where we know how these controls will
interact with the page so that we can make our page more than just a sea of crazy
peach gradient backgrounds, but that still isn't enough. Static content is so 1999,
and that party broke up long ago. The next step in a quality, modern web site is creating
dynamic content, and rendering it to the page. To do this, we need Data Binding.
</p>
        <blockquote>
          <h3>About the Series
</h3>
          <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
          <p>
Part 1: <a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Events
of the ASP.NET Page Life Cycle</a><br />
Part 2: <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">ASP.NET
Page &amp; WebControl Event Execution Order</a><br />
Part 3: Getting Started with ASP.NET Data Binding 
<br />
Part 4: <a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">Wiring
Events to your Page</a></p>
        </blockquote>
        <h3>Getting Started with ASP.NET Data Binding
</h3>
        <p>
When we load up data from the database, we could use a for-each loop to iterate through
the data, manually create every new control, such as a table row, and manually write
our data as the content for each new control, but that would be a lot of useless,
repetitive, and senseless <a title="Boilerplate Code (Wikipedia)" href="http://en.wikipedia.org/wiki/Boilerplate_(text)#Boilerplate_code">boilerplate
code</a>. Instead, we should use functionality that is built in to the ASP.NET framework,
and allow the framework to do a lot of the work for us. This process of using the
framework for binding dynamic data to the page, or to controls within the page, is
called Data Binding.
</p>
        <p>
As we've discussed before, there are several events that handle the processing and
rendering of a page. Similarly, there are several events that handle the process of
binding data to a page (and its child controls). Each of these events handle a very
specific sub-set of the process. And as before, knowing the execution order of these
events will make you more proficient at ASP.NET development.
</p>
        <p>
For our examples, we will have a page for displaying Company data. The page will host
a TextBox control containing the name of the company, and a repeater for departments
within the company. Each repeater item will display the name of the department and
a DropDownList containing the names of employees within the department.
</p>
        <pre class="csharp:nocontrols" name="code">public partial class CompanyInformation : Page
{
  private Repeater _deparmentsRepeater;
  private TextBox _companyNameTextbox;

  public _Default()
  {
    Init += Page_Init;
    Load += Page_Load;
  }

  void Page_Init(object sender, EventArgs e)
  {
    var container = new Panel();
    _companyNameTextbox = new TextBox();
    _companyNameTextbox.ID = "companyNameTextbox";
    _companyNameTextbox.DataBinding += TBox_DataBinding;
    container.Controls.Add(_companyNameTextbox);
    myForm.Controls.Add(container);

    _deparmentsRepeater = new Repeater();
    _deparmentsRepeater.ID = "departmentsRepeater";
    _deparmentsRepeater.ItemCreated += Rpt_ItemCreated;
    _deparmentsRepeater.ItemDataBound += Rpt_ItemDataBound;
    myForm.Controls.Add(_deparmentsRepeater);
  }
}</pre>
        <h3>ASP.NET Data Binding Events
</h3>
        <h4>Page.DataBind() / Control.DataBind()
</h4>
        <p>
The first "Event" of ASP.NET's Data Binding Life Cycle is not an event at
all; it is a method that gets everything started. The DataBind method initiates the
process of binding all controls on the page, attaching your dynamic loaded classes
or lists to the areas of your page where the data will be displayed. DataBind can
be executed at the Page level, or on any bindable Control. Whenever it is executed,
DataBind also cascades through all child controls of the execution point. If you have
an ASP.NET Panel Control that contains several TextBox controls, executing DataBind
on the panel will run the method on the Panel first, then all of the child text boxes.
If you execute on the Page, it will fire first on the page, then on the panel, then
on all of the text boxes. The cascading rules are the same as the event execution
order discussed in <a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx" target="_blank">Part
2</a> of this series.
</p>
        <p>
Traditionally, DataBind is executed from within the Page Load event, though it can
be executed from other places instead, as long as you keep the rest of the page life
cycle in mind. For example, do not run DataBind on your page in PreInit; none of your
controls have been instantiated, yet. Also, as my personal preference, I put all of
my data retrieval logic in an override of the DataBind method, keeping all of my binding
logic in a centralized location, though this can also occur elsewhere providing that
it is prior to the execution of the base Page.DataBind() or Control.DataBind() method.
</p>
        <pre class="csharp:nocontrols" name="code">void Page_Load(object sender, EventArgs e)
{
  DataBind();
}
public override void DataBind()
{
  Trace.Write("Beginning to bind all controls");
  _myRepeater.DataSource = MyCompany.Departments();
  base.DataBind();
}</pre>
        <p>
Here, the Page Load event kicks off our override of the DataBind method. This override
retrieves a list of Departments for the company, and assigns the required properties
from the company class instance to controls as needed, which includes all controls
with a DataSource property. DataSource is usually a collection of objects to be used
for binding collections to list-based controls from DropDownLists to Repeaters, though
some other controls may have different needs and uses for DataSource. Above, we assign
the Departments list to the repeater's DataSource property, so that later on in the
binding process, the repeater can pass appropriate Department information on to each
of its RepeaterItems and child controls. With the DataSource defined, the base DataBind
method is executed to initiate binding to all controls on the page.
</p>
        <h4>DataBinding Event
</h4>
        <p>
DataBinding is the first actual event that fires in the DataBinding series, and it
will fire for every bindable control and child control from the point where the DataBind
method was executed, top-down. When the event executes, binding has not yet been executed,
but the data to be bound is available for manipulation. As such, binding has also
not yet executed on any of the child controls of the event target. With the exception
of list-based container controls like a DataGrid, GridView, or Repeater, during the
DataBinding event would be where any dynamic child controls are created and their
DataSources assigned.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _companyNameTextbox.DataBinding += TBox_DataBinding;
void TBox_DataBinding(object sender, EventArgs e)
{
  ((TextBox) sender).Text = MyCompany.Name;
}</pre>
        <p>
Above, we bind our TextBox control to the name of our company. TextBox does not have
a DataSource property, so the company name is manually assigned directly to the TextBox's
Text property.
</p>
        <h4>RowCreated / ItemCreated Event
</h4>
        <p>
Note: RowCreated is used for GridView Control, only. ItemCreated is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
<br />
The RowCreated and ItemCreated events are very similar in nature to the DataBinding
event, and are used for list-based container controls like a DataGrid, GridView, or
Repeater. This event will execute once for each item in the DataSource collection,
and correspond to one Row or Item in the control. On execution, the individual Row
or Item in your control has been created, any child controls specified in your markup
have been initialized and loaded, and the individual data item is available for manipulation.
However, Data Binding has not yet been executed on the row or any of its child controls.
This event cannot be dependent upon any control data since the binding has yet to
occur. An example scenario that would use this event would be if your data item contained
another list, such as our Department class instance that contains a list of Employees,
and the Employees will display in a dropdown. During Created, the dropdown's DataSource
can be assigned to the list contained within the Employees property. After the Created
event finished for the Row or Item, ASP.NET will automatically execute the DataBind
method on the Employees dropdown, binding the list data to populate the control. This
automatic execution of DataBind applies to all child controls of the Row or Item,
including controls defined in the Code-In-Front markup or those that were manually
created in the code-behind.
</p>
        <p>
Be aware that any code in the Created events cannot be dependent upon Control data.
Even if you specify the DataSource property of a child dropdown, the DropDownList.Items
list property is still empty. Executing code dependant upon control data, such as
setting the selected item, will fail.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _myRepeater.ItemCreated += Rpt_ItemCreated;
void Rpt_ItemCreated(object sender, RepeaterItemEventArgs e)
{
  //Preparing another Repeater Item for binding
  var dataItem = (Department) e.Item.DataItem;

  var departmentLabel = new Label();
  departmentLabel.Text = dataItem.Name;
  e.Item.Controls.Add(departmentLabel);

  var dropdownOfEmployees = new DropDownList();
  dropdownOfEmployees.DataSource = dataItem.Employees;
  dropdownOfEmployees.DataTextField = "Name";
  e.Item.Controls.Add(dropdownOfEmployees);

  e.Item.Controls.Add(new LiteralControl("<br />
")); }</pre>
        <p>
The ItemCreated event creates a label for the name of the department, assigning the
name, and a DropDownList of Employees, assigning only the list's DataSource. ItemCreated
is repeated for every item in the repeater's DataSource collection, or in other words,
every department associated with our company. Because DataBinding has not yet executed
for any child control, none of the Employee ListItems exist in our DropDownList, but
because it has not yet executed, we also do not need to manually create the ListItems
or manually re-execute DropDownList.DataBind().
</p>
        <h4>RowDataBound / ItemDataBound
</h4>
        <p>
Note: RowDataBound is used for GridView Control, only. ItemDataBound is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
<br />
The final event in the Data Binding Life Cycle is the Bound events, RowDataBound and
ItemDataBound. This event fires when the data binding process has completed for all
child controls within the Row or Item. All controls within the row have their data,
and code that is dependant upon control data, such as setting a selected item, can
now be executed. It is strongly advised that this event contains no modifications
to the item data that would require a DataBind method to be executed; this sort of
data manipulation should occur instead in the Created events, prior to Data Binding
the child control tree. If DataBind were to be executed again on this control, the
Data Binding process for this control and all children is restarted and re-executed,
causing controls to be Data Bound two or more times and potentially leading to an
unknown state and adverse outcomes in your application.
</p>
        <p>
The DataBound events indicate that binding has been completed for this item and all
child controls; for us, this means our DropDownList of employees has been fully bound,
and that a ListItem exists for each Employee. We can use this post-binding event to
perform any data-dependent logic. In this case, we now set the SelectedIndex of our
DropDownList; the method would fail if it was executed during ItemCreated. As with
ItemCreated, this event will execute for each item in our repeater's DataSource collection.
</p>
        <pre class="csharp:nocontrols" name="code">// Use with: _myRepeater.ItemDataBound += Rpt_ItemDataBound;
void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  var dropdownOfEmployees = (DropDownList) e.Item.Controls[1];
  dropdownOfEmployees.SelectedIndex = 2;
}</pre>
        <h3>Proper Data Binding
</h3>
        <p>
This is the essence of Data Binding within ASP.NET Web Forms: one method and three
events. When done properly, the DataBind method should only be called once on a control
tree. Execute DataBind directly on the page, or execute it once on individual controls
such as each of your top-level Repeaters, but whatever path you follow, make sure
that the method is not executed twice on any single control. Use the binding events
to make this happen. Failure to do so can cause unfortunate side effects and extensive
pain.
</p>
        <ul>
          <li>
DataSources should be assigned prior to execution of a control's DataBind method. 
</li>
          <li>
Controls without a DataSource property can be manually bound using the controls DataBinding
event, after executing DataBind. 
</li>
          <li>
For collection-based container controls, define child controls and assign their DataSource
using the RowCreated/ItemCreated events. 
</li>
          <li>
For collection-based container controls, perform data-dependent logic on child controls
in the RowDataBound/ItemDataBound event. 
</li>
        </ul>
        <p>
The ASP.NET Page Life Cycle may seem like a monotonous mess, but it is a useful tool
if you understand the events, their order, and their relationship to each other. If
you missed them, go back and review <a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Part
1</a> for the core Page Life Cycle events and to <a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">Part
2</a> for the Event Execution Order between a page and its controls. Once you have
these down, there still are some oddities to look out for (and some tricks to help
you out). <a title="Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]" href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">As
the series continues</a>, we discuss some of these tips, tricks, and traps to help
you unleash the full power of your ASP.NET applications.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:233f1d3b-6345-4d60-a6cb-b67b55265237" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a>,<a href="http://technorati.com/tags/Data+Binding" rel="tag">Data Binding</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6682b44d-7660-4b5e-8746-bdcfdae81f84" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 3 [Data Binding]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</guid>
      <link>http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx</link>
      <pubDate>Wed, 24 Mar 2010 00:32:53 GMT</pubDate>
      <description>&lt;p&gt;
The previous installments of this series cover the core events of the ASP.NET Page
class, of the ASP.NET Controls, and how the page and controls work together to co-exist
in the same sandbox. We've gotten to the point where we know how these controls will
interact with the page so that we can make our page more than just a sea of crazy
peach gradient backgrounds, but that still isn't enough. Static content is so 1999,
and that party broke up long ago. The next step in a quality, modern web site is creating
dynamic content, and rendering it to the page. To do this, we need Data Binding.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;About the Series
&lt;/h3&gt;
&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;p&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Events
of the ASP.NET Page Life Cycle&lt;/a&gt; 
&lt;br /&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;ASP.NET
Page &amp;amp; WebControl Event Execution Order&lt;/a&gt; 
&lt;br /&gt;
Part 3: Getting Started with ASP.NET Data Binding 
&lt;br /&gt;
Part 4: &lt;a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;Wiring
Events to your Page&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h3&gt;Getting Started with ASP.NET Data Binding
&lt;/h3&gt;
&lt;p&gt;
When we load up data from the database, we could use a for-each loop to iterate through
the data, manually create every new control, such as a table row, and manually write
our data as the content for each new control, but that would be a lot of useless,
repetitive, and senseless &lt;a title="Boilerplate Code (Wikipedia)" href="http://en.wikipedia.org/wiki/Boilerplate_(text)#Boilerplate_code"&gt;boilerplate
code&lt;/a&gt;. Instead, we should use functionality that is built in to the ASP.NET framework,
and allow the framework to do a lot of the work for us. This process of using the
framework for binding dynamic data to the page, or to controls within the page, is
called Data Binding.
&lt;/p&gt;
&lt;p&gt;
As we've discussed before, there are several events that handle the processing and
rendering of a page. Similarly, there are several events that handle the process of
binding data to a page (and its child controls). Each of these events handle a very
specific sub-set of the process. And as before, knowing the execution order of these
events will make you more proficient at ASP.NET development.
&lt;/p&gt;
&lt;p&gt;
For our examples, we will have a page for displaying Company data. The page will host
a TextBox control containing the name of the company, and a repeater for departments
within the company. Each repeater item will display the name of the department and
a DropDownList containing the names of employees within the department.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public partial class CompanyInformation : Page
{
  private Repeater _deparmentsRepeater;
  private TextBox _companyNameTextbox;

  public _Default()
  {
    Init += Page_Init;
    Load += Page_Load;
  }

  void Page_Init(object sender, EventArgs e)
  {
    var container = new Panel();
    _companyNameTextbox = new TextBox();
    _companyNameTextbox.ID = &amp;quot;companyNameTextbox&amp;quot;;
    _companyNameTextbox.DataBinding += TBox_DataBinding;
    container.Controls.Add(_companyNameTextbox);
    myForm.Controls.Add(container);

    _deparmentsRepeater = new Repeater();
    _deparmentsRepeater.ID = &amp;quot;departmentsRepeater&amp;quot;;
    _deparmentsRepeater.ItemCreated += Rpt_ItemCreated;
    _deparmentsRepeater.ItemDataBound += Rpt_ItemDataBound;
    myForm.Controls.Add(_deparmentsRepeater);
  }
}&lt;/pre&gt;
&lt;h3&gt;ASP.NET Data Binding Events
&lt;/h3&gt;
&lt;h4&gt;Page.DataBind() / Control.DataBind()
&lt;/h4&gt;
&lt;p&gt;
The first &amp;quot;Event&amp;quot; of ASP.NET's Data Binding Life Cycle is not an event at
all; it is a method that gets everything started. The DataBind method initiates the
process of binding all controls on the page, attaching your dynamic loaded classes
or lists to the areas of your page where the data will be displayed. DataBind can
be executed at the Page level, or on any bindable Control. Whenever it is executed,
DataBind also cascades through all child controls of the execution point. If you have
an ASP.NET Panel Control that contains several TextBox controls, executing DataBind
on the panel will run the method on the Panel first, then all of the child text boxes.
If you execute on the Page, it will fire first on the page, then on the panel, then
on all of the text boxes. The cascading rules are the same as the event execution
order discussed in &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx" target="_blank"&gt;Part
2&lt;/a&gt; of this series.
&lt;/p&gt;
&lt;p&gt;
Traditionally, DataBind is executed from within the Page Load event, though it can
be executed from other places instead, as long as you keep the rest of the page life
cycle in mind. For example, do not run DataBind on your page in PreInit; none of your
controls have been instantiated, yet. Also, as my personal preference, I put all of
my data retrieval logic in an override of the DataBind method, keeping all of my binding
logic in a centralized location, though this can also occur elsewhere providing that
it is prior to the execution of the base Page.DataBind() or Control.DataBind() method.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;void Page_Load(object sender, EventArgs e)
{
  DataBind();
}
public override void DataBind()
{
  Trace.Write(&amp;quot;Beginning to bind all controls&amp;quot;);
  _myRepeater.DataSource = MyCompany.Departments();
  base.DataBind();
}&lt;/pre&gt;
&lt;p&gt;
Here, the Page Load event kicks off our override of the DataBind method. This override
retrieves a list of Departments for the company, and assigns the required properties
from the company class instance to controls as needed, which includes all controls
with a DataSource property. DataSource is usually a collection of objects to be used
for binding collections to list-based controls from DropDownLists to Repeaters, though
some other controls may have different needs and uses for DataSource. Above, we assign
the Departments list to the repeater's DataSource property, so that later on in the
binding process, the repeater can pass appropriate Department information on to each
of its RepeaterItems and child controls. With the DataSource defined, the base DataBind
method is executed to initiate binding to all controls on the page.
&lt;/p&gt;
&lt;h4&gt;DataBinding Event
&lt;/h4&gt;
&lt;p&gt;
DataBinding is the first actual event that fires in the DataBinding series, and it
will fire for every bindable control and child control from the point where the DataBind
method was executed, top-down. When the event executes, binding has not yet been executed,
but the data to be bound is available for manipulation. As such, binding has also
not yet executed on any of the child controls of the event target. With the exception
of list-based container controls like a DataGrid, GridView, or Repeater, during the
DataBinding event would be where any dynamic child controls are created and their
DataSources assigned.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _companyNameTextbox.DataBinding += TBox_DataBinding;
void TBox_DataBinding(object sender, EventArgs e)
{
  ((TextBox) sender).Text = MyCompany.Name;
}&lt;/pre&gt;
&lt;p&gt;
Above, we bind our TextBox control to the name of our company. TextBox does not have
a DataSource property, so the company name is manually assigned directly to the TextBox's
Text property.
&lt;/p&gt;
&lt;h4&gt;RowCreated / ItemCreated Event
&lt;/h4&gt;
&lt;p&gt;
Note: RowCreated is used for GridView Control, only. ItemCreated is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
&lt;br /&gt;
The RowCreated and ItemCreated events are very similar in nature to the DataBinding
event, and are used for list-based container controls like a DataGrid, GridView, or
Repeater. This event will execute once for each item in the DataSource collection,
and correspond to one Row or Item in the control. On execution, the individual Row
or Item in your control has been created, any child controls specified in your markup
have been initialized and loaded, and the individual data item is available for manipulation.
However, Data Binding has not yet been executed on the row or any of its child controls.
This event cannot be dependent upon any control data since the binding has yet to
occur. An example scenario that would use this event would be if your data item contained
another list, such as our Department class instance that contains a list of Employees,
and the Employees will display in a dropdown. During Created, the dropdown's DataSource
can be assigned to the list contained within the Employees property. After the Created
event finished for the Row or Item, ASP.NET will automatically execute the DataBind
method on the Employees dropdown, binding the list data to populate the control. This
automatic execution of DataBind applies to all child controls of the Row or Item,
including controls defined in the Code-In-Front markup or those that were manually
created in the code-behind.
&lt;/p&gt;
&lt;p&gt;
Be aware that any code in the Created events cannot be dependent upon Control data.
Even if you specify the DataSource property of a child dropdown, the DropDownList.Items
list property is still empty. Executing code dependant upon control data, such as
setting the selected item, will fail.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _myRepeater.ItemCreated += Rpt_ItemCreated;
void Rpt_ItemCreated(object sender, RepeaterItemEventArgs e)
{
  //Preparing another Repeater Item for binding
  var dataItem = (Department) e.Item.DataItem;

  var departmentLabel = new Label();
  departmentLabel.Text = dataItem.Name;
  e.Item.Controls.Add(departmentLabel);

  var dropdownOfEmployees = new DropDownList();
  dropdownOfEmployees.DataSource = dataItem.Employees;
  dropdownOfEmployees.DataTextField = &amp;quot;Name&amp;quot;;
  e.Item.Controls.Add(dropdownOfEmployees);

  e.Item.Controls.Add(new LiteralControl(&amp;quot;&lt;br /&gt;
&amp;quot;)); }&lt;/pre&gt;
&lt;p&gt;
The ItemCreated event creates a label for the name of the department, assigning the
name, and a DropDownList of Employees, assigning only the list's DataSource. ItemCreated
is repeated for every item in the repeater's DataSource collection, or in other words,
every department associated with our company. Because DataBinding has not yet executed
for any child control, none of the Employee ListItems exist in our DropDownList, but
because it has not yet executed, we also do not need to manually create the ListItems
or manually re-execute DropDownList.DataBind().
&lt;/p&gt;
&lt;h4&gt;RowDataBound / ItemDataBound
&lt;/h4&gt;
&lt;p&gt;
Note: RowDataBound is used for GridView Control, only. ItemDataBound is used for DataGrid,
ListView, Repeater, and all other list-based container controls. 
&lt;br /&gt;
The final event in the Data Binding Life Cycle is the Bound events, RowDataBound and
ItemDataBound. This event fires when the data binding process has completed for all
child controls within the Row or Item. All controls within the row have their data,
and code that is dependant upon control data, such as setting a selected item, can
now be executed. It is strongly advised that this event contains no modifications
to the item data that would require a DataBind method to be executed; this sort of
data manipulation should occur instead in the Created events, prior to Data Binding
the child control tree. If DataBind were to be executed again on this control, the
Data Binding process for this control and all children is restarted and re-executed,
causing controls to be Data Bound two or more times and potentially leading to an
unknown state and adverse outcomes in your application.
&lt;/p&gt;
&lt;p&gt;
The DataBound events indicate that binding has been completed for this item and all
child controls; for us, this means our DropDownList of employees has been fully bound,
and that a ListItem exists for each Employee. We can use this post-binding event to
perform any data-dependent logic. In this case, we now set the SelectedIndex of our
DropDownList; the method would fail if it was executed during ItemCreated. As with
ItemCreated, this event will execute for each item in our repeater's DataSource collection.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;// Use with: _myRepeater.ItemDataBound += Rpt_ItemDataBound;
void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  var dropdownOfEmployees = (DropDownList) e.Item.Controls[1];
  dropdownOfEmployees.SelectedIndex = 2;
}&lt;/pre&gt;
&lt;h3&gt;Proper Data Binding
&lt;/h3&gt;
&lt;p&gt;
This is the essence of Data Binding within ASP.NET Web Forms: one method and three
events. When done properly, the DataBind method should only be called once on a control
tree. Execute DataBind directly on the page, or execute it once on individual controls
such as each of your top-level Repeaters, but whatever path you follow, make sure
that the method is not executed twice on any single control. Use the binding events
to make this happen. Failure to do so can cause unfortunate side effects and extensive
pain.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
DataSources should be assigned prior to execution of a control's DataBind method. 
&lt;/li&gt;
&lt;li&gt;
Controls without a DataSource property can be manually bound using the controls DataBinding
event, after executing DataBind. 
&lt;/li&gt;
&lt;li&gt;
For collection-based container controls, define child controls and assign their DataSource
using the RowCreated/ItemCreated events. 
&lt;/li&gt;
&lt;li&gt;
For collection-based container controls, perform data-dependent logic on child controls
in the RowDataBound/ItemDataBound event. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The ASP.NET Page Life Cycle may seem like a monotonous mess, but it is a useful tool
if you understand the events, their order, and their relationship to each other. If
you missed them, go back and review &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Part
1&lt;/a&gt; for the core Page Life Cycle events and to &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]" href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;Part
2&lt;/a&gt; for the Event Execution Order between a page and its controls. Once you have
these down, there still are some oddities to look out for (and some tricks to help
you out). &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 4 [Event Wireup]" href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;As
the series continues&lt;/a&gt;, we discuss some of these tips, tricks, and traps to help
you unleash the full power of your ASP.NET applications.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:233f1d3b-6345-4d60-a6cb-b67b55265237" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Data+Binding" rel="tag"&gt;Data Binding&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6682b44d-7660-4b5e-8746-bdcfdae81f84" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,6682b44d-7660-4b5e-8746-bdcfdae81f84.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=1d5d9618-a51a-471f-a5ec-7381c916f11d</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1d5d9618-a51a-471f-a5ec-7381c916f11d</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On Tuesday, February 9th, I was scheduled lead a jam session for Come Jam With Us,
the software developer study group in Ann Arbor. The session was to be on ASP.NET
MVC 2, aimed to give attendees enough of an introduction to the product to empower
developers to be able to start coding their own ASP.NET MVC 2 projects. Unfortunately,
Mother Nature did not cooperate that day, half of the state of Michigan seemingly
shut down under 8" of snow, and the session was cancelled and rescheduled for
February 23rd. The goal of these Learn to Code exercises is to give you an introduction
to building applications with ASP.NET MVC 2. In the near future, I also hope to provide
a screen cast of these same exercises.
</p>
        <h3>About this Exercise
</h3>
        <p>
This coding exercise is designed to give you an introduction to ASP.NET MVC 2. In
this exercise, developers will create their first database-driven ASP.NET MVC 2 application
within Visual Studio, primarily using code generation built in to Visual Studio. Developers
performing this exercise should be familiar with ASP.NET development and Visual Studio,
but no previous experience with ASP.NET MVC is required.
</p>
        <h3>Prerequisites
</h3>
        <p>
You will need few things for ASP.NET MVC 2 application development and to complete
these exercises. Please complete the following prerequisites prior to moving on. The
session is designed to be completed in about an hour, but prerequisite setup is not
included in that time.
</p>
        <ul>
          <li>
Install <a href="http://www.microsoft.com/visualstudio/">Microsoft Visual Studio 2008
SP1</a> or <a href="http://www.microsoft.com/express/Web/">Visual Web Developer 2008
Express</a> with SP1 
</li>
          <li>
Install <a href="http://www.microsoft.com/sqlserver/2008/">Microsoft SQL Server</a> or <a href="http://www.microsoft.com/express/Database/">SQL
Server Express</a></li>
          <li>
Download and install the latest <a title="ASP.NET MVC 2 RC 2 on Microsoft" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;displaylang=en">ASP.NET
MVC 2 RC 2 bits from Microsoft</a></li>
        </ul>
        <h3>Exercise 0: Getting Started
</h3>
        <h4>Creating a Project
</h4>
        <p>
Before any coding can occur, the first thing that we have to do is create a new ASP.NET
MVC 2 project from within Visual Studio.
</p>
        <ol>
          <li>
In Visual Studio, create a new "ASP.NET MVC 2 Web Application" named MvcJamSession.
You can create your project in either Visual Basic or Visual C#, though all of the
examples in this post will be in C#. 
</li>
          <li>
After selecting the project type and solution/project name, you will be prompted for
creating a unit test project. Select "Yes." Though we will not be using
these tests in this exercise, we will be in future installments. 
</li>
          <li>
Be sure that your MvcJamSession.Test project includes a project reference back to
your MvcJamSession project. 
</li>
          <li>
Compile and run. Your browser should display a blue web site showing "Welcome
to ASP.NET MVC!" Congratulations. You now have your first ASP.NET MVC application. 
</li>
        </ol>
        <h4>Convention-Based Development
</h4>
        <p>
          <a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession.jpg">
            <img style="border-right-width: 0px; margin: 0px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Project Folder Structure of MVC Jam Session Project" border="0" alt="Project Folder Structure of MVC Jam Session Project" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession_thumb.jpg" width="200" height="382" />
          </a> Development
within ASP.NET MVC is based on convention over configuration. Certain naming conventions
are built in the system to eliminate the amount of boiler-plate code that you need
to recreate. That is not to say that you <em>must</em> follow these naming conventions—you
may use whatever convention you like—but by straying from the standardized conventions,
you will be creating a lot of extra work for yourself. With our new ASP.NET MVC 2
Project, a few of these conventions are immediately visible.
</p>
        <ul>
          <li>
            <em>Controllers</em> folder. This is where all Controller classes must go. Individual
classes must be named <em>&lt;Name&gt;Controller</em>. The default project includes <em>HomeController</em>,
which governs the Home and About actions, and <em>AccountController</em>, which governs
the log in, log out, change password, and registration actions. If we were to make
an application that manages Widgets, we would likely have a class named <em>WidgetController</em>. 
</li>
          <li>
            <em>Views</em> folder. This is where all of the Views must go. By default, views are
paired one-to-one with controller actions, such as one view for new user registration
and another view for changing your password. Views are also separated into folders
matching the associated controller name—<em>/Views/&lt;ControllerName&gt;/&lt;ActionName&gt;</em>.
Thus, <em>HomeController</em>'s About action is associated with the <em>/Views/Home/About</em> view. 
<br />
The Views folder also contains a <em>Shared</em> folder. This folder is where any
common views, such as a Master Page, would reside. The <em>Shared</em> folder is also
where the ViewEngine cascades to when it can't find an appropriate view in the /<em>Views/&lt;ControllerName&gt;</em> folder;
if <em>/Views/Home/About</em> didn't exist, the ViewEngine would look for <em>/Views/Shared/About</em>.
This can come in handy for common pages shared by all controllers, such as an error
page. 
</li>
        </ul>
        <h3>Session Exercise 1: Building an Application
</h3>
        <p>
Using the project we just created, we're going to create an application that manage
a list of employees, including their name, job title, date of hire, and date of termination.
Though the default project is a great help on some applications, it can get in the
way on others; we're not going to need any account services in our application, so
we need to first trim the project down a little.
</p>
        <ol>
          <li>
Delete the entire <em>Account</em> folder under /Views/. 
</li>
          <li>
Delete <em>AccountController.cs</em> from the Controllers folder. 
</li>
          <li>
Delete shared partial view <em>/Views/Shared/LogOnUserControl.ascx</em>. 
</li>
          <li>
Delete reference to this partial view by removing the "LoginDisplay" DIV
from<em> /Views/Shared/Site.Master</em>, lines 18-20. 
</li>
          <li>
Removing <em>LoginDisplay</em> will cause a slight layout problem from the CSS. To
fix it, modify the <em>#menucontainer</em> definition in <em>/Content/Site.css</em> on
line 263. 
<br /><pre class="css:nocontrols" name="code">#menucontainer
{
    padding-top:40px;
}</pre></li>
          <li>
Save all, compile, and Run. The site should be functioning normally, but without the
Log In link in the top right of the home page. 
</li>
        </ol>
        <h4>Creating a Database
</h4>
        <p>
Like any dynamic web site, we need a storage mechanism. Create a database in SQL Server
or SQL Server Express with an Employee table containing columns for name, job title,
hired date, and termination date, as well as an identity column for finding records.<a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_1.jpg"><img style="border-right-width: 0px; margin: 5px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MvcJamSession-EmployeeTable" border="0" alt="MvcJamSession-EmployeeTable" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_thumb_1.jpg" width="250" height="283" /></a></p>
        <ul>
          <li>
For those with SQL Server Express, create a new database through right-clicking the
App_Data folder, and adding a new item. The new item should be a SQL Server Database. 
</li>
          <li>
For those with SQL Server, create a new database through SQL Server Management Studio
(SSMS). 
</li>
        </ul>
        <ol>
          <li>
Create a new table called <em>Employee</em>. 
</li>
          <li>
Create the following columns within the new table: 
<ul><li>
Id (primary key, identity, int, not null) 
</li><li>
Name (varchar(50), not null) 
</li><li>
JobTitle (varchar(50), not null) 
</li><li>
HiredOn (date, not null) 
</li><li>
TerminatedOn (date, null) 
</li></ul></li>
          <li>
Add a record or two to the table for good measure. 
</li>
        </ol>
        <h4>Creating a Model
</h4>
        <p>
The next thing we need to create is our Model. Not only is it the code representation
for our business entity (An Employee class contains Name, JobTitle, HiredOn, and TerminatedOn
properties), it is also responsible for <em>how</em> to get, save, or delete data
from the database. We will be using Microsoft Entity Framework through Visual Studio
to generate our model for us.
</p>
        <ol>
          <li>
To create our new Model, right-click the <em>Model</em> folder and select Add New
Item. The new item should be an ADO.NET Entity Data Model, found within the Data category
of the Add New Item dialog. Name it MvcJamSessionEntities. 
</li>
          <li>
Generate the Model from a database, connecting to your MVC Jam Session database created
in the previous section. On the step where you select your database connection, be
sure to allow Visual Studio to add the connection string to your web.config by checking
the appropriate checkbox. 
</li>
          <li>
The <em>Employee</em> table is the only Data Object that needs to be generated. 
</li>
          <li>
When the dialog completes, your Entity Data Model (a .edmx file) should be displayed
in design view. 
</li>
          <li>
Save all and compile. 
</li>
        </ol>
        <h4>Creating a Controller
</h4>
        <p>
Now that our model is in place, we need to create a controller. The controller is
responsible for managing all interaction between the end-user and the application,
including identifying what data to get, save, or delete. (Remember, though the Controller
is responsible for <em>what</em>, the Model is responsible for <em>how</em>.)
</p>
        <ol>
          <li>
To create our new Controller, right-click the <em>Controller</em> folder and select
Add Controller. Since the name matters in ASP.NET MVC's convention-over-configuration
style, name it EmployeeController.cs (or .vb, if you happen to be working in Visual
Basic .NET). Be sure to check the "Add action methods for Create, Update, Detail,
and Delete scenarios" as we will be using them later. 
</li>
          <li>
We now have a basic controller, but it doesn't do anything yet. First, we want to
modify our Index action, as it is the default action in the controller. We will use
this action to list all of the Employees that currently exist in our database. Your
Index() action method should contain the following code: 
<br /><pre class="csharp:nocontrols" name="code">var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.ToList());</pre></li>
          <li>
Save all and compile. 
</li>
        </ol>
        <h4>Creating a View
</h4>
        <p>
We have our Model and we have our Controller, so it is time for our View. The View
is responsible for display only—it should contain virtually no logic. Our controller
doesn't do anything yet, but we can at least get the base file structure and navigation
in place. Since our Model governs <em>how</em> and our Controller governs <em>what</em>,
think of the View as governing <em>where</em>, as it is responsible for deciding where
each data element gets displayed on your page.
</p>
        <ol>
          <li>
Once you have saved and compiled your Controller, right-click on the Index action
name and select Add View. 
</li>
          <li>
The View should be named Index, just like your Controller action. Also, make your
View strongly typed to the Employee model. Since a list of Employees is being passed
to the View from the Controller, making the View strongly-typed prevents us from having
to cast our View Model from object to Employee. Finally, since we are providing a
list of Employees, the View Content should be a List. 
</li>
          <li>
The <em>Employee</em> folder should be automatically created under the <em>/Views/</em> folder,
and the <em>Index.aspx</em> View inside of this new <em>Employee</em> folder. 
</li>
          <li>
The last thing we need to do is provide some navigation to this view. Open up <em>/Views/Shared/Site.Master</em> and
add an ActionLink within the Menu Container section to the Index action of the Employee
controller. When you are done, the Menu Container should look like this: 
<br /><pre class="csharp:nocontrols" name="code">&lt;ul id="menu"&gt;              
  &lt;li&gt;&lt;%= Html.ActionLink("Home", "Index", "Home")%&gt;&lt;/li&gt;
  &lt;li&gt;&lt;%= Html.ActionLink("About", "About", "Home")%&gt;&lt;/li&gt;
  &lt;li&gt;&lt;%= Html.ActionLink("Employees", "Index", "Employee")%&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></li>
          <li>
Save all and run. When you navigate to your Employees link, you should get a list
of all employees currently in the database with an Edit, Details, and Delete links. 
</li>
        </ol>
        <blockquote>
          <h5>New in ASP.NET MVC 2: Strongly Typed HTML Helpers
</h5>
          <p>
In the previous version of ASP.NET MVC, HTML helpers were simple generic classes.
Generated views were full of Magic Strings for each property in your model, such as
&lt;%= Html.TextBox("Name") %&gt;, opening the door for a fat-fingered property
name. MVC 2 includes strongly-typed HTML helpers on strongly-typed views. For form-based
views, use the new strongly-typed "For" methods, such as &lt;%= Html.TextBoxFor(model
=&gt; model.Name) %&gt; or &lt;%= Html.EditorFor(model =&gt; model.Name) %&gt; to
eliminate the risk of incorrectly entering a property name. For display fields, use
Html.DisplayFor() to provide similar benefits for your read-only data, including the
elimination of HTML encoding for each field.
</p>
        </blockquote>
        <h4>Adding New Data
</h4>
        <p>
A list of employees is great, but we also need the ability to manipulate that data.
First, let's start with the ability to create new data.
</p>
        <ol>
          <li>
Within Visual Studio, open <em>/Controllers/EmployeeController</em> and navigate to
the POST Create action. POST is an HTTP verb associated with pushing data to the server
in the HTTP header, commonly associated with form submits. GET is the HTTP verb for
pure retrieval, commonly associated with a direct URL request, such as clicking a
link. In this case, the POST action can be identified by the [HttpPost] decoration. 
</li>
          <li>
The method arguments currently include only a FormCollection object that will contain
all of the header values associated with the POST. However, MVC is smart; it can automatically
transform this collection into a type-safe Model, based on the names of the header
variables (the identities of the HTML form inputs match the names of the Model's properties).
The one exception is the Id attribute, which is available in the Model but not populated
until after the object is saved to the database. To get strong typing, and avoid having
to manually cast or map form collection data to a new instance of our Model, change
the method signature to the following: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Create([Bind(Exclude="Id")] Employee newEmployee)</pre></li>
          <li>
Now that we have a populated Model, we just need to save the Model to the database
and redirect back to the List when we are done. Do this by replacing the contents
of the action method with the following code: 
<br /><pre class="csharp:nocontrols" name="code">try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  _entities.AddToEmployee(employee);
  _entities.SaveChanges();
  return RedirectToAction("Index");
}
catch
{
  return View();
}</pre></li>
          <li>
Save and compile. 
</li>
          <li>
Now we must create the View for adding data. As we did with Index, we can create the
view by right-clicking the Action method and selecting Add View. The view content
should be Create. 
</li>
          <li>
The only modification for the Create view is the Id property. The generated code creates
an input for this property, but it is not needed since the column value is auto-populated
by the database when saving the entity. Remove this input and label from the form. 
</li>
          <li>
Save and run. You should now be able to add new records to the database. 
</li>
        </ol>
        <blockquote>
          <h5>New in ASP.NET MVC2: Better Verb Attributes
</h5>
          <p>
In the first version of ASP.NET MVC, HTTP Verb conditions were placed on an Action
via the AcceptVerbsAttribute, such as the Create action's [AcceptVerbs(HttpVerbs.Post)].
In ASP.NET MVC 2, these attributes have been simplified with the introduction of the
HttpGetAttribute, HttpDeleteAttribute, HttpPostAttribute, and HttpPutAttribute.
</p>
        </blockquote>
        <h4>Routing and Updates
</h4>
        <p>
The end user can view a list of Employees, and can create new employees, but when
the end user clicks the Edit or Detail links, they get an error since these Views
haven't been created yet and the Actions are not implemented. One by one, we will
get the new views in place.
</p>
        <ol>
          <li>
Within Visual Studio, open <em>/Controllers/EmployeeController</em> and navigate to
the Details action. You may notice that the method already accepts an integer as input,
and shows example usage of the action in a commented URL above the method: <em>/Employee/Details/5</em>.
This integer is the identity value of the Employee record, and is already populated
in the links of our List view created in the previous section. 
</li>
          <li>
Within Visual Studio, open <em>Global.asax</em> and navigate to the RegisterRoutes
method. The default route for ASP.NET MVC is <em>/{controller}/{action}/{id}/</em>.
By parsing out any URL into the application, MVC can determine which Controller to
use, which Action to execute, and which arguments to pass to the Action. Later portions
of the URL path are often optional, and when not specified, are replaced with the
default values: Home, Index, and String.Empty. The URLs of "/", "/Home",
"/Home/", "/Home/Index", and "/Home/Index/" are all
equivalent URLs in the eyes of ASP.NET MVC. 
</li>
        </ol>
        <p>
Go back to the Employee controller. Now that we know what the integer argument is
for, we need to retrieve the Employee matching the associated identity and pass it
to a view for editing or display.
</p>
        <ol>
          <li>
Replace the contents of the GET version of the Edit action method with the following
code to retrieve the Employee from the database that matches the identity specified
in the route: 
<br /><pre class="csharp:nocontrols" name="code">var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.Where(emp =&gt; emp.Id == id).First());</pre></li>
          <li>
Save and compile. 
</li>
          <li>
Right-click the Action and add the View. The View should still be strongly typed,
but this time the view content should be Edit. 
</li>
          <li>
The Details and GET Delete actions are largely similar as the GET Edit action, except
that the view is labels instead of text boxes. Repeat the above three steps for the
Details action method with a Details view content and for the GET Delete action method
with a Delete view content. 
</li>
          <li>
As with the Create view, the Id property should be removed from the Edit view, as
it is not an item that should be edited by the end user. 
</li>
          <li>
Save and run. You should see the details of an Employee when clicking on the Edit
and Details links. 
</li>
          <li>
We can view Employee details within the Edit form, but when we make changes and submit,
nothing happens. We need to modify the POST Edit action to save our changes back to
the database. The default POST Edit action accepts an id and a FormCollection as input
arguments, but similarly to the POST Create action, we can change this to use our
strongly typed model to avoid having to cast or map data. However, unlike our Create
action, we need to bind the id property so that the system knows which record to update.
To make these modifications, replace the POST Edit signature with the following: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Edit(MvcJamSession.Models.Employee employee)</pre></li>
          <li>
Replace the contents of the POST Edit action method with the following code to save
the changes to the database: 
<br /><pre class="csharp:nocontrols" name="code">try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  var _originalEmployee =
    _entities.Employee.Where(emp =&gt; emp.Id == employee.Id).First();
  _entities.ApplyPropertyChanges(_originalEmployee.EntityKey.EntitySetName,
                                 employee);
  _entities.SaveChanges();
  return RedirectToAction("Index");
}
catch
{
  return View();
}</pre></li>
          <li>
Replace the signature of the POST Delete action method with the following code to
provide strong typing on our model: 
<br /><pre class="csharp:nocontrols" name="code">public ActionResult Delete(MvcJamSession.Models.Employee employee)</pre></li>
          <li>
Finally, replace the contents of the POST Delete action method with the following
code to delete a record: 
<br /><pre class="csharp:nocontrols" name="code">var _entites = new MvcJamSession.Models.MvcJamSessionEntities();
var originalEmployee =
  _entites.Employee.Where(emp =&gt; emp.Id == deletedEmployee.Id).First();
_entites.DeleteObject(originalEmployee);
_entites.SaveChanges();
return RedirectToAction("Index");</pre></li>
          <li>
Save, compile, and run. You should now be able to modify existing Employee records. 
</li>
        </ol>
        <p>
We now have a fully-functional ASP.NET MVC application to manage our Employee records.
Congratulations!
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:13d5b303-4a7f-4ac9-93a3-f208ffe24fe0" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag">ASP.NET MVC</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/Jam+Session" rel="tag">Jam
Session</a>,<a href="http://technorati.com/tags/Coding+Exercise" rel="tag">Coding
Exercise</a>,<a href="http://technorati.com/tags/Learn+to+Code" rel="tag">Learn to
Code</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d5d9618-a51a-471f-a5ec-7381c916f11d" />
      </body>
      <title>Learn to Code ASP.NET MVC 2 : Introduction to ASP.NET MVC 2</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</guid>
      <link>http://www.cptloadtest.com/2010/02/23/Learn-To-Code-ASPNET-MVC-2-Introduction-To-ASPNET-MVC-2.aspx</link>
      <pubDate>Tue, 23 Feb 2010 22:38:00 GMT</pubDate>
      <description>&lt;p&gt;
On Tuesday, February 9th, I was scheduled lead a jam session for Come Jam With Us,
the software developer study group in Ann Arbor. The session was to be on ASP.NET
MVC 2, aimed to give attendees enough of an introduction to the product to empower
developers to be able to start coding their own ASP.NET MVC 2 projects. Unfortunately,
Mother Nature did not cooperate that day, half of the state of Michigan seemingly
shut down under 8&amp;quot; of snow, and the session was cancelled and rescheduled for
February 23rd. The goal of these Learn to Code exercises is to give you an introduction
to building applications with ASP.NET MVC 2. In the near future, I also hope to provide
a screen cast of these same exercises.
&lt;/p&gt;
&lt;h3&gt;About this Exercise
&lt;/h3&gt;
&lt;p&gt;
This coding exercise is designed to give you an introduction to ASP.NET MVC 2. In
this exercise, developers will create their first database-driven ASP.NET MVC 2 application
within Visual Studio, primarily using code generation built in to Visual Studio. Developers
performing this exercise should be familiar with ASP.NET development and Visual Studio,
but no previous experience with ASP.NET MVC is required.
&lt;/p&gt;
&lt;h3&gt;Prerequisites
&lt;/h3&gt;
&lt;p&gt;
You will need few things for ASP.NET MVC 2 application development and to complete
these exercises. Please complete the following prerequisites prior to moving on. The
session is designed to be completed in about an hour, but prerequisite setup is not
included in that time.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/visualstudio/"&gt;Microsoft Visual Studio 2008
SP1&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Web/"&gt;Visual Web Developer 2008
Express&lt;/a&gt; with SP1 
&lt;/li&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/sqlserver/2008/"&gt;Microsoft SQL Server&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Database/"&gt;SQL
Server Express&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Download and install the latest &lt;a title="ASP.NET MVC 2 RC 2 on Microsoft" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;amp;displaylang=en"&gt;ASP.NET
MVC 2 RC 2 bits from Microsoft&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Exercise 0: Getting Started
&lt;/h3&gt;
&lt;h4&gt;Creating a Project
&lt;/h4&gt;
&lt;p&gt;
Before any coding can occur, the first thing that we have to do is create a new ASP.NET
MVC 2 project from within Visual Studio.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
In Visual Studio, create a new &amp;quot;ASP.NET MVC 2 Web Application&amp;quot; named MvcJamSession.
You can create your project in either Visual Basic or Visual C#, though all of the
examples in this post will be in C#. 
&lt;/li&gt;
&lt;li&gt;
After selecting the project type and solution/project name, you will be prompted for
creating a unit test project. Select &amp;quot;Yes.&amp;quot; Though we will not be using
these tests in this exercise, we will be in future installments. 
&lt;/li&gt;
&lt;li&gt;
Be sure that your MvcJamSession.Test project includes a project reference back to
your MvcJamSession project. 
&lt;/li&gt;
&lt;li&gt;
Compile and run. Your browser should display a blue web site showing &amp;quot;Welcome
to ASP.NET MVC!&amp;quot; Congratulations. You now have your first ASP.NET MVC application. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Convention-Based Development
&lt;/h4&gt;
&lt;p&gt;
&lt;a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Project Folder Structure of MVC Jam Session Project" border="0" alt="Project Folder Structure of MVC Jam Session Project" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/SolutionExplorer-MvcJamSession_thumb.jpg" width="200" height="382" /&gt;&lt;/a&gt; Development
within ASP.NET MVC is based on convention over configuration. Certain naming conventions
are built in the system to eliminate the amount of boiler-plate code that you need
to recreate. That is not to say that you &lt;em&gt;must&lt;/em&gt; follow these naming conventions—you
may use whatever convention you like—but by straying from the standardized conventions,
you will be creating a lot of extra work for yourself. With our new ASP.NET MVC 2
Project, a few of these conventions are immediately visible.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Controllers&lt;/em&gt; folder. This is where all Controller classes must go. Individual
classes must be named &lt;em&gt;&amp;lt;Name&amp;gt;Controller&lt;/em&gt;. The default project includes &lt;em&gt;HomeController&lt;/em&gt;,
which governs the Home and About actions, and &lt;em&gt;AccountController&lt;/em&gt;, which governs
the log in, log out, change password, and registration actions. If we were to make
an application that manages Widgets, we would likely have a class named &lt;em&gt;WidgetController&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Views&lt;/em&gt; folder. This is where all of the Views must go. By default, views are
paired one-to-one with controller actions, such as one view for new user registration
and another view for changing your password. Views are also separated into folders
matching the associated controller name—&lt;em&gt;/Views/&amp;lt;ControllerName&amp;gt;/&amp;lt;ActionName&amp;gt;&lt;/em&gt;.
Thus, &lt;em&gt;HomeController&lt;/em&gt;'s About action is associated with the &lt;em&gt;/Views/Home/About&lt;/em&gt; view. 
&lt;br /&gt;
The Views folder also contains a &lt;em&gt;Shared&lt;/em&gt; folder. This folder is where any
common views, such as a Master Page, would reside. The &lt;em&gt;Shared&lt;/em&gt; folder is also
where the ViewEngine cascades to when it can't find an appropriate view in the /&lt;em&gt;Views/&amp;lt;ControllerName&amp;gt;&lt;/em&gt; folder;
if &lt;em&gt;/Views/Home/About&lt;/em&gt; didn't exist, the ViewEngine would look for &lt;em&gt;/Views/Shared/About&lt;/em&gt;.
This can come in handy for common pages shared by all controllers, such as an error
page. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Session Exercise 1: Building an Application
&lt;/h3&gt;
&lt;p&gt;
Using the project we just created, we're going to create an application that manage
a list of employees, including their name, job title, date of hire, and date of termination.
Though the default project is a great help on some applications, it can get in the
way on others; we're not going to need any account services in our application, so
we need to first trim the project down a little.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Delete the entire &lt;em&gt;Account&lt;/em&gt; folder under /Views/. 
&lt;/li&gt;
&lt;li&gt;
Delete &lt;em&gt;AccountController.cs&lt;/em&gt; from the Controllers folder. 
&lt;/li&gt;
&lt;li&gt;
Delete shared partial view &lt;em&gt;/Views/Shared/LogOnUserControl.ascx&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
Delete reference to this partial view by removing the &amp;quot;LoginDisplay&amp;quot; DIV
from&lt;em&gt; /Views/Shared/Site.Master&lt;/em&gt;, lines 18-20. 
&lt;/li&gt;
&lt;li&gt;
Removing &lt;em&gt;LoginDisplay&lt;/em&gt; will cause a slight layout problem from the CSS. To
fix it, modify the &lt;em&gt;#menucontainer&lt;/em&gt; definition in &lt;em&gt;/Content/Site.css&lt;/em&gt; on
line 263. 
&lt;br /&gt;
&lt;pre class="css:nocontrols" name="code"&gt;#menucontainer
{
    padding-top:40px;
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all, compile, and Run. The site should be functioning normally, but without the
Log In link in the top right of the home page. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Database
&lt;/h4&gt;
&lt;p&gt;
Like any dynamic web site, we need a storage mechanism. Create a database in SQL Server
or SQL Server Express with an Employee table containing columns for name, job title,
hired date, and termination date, as well as an identity column for finding records.&lt;a href="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_1.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 5px 15px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MvcJamSession-EmployeeTable" border="0" alt="MvcJamSession-EmployeeTable" align="right" src="http://www.cptloadtest.com/content/binary/WindowsLiveWriter/ASP.NETMVCJamSession_14C62/MvcJamSession-EmployeeTable_thumb_1.jpg" width="250" height="283" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
For those with SQL Server Express, create a new database through right-clicking the
App_Data folder, and adding a new item. The new item should be a SQL Server Database. 
&lt;/li&gt;
&lt;li&gt;
For those with SQL Server, create a new database through SQL Server Management Studio
(SSMS). 
&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;
Create a new table called &lt;em&gt;Employee&lt;/em&gt;. 
&lt;/li&gt;
&lt;li&gt;
Create the following columns within the new table: 
&lt;ul&gt;
&lt;li&gt;
Id (primary key, identity, int, not null) 
&lt;/li&gt;
&lt;li&gt;
Name (varchar(50), not null) 
&lt;/li&gt;
&lt;li&gt;
JobTitle (varchar(50), not null) 
&lt;/li&gt;
&lt;li&gt;
HiredOn (date, not null) 
&lt;/li&gt;
&lt;li&gt;
TerminatedOn (date, null) 
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Add a record or two to the table for good measure. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Model
&lt;/h4&gt;
&lt;p&gt;
The next thing we need to create is our Model. Not only is it the code representation
for our business entity (An Employee class contains Name, JobTitle, HiredOn, and TerminatedOn
properties), it is also responsible for &lt;em&gt;how&lt;/em&gt; to get, save, or delete data
from the database. We will be using Microsoft Entity Framework through Visual Studio
to generate our model for us.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
To create our new Model, right-click the &lt;em&gt;Model&lt;/em&gt; folder and select Add New
Item. The new item should be an ADO.NET Entity Data Model, found within the Data category
of the Add New Item dialog. Name it MvcJamSessionEntities. 
&lt;/li&gt;
&lt;li&gt;
Generate the Model from a database, connecting to your MVC Jam Session database created
in the previous section. On the step where you select your database connection, be
sure to allow Visual Studio to add the connection string to your web.config by checking
the appropriate checkbox. 
&lt;/li&gt;
&lt;li&gt;
The &lt;em&gt;Employee&lt;/em&gt; table is the only Data Object that needs to be generated. 
&lt;/li&gt;
&lt;li&gt;
When the dialog completes, your Entity Data Model (a .edmx file) should be displayed
in design view. 
&lt;/li&gt;
&lt;li&gt;
Save all and compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a Controller
&lt;/h4&gt;
&lt;p&gt;
Now that our model is in place, we need to create a controller. The controller is
responsible for managing all interaction between the end-user and the application,
including identifying what data to get, save, or delete. (Remember, though the Controller
is responsible for &lt;em&gt;what&lt;/em&gt;, the Model is responsible for &lt;em&gt;how&lt;/em&gt;.)
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
To create our new Controller, right-click the &lt;em&gt;Controller&lt;/em&gt; folder and select
Add Controller. Since the name matters in ASP.NET MVC's convention-over-configuration
style, name it EmployeeController.cs (or .vb, if you happen to be working in Visual
Basic .NET). Be sure to check the &amp;quot;Add action methods for Create, Update, Detail,
and Delete scenarios&amp;quot; as we will be using them later. 
&lt;/li&gt;
&lt;li&gt;
We now have a basic controller, but it doesn't do anything yet. First, we want to
modify our Index action, as it is the default action in the controller. We will use
this action to list all of the Employees that currently exist in our database. Your
Index() action method should contain the following code: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.ToList());&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all and compile. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Creating a View
&lt;/h4&gt;
&lt;p&gt;
We have our Model and we have our Controller, so it is time for our View. The View
is responsible for display only—it should contain virtually no logic. Our controller
doesn't do anything yet, but we can at least get the base file structure and navigation
in place. Since our Model governs &lt;em&gt;how&lt;/em&gt; and our Controller governs &lt;em&gt;what&lt;/em&gt;,
think of the View as governing &lt;em&gt;where&lt;/em&gt;, as it is responsible for deciding where
each data element gets displayed on your page.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Once you have saved and compiled your Controller, right-click on the Index action
name and select Add View. 
&lt;/li&gt;
&lt;li&gt;
The View should be named Index, just like your Controller action. Also, make your
View strongly typed to the Employee model. Since a list of Employees is being passed
to the View from the Controller, making the View strongly-typed prevents us from having
to cast our View Model from object to Employee. Finally, since we are providing a
list of Employees, the View Content should be a List. 
&lt;/li&gt;
&lt;li&gt;
The &lt;em&gt;Employee&lt;/em&gt; folder should be automatically created under the &lt;em&gt;/Views/&lt;/em&gt; folder,
and the &lt;em&gt;Index.aspx&lt;/em&gt; View inside of this new &lt;em&gt;Employee&lt;/em&gt; folder. 
&lt;/li&gt;
&lt;li&gt;
The last thing we need to do is provide some navigation to this view. Open up &lt;em&gt;/Views/Shared/Site.Master&lt;/em&gt; and
add an ActionLink within the Menu Container section to the Index action of the Employee
controller. When you are done, the Menu Container should look like this: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;&amp;lt;ul id=&amp;quot;menu&amp;quot;&amp;gt;              
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;Home&amp;quot;, &amp;quot;Index&amp;quot;, &amp;quot;Home&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;About&amp;quot;, &amp;quot;About&amp;quot;, &amp;quot;Home&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;%= Html.ActionLink(&amp;quot;Employees&amp;quot;, &amp;quot;Index&amp;quot;, &amp;quot;Employee&amp;quot;)%&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save all and run. When you navigate to your Employees link, you should get a list
of all employees currently in the database with an Edit, Details, and Delete links. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt; 
&lt;h5&gt;New in ASP.NET MVC 2: Strongly Typed HTML Helpers
&lt;/h5&gt;
&lt;p&gt;
In the previous version of ASP.NET MVC, HTML helpers were simple generic classes.
Generated views were full of Magic Strings for each property in your model, such as
&amp;lt;%= Html.TextBox(&amp;quot;Name&amp;quot;) %&amp;gt;, opening the door for a fat-fingered property
name. MVC 2 includes strongly-typed HTML helpers on strongly-typed views. For form-based
views, use the new strongly-typed &amp;quot;For&amp;quot; methods, such as &amp;lt;%= Html.TextBoxFor(model
=&amp;gt; model.Name) %&amp;gt; or &amp;lt;%= Html.EditorFor(model =&amp;gt; model.Name) %&amp;gt; to
eliminate the risk of incorrectly entering a property name. For display fields, use
Html.DisplayFor() to provide similar benefits for your read-only data, including the
elimination of HTML encoding for each field.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h4&gt;Adding New Data
&lt;/h4&gt;
&lt;p&gt;
A list of employees is great, but we also need the ability to manipulate that data.
First, let's start with the ability to create new data.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;/Controllers/EmployeeController&lt;/em&gt; and navigate to
the POST Create action. POST is an HTTP verb associated with pushing data to the server
in the HTTP header, commonly associated with form submits. GET is the HTTP verb for
pure retrieval, commonly associated with a direct URL request, such as clicking a
link. In this case, the POST action can be identified by the [HttpPost] decoration. 
&lt;/li&gt;
&lt;li&gt;
The method arguments currently include only a FormCollection object that will contain
all of the header values associated with the POST. However, MVC is smart; it can automatically
transform this collection into a type-safe Model, based on the names of the header
variables (the identities of the HTML form inputs match the names of the Model's properties).
The one exception is the Id attribute, which is available in the Model but not populated
until after the object is saved to the database. To get strong typing, and avoid having
to manually cast or map form collection data to a new instance of our Model, change
the method signature to the following: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Create([Bind(Exclude=&amp;quot;Id&amp;quot;)] Employee newEmployee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Now that we have a populated Model, we just need to save the Model to the database
and redirect back to the List when we are done. Do this by replacing the contents
of the action method with the following code: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  _entities.AddToEmployee(employee);
  _entities.SaveChanges();
  return RedirectToAction(&amp;quot;Index&amp;quot;);
}
catch
{
  return View();
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save and compile. 
&lt;/li&gt;
&lt;li&gt;
Now we must create the View for adding data. As we did with Index, we can create the
view by right-clicking the Action method and selecting Add View. The view content
should be Create. 
&lt;/li&gt;
&lt;li&gt;
The only modification for the Create view is the Id property. The generated code creates
an input for this property, but it is not needed since the column value is auto-populated
by the database when saving the entity. Remove this input and label from the form. 
&lt;/li&gt;
&lt;li&gt;
Save and run. You should now be able to add new records to the database. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt; 
&lt;h5&gt;New in ASP.NET MVC2: Better Verb Attributes
&lt;/h5&gt;
&lt;p&gt;
In the first version of ASP.NET MVC, HTTP Verb conditions were placed on an Action
via the AcceptVerbsAttribute, such as the Create action's [AcceptVerbs(HttpVerbs.Post)].
In ASP.NET MVC 2, these attributes have been simplified with the introduction of the
HttpGetAttribute, HttpDeleteAttribute, HttpPostAttribute, and HttpPutAttribute.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h4&gt;Routing and Updates
&lt;/h4&gt;
&lt;p&gt;
The end user can view a list of Employees, and can create new employees, but when
the end user clicks the Edit or Detail links, they get an error since these Views
haven't been created yet and the Actions are not implemented. One by one, we will
get the new views in place.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;/Controllers/EmployeeController&lt;/em&gt; and navigate to
the Details action. You may notice that the method already accepts an integer as input,
and shows example usage of the action in a commented URL above the method: &lt;em&gt;/Employee/Details/5&lt;/em&gt;.
This integer is the identity value of the Employee record, and is already populated
in the links of our List view created in the previous section. 
&lt;/li&gt;
&lt;li&gt;
Within Visual Studio, open &lt;em&gt;Global.asax&lt;/em&gt; and navigate to the RegisterRoutes
method. The default route for ASP.NET MVC is &lt;em&gt;/{controller}/{action}/{id}/&lt;/em&gt;.
By parsing out any URL into the application, MVC can determine which Controller to
use, which Action to execute, and which arguments to pass to the Action. Later portions
of the URL path are often optional, and when not specified, are replaced with the
default values: Home, Index, and String.Empty. The URLs of &amp;quot;/&amp;quot;, &amp;quot;/Home&amp;quot;,
&amp;quot;/Home/&amp;quot;, &amp;quot;/Home/Index&amp;quot;, and &amp;quot;/Home/Index/&amp;quot; are all
equivalent URLs in the eyes of ASP.NET MVC. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Go back to the Employee controller. Now that we know what the integer argument is
for, we need to retrieve the Employee matching the associated identity and pass it
to a view for editing or display.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Replace the contents of the GET version of the Edit action method with the following
code to retrieve the Employee from the database that matches the identity specified
in the route: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
return View(_entities.Employee.Where(emp =&amp;gt; emp.Id == id).First());&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save and compile. 
&lt;/li&gt;
&lt;li&gt;
Right-click the Action and add the View. The View should still be strongly typed,
but this time the view content should be Edit. 
&lt;/li&gt;
&lt;li&gt;
The Details and GET Delete actions are largely similar as the GET Edit action, except
that the view is labels instead of text boxes. Repeat the above three steps for the
Details action method with a Details view content and for the GET Delete action method
with a Delete view content. 
&lt;/li&gt;
&lt;li&gt;
As with the Create view, the Id property should be removed from the Edit view, as
it is not an item that should be edited by the end user. 
&lt;/li&gt;
&lt;li&gt;
Save and run. You should see the details of an Employee when clicking on the Edit
and Details links. 
&lt;/li&gt;
&lt;li&gt;
We can view Employee details within the Edit form, but when we make changes and submit,
nothing happens. We need to modify the POST Edit action to save our changes back to
the database. The default POST Edit action accepts an id and a FormCollection as input
arguments, but similarly to the POST Create action, we can change this to use our
strongly typed model to avoid having to cast or map data. However, unlike our Create
action, we need to bind the id property so that the system knows which record to update.
To make these modifications, replace the POST Edit signature with the following: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Edit(MvcJamSession.Models.Employee employee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Replace the contents of the POST Edit action method with the following code to save
the changes to the database: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;try
{
  if (!ModelState.IsValid) return View();

  var _entities = new MvcJamSession.Models.MvcJamSessionEntities();
  var _originalEmployee =
    _entities.Employee.Where(emp =&amp;gt; emp.Id == employee.Id).First();
  _entities.ApplyPropertyChanges(_originalEmployee.EntityKey.EntitySetName,
                                 employee);
  _entities.SaveChanges();
  return RedirectToAction(&amp;quot;Index&amp;quot;);
}
catch
{
  return View();
}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Replace the signature of the POST Delete action method with the following code to
provide strong typing on our model: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public ActionResult Delete(MvcJamSession.Models.Employee employee)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Finally, replace the contents of the POST Delete action method with the following
code to delete a record: 
&lt;br /&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;var _entites = new MvcJamSession.Models.MvcJamSessionEntities();
var originalEmployee =
  _entites.Employee.Where(emp =&amp;gt; emp.Id == deletedEmployee.Id).First();
_entites.DeleteObject(originalEmployee);
_entites.SaveChanges();
return RedirectToAction(&amp;quot;Index&amp;quot;);&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Save, compile, and run. You should now be able to modify existing Employee records. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
We now have a fully-functional ASP.NET MVC application to manage our Employee records.
Congratulations!
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:13d5b303-4a7f-4ac9-93a3-f208ffe24fe0" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag"&gt;ASP.NET MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Jam+Session" rel="tag"&gt;Jam
Session&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Coding+Exercise" rel="tag"&gt;Coding
Exercise&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Learn+to+Code" rel="tag"&gt;Learn to
Code&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d5d9618-a51a-471f-a5ec-7381c916f11d" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,1d5d9618-a51a-471f-a5ec-7381c916f11d.aspx</comments>
      <category>ASP.Net</category>
      <category>Learn to Code</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=f8562c46-4083-4d56-bc7c-42bff947af62</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f8562c46-4083-4d56-bc7c-42bff947af62</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On Tuesday, February 23rd, I will be leading the jam session for <a href="http://www.comejamwithus.org">Come
Jam With Us</a>, the software developer study group in Ann Arbor. The session will
be on ASP.NET MVC, and aims to give attendees enough of an introduction to the product
to empower developers to be able to start coding their own ASP.NET MVC 2 projects.
Like all of the Come Jam With Us sessions for the winter/spring of 2010, it will be
held at the offices of SRT Solutions in Ann Arbor at 5:30p.
</p>
        <h3>Prerequisites
</h3>
        <p>
You will need few things for ASP.NET MVC 2 application development and to complete
this exercise. Please complete the following prerequisites prior to the session, otherwise
you likely will spend the hour downloading and installing rather than coding.
</p>
        <ul>
          <li>
Install <a href="http://www.microsoft.com/visualstudio/">Microsoft Visual Studio 2008
SP1</a> or <a href="http://www.microsoft.com/express/Web/">Visual Web Developer 2008
Express</a> with SP1 
</li>
          <li>
Install <a href="http://www.microsoft.com/sqlserver/2008/">Microsoft SQL Server</a> or <a href="http://www.microsoft.com/express/Database/">SQL
Server Express</a></li>
          <li>
Download and install <a title="ASP.NET MVC 2 on Microsoft.com" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;displaylang=en">ASP.NET
MVC 2 from Microsoft.com</a></li>
        </ul>
        <h3>Rescheduled
</h3>
        <p>
Due to weather, the original February 9th meeting was cancelled. This session is rescheduled
for Tuesday, February 23rd.
</p>
        <p>
        </p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3e0d3cc-b34f-4985-84a1-155c955740ed" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Ann+Arbor" rel="tag">Ann Arbor</a>,<a href="http://technorati.com/tags/Software" rel="tag">Software</a>,<a href="http://technorati.com/tags/Code+Jam" rel="tag">Code
Jam</a>,<a href="http://technorati.com/tags/Study+Group" rel="tag">Study Group</a>,<a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag">ASP.NET
MVC</a>,<a href="http://technorati.com/tags/ASP.NET+MVC2" rel="tag">ASP.NET MVC2</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=f8562c46-4083-4d56-bc7c-42bff947af62" />
      </body>
      <title>Upcoming ASP.NET MVC 2 Study Group Jam Session in Ann Arbor</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</guid>
      <link>http://www.cptloadtest.com/2010/02/08/Upcoming-ASPNET-MVC-2-Study-Group-Jam-Session-In-Ann-Arbor.aspx</link>
      <pubDate>Mon, 08 Feb 2010 04:45:36 GMT</pubDate>
      <description>&lt;p&gt;
On Tuesday, February 23rd, I will be leading the jam session for &lt;a href="http://www.comejamwithus.org"&gt;Come
Jam With Us&lt;/a&gt;, the software developer study group in Ann Arbor. The session will
be on ASP.NET MVC, and aims to give attendees enough of an introduction to the product
to empower developers to be able to start coding their own ASP.NET MVC 2 projects.
Like all of the Come Jam With Us sessions for the winter/spring of 2010, it will be
held at the offices of SRT Solutions in Ann Arbor at 5:30p.
&lt;/p&gt;
&lt;h3&gt;Prerequisites
&lt;/h3&gt;
&lt;p&gt;
You will need few things for ASP.NET MVC 2 application development and to complete
this exercise. Please complete the following prerequisites prior to the session, otherwise
you likely will spend the hour downloading and installing rather than coding.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/visualstudio/"&gt;Microsoft Visual Studio 2008
SP1&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Web/"&gt;Visual Web Developer 2008
Express&lt;/a&gt; with SP1 
&lt;/li&gt;
&lt;li&gt;
Install &lt;a href="http://www.microsoft.com/sqlserver/2008/"&gt;Microsoft SQL Server&lt;/a&gt; or &lt;a href="http://www.microsoft.com/express/Database/"&gt;SQL
Server Express&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Download and install &lt;a title="ASP.NET MVC 2 on Microsoft.com" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7aba081a-19b9-44c4-a247-3882c8f749e3&amp;amp;displaylang=en"&gt;ASP.NET
MVC 2 from Microsoft.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Rescheduled
&lt;/h3&gt;
&lt;p&gt;
Due to weather, the original February 9th meeting was cancelled. This session is rescheduled
for Tuesday, February 23rd.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3e0d3cc-b34f-4985-84a1-155c955740ed" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Ann+Arbor" rel="tag"&gt;Ann Arbor&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Software" rel="tag"&gt;Software&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Code+Jam" rel="tag"&gt;Code
Jam&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Study+Group" rel="tag"&gt;Study Group&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag"&gt;ASP.NET
MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET+MVC2" rel="tag"&gt;ASP.NET MVC2&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=f8562c46-4083-4d56-bc7c-42bff947af62" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,f8562c46-4083-4d56-bc7c-42bff947af62.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=9542d934-717f-4888-8164-5e0ba7843907</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,9542d934-717f-4888-8164-5e0ba7843907.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,9542d934-717f-4888-8164-5e0ba7843907.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9542d934-717f-4888-8164-5e0ba7843907</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
</p>
        <blockquote>
          <p style="color: maroon">
            <em>The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.</em>
          </p>
        </blockquote>
        <p>
The following solution applies to the first version of the ASP.NET MVC framework.
If you are looking for a solution in the ASP.NET MVC 2 framework, try <a href="http://www.cptloadtest.com/2010/04/05/Validating-ASPNET-MVC-2-Route-Values-With-DefaultValueAttribute.aspx">Validating
ASP.NET MVC 2 Route Values with DefaultValueAttribute</a>.
</p>
        <p>
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is a string, defaulting to String.Empty,
but it can be parsed to other types such as an integer for a default Details action.
The problem stems from when invalid values or missing values are passed to these integer-expecting
actions; MVC handles the inability to parse the value into an integer, but then throws
an exception trying to pass a null value to a Controller Action expecting a value
type for a method argument.
</p>
        <p>
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
</p>
        <p>
Using a custom ActionFilterAttribute, I can accomplish my goal. Prior to executing
my Controller Action, an ActionFilterAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this will allowing me to keep that method argument as a value type integer
and avoid Nullable&lt;int&gt;.HasValue.
</p>
        <h3>
        </h3>
        <h3>ForceIntegerRouteValueAttribute class
</h3>
        <pre class="csharp:nocontrols" name="code">public sealed class ForceIntegerRouteValueAttribute : ActionFilterAttribute
{
  private readonly int _defaultValue;
  private readonly string _routeValueName;

  public ForceIntegerRouteValueAttribute(string valueName, int defaultValue)
  {
    _routeValueName = valueName;
    _defaultValue = defaultValue;
  }

  public override void OnActionExecuting(ActionExecutingContext context)
  {
    base.OnActionExecuting(context);

    int testValue;
    if (!context.ActionParameters.ContainsKey(_routeValueName) ||
        !int.TryParse(context.RouteData.Values[_routeValueName].ToString(),
                      out testValue))
    {
      context.RouteData.Values[_routeValueName] = _defaultValue;
      context.Result = new RedirectToRouteResult(context.RouteData.Values);
    }
  }
}</pre>
        <p>
The attribute accepts a string matching the name of the route value to analyze and
a default value to assign to the route value should the validation fail. The override
on OnActionExecuting will cause the validation to fire immediately prior to the Action
being executed. The method attempts to parse the route value to an integer, and if
it fails it will set the route value to the default value and restart processing of
the route. RedirectToRouteResult restarts the route processing over again, since to
get to this point, the method must have changed a route value, and without the redirect,
the action would continue on with the original value. This will also redirect the
browser to route matching the new route value, such as redirecting ~/Widgets/Details/Foo
to ~/Widgets/Details/0.
</p>
        <h3>Usage
</h3>
        <pre class="csharp:nocontrols" name="code">public class WidgetsController : Controller
{
  [ForceIntegerRouteValue("id", 0)]
  public ActionResult Details(int id)
  {
    return View();
  }
}</pre>
        <p>
By simply adding the attribute to the Action, I can now validate that my identity
is an integer, allowing for some level of trust into user input values, and do so
without having to laden my code with unnecessary value checks. This same idea can
be used for any sort of pre-filtering, which could also include checking that the
number contains no more than two decimal places. Just don’t go too far with this idea.
Familiarize yourself with Route Constraints, too, as there will be times that a constraint
can better serve your business needs than an Action Filter. But for areas where this
solution does serve well, such as eliminating Nullable&lt;int&gt; in Action arguments,
this option just seems cleaner to me. And in the ASP.NET MVC World of keeping Controllers
as light as possible, I like clean.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:035a81e9-f452-4217-9318-7b1b84402f64" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/MVC" rel="tag">MVC</a>,<a href="http://technorati.com/tags/ActionFilterAttribute" rel="tag">ActionFilterAttribute</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=9542d934-717f-4888-8164-5e0ba7843907" />
      </body>
      <title>Validating ASP.NET MVC Route Values with ActionFilterAttribute</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,9542d934-717f-4888-8164-5e0ba7843907.aspx</guid>
      <link>http://www.cptloadtest.com/2009/12/01/Validating-ASPNET-MVC-Route-Values-With-ActionFilterAttribute.aspx</link>
      <pubDate>Tue, 01 Dec 2009 04:12:51 GMT</pubDate>
      <description>&lt;p&gt;
In ASP.NET MVC, the default signature for a Details action includes an Int32 method
argument. The system works fine when the expected data is entered, and the happy path
is followed, but put in an invalid value or no value at all, and an exception explodes
all over the user.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p style="color: maroon"&gt;
&lt;em&gt;The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcSampleApplication.Controllers.WidgetController'.&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The following solution applies to the first version of the ASP.NET MVC framework.
If you are looking for a solution in the ASP.NET MVC 2 framework, try &lt;a href="http://www.cptloadtest.com/2010/04/05/Validating-ASPNET-MVC-2-Route-Values-With-DefaultValueAttribute.aspx"&gt;Validating
ASP.NET MVC 2 Route Values with DefaultValueAttribute&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
By default, the third section of an ASP.NET MVC Route is the Id, passed as a method
argument to a controller action. This value is a string, defaulting to String.Empty,
but it can be parsed to other types such as an integer for a default Details action.
The problem stems from when invalid values or missing values are passed to these integer-expecting
actions; MVC handles the inability to parse the value into an integer, but then throws
an exception trying to pass a null value to a Controller Action expecting a value
type for a method argument.
&lt;/p&gt;
&lt;p&gt;
A common solution is to convert the method argument to a nullable integer, which will
automatically cause the argument to be null when the route value specified in the
URL is an empty or non-integer value. The solution works fine, but seems a little
lame to me; I want to avoid having to check HasValue within every action. I have to
check for invalid identity values anyway (a user with an id of –1 isn’t going to exist
in my system), so I would much rather default these invalid integers to one of these
known, invalid values.
&lt;/p&gt;
&lt;p&gt;
Using a custom ActionFilterAttribute, I can accomplish my goal. Prior to executing
my Controller Action, an ActionFilterAttribute can validate that the specified route
value meets an expected type, and correct the value to a default value if this validation
fails; this will allowing me to keep that method argument as a value type integer
and avoid Nullable&amp;lt;int&amp;gt;.HasValue.
&lt;/p&gt;
&lt;h3&gt;
&lt;/h3&gt;
&lt;h3&gt;ForceIntegerRouteValueAttribute class
&lt;/h3&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public sealed class ForceIntegerRouteValueAttribute : ActionFilterAttribute
{
  private readonly int _defaultValue;
  private readonly string _routeValueName;

  public ForceIntegerRouteValueAttribute(string valueName, int defaultValue)
  {
    _routeValueName = valueName;
    _defaultValue = defaultValue;
  }

  public override void OnActionExecuting(ActionExecutingContext context)
  {
    base.OnActionExecuting(context);

    int testValue;
    if (!context.ActionParameters.ContainsKey(_routeValueName) ||
        !int.TryParse(context.RouteData.Values[_routeValueName].ToString(),
                      out testValue))
    {
      context.RouteData.Values[_routeValueName] = _defaultValue;
      context.Result = new RedirectToRouteResult(context.RouteData.Values);
    }
  }
}&lt;/pre&gt;
&lt;p&gt;
The attribute accepts a string matching the name of the route value to analyze and
a default value to assign to the route value should the validation fail. The override
on OnActionExecuting will cause the validation to fire immediately prior to the Action
being executed. The method attempts to parse the route value to an integer, and if
it fails it will set the route value to the default value and restart processing of
the route. RedirectToRouteResult restarts the route processing over again, since to
get to this point, the method must have changed a route value, and without the redirect,
the action would continue on with the original value. This will also redirect the
browser to route matching the new route value, such as redirecting ~/Widgets/Details/Foo
to ~/Widgets/Details/0.
&lt;/p&gt;
&lt;h3&gt;Usage
&lt;/h3&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;public class WidgetsController : Controller
{
  [ForceIntegerRouteValue(&amp;quot;id&amp;quot;, 0)]
  public ActionResult Details(int id)
  {
    return View();
  }
}&lt;/pre&gt;
&lt;p&gt;
By simply adding the attribute to the Action, I can now validate that my identity
is an integer, allowing for some level of trust into user input values, and do so
without having to laden my code with unnecessary value checks. This same idea can
be used for any sort of pre-filtering, which could also include checking that the
number contains no more than two decimal places. Just don’t go too far with this idea.
Familiarize yourself with Route Constraints, too, as there will be times that a constraint
can better serve your business needs than an Action Filter. But for areas where this
solution does serve well, such as eliminating Nullable&amp;lt;int&amp;gt; in Action arguments,
this option just seems cleaner to me. And in the ASP.NET MVC World of keeping Controllers
as light as possible, I like clean.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:035a81e9-f452-4217-9318-7b1b84402f64" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MVC" rel="tag"&gt;MVC&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ActionFilterAttribute" rel="tag"&gt;ActionFilterAttribute&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=9542d934-717f-4888-8164-5e0ba7843907" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,9542d934-717f-4888-8164-5e0ba7843907.aspx</comments>
      <category>ASP.Net</category>
      <category>MVC</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=6e198e97-6c35-49d2-b959-41a3340d8991</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,6e198e97-6c35-49d2-b959-41a3340d8991.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,6e198e97-6c35-49d2-b959-41a3340d8991.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6e198e97-6c35-49d2-b959-41a3340d8991</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <div style="margin: 0px 0px 10px 30px; float: right">
          <a href="http://www.dayofdotnet.org/Lansing/">
            <img border="0" alt="Lansing Day of .Net, 1 August 2009 - I'll be there!" src="http://www.dayofdotnet.org/Lansing/2009/images/ldodn-100x160.png" width="100" height="160" />
          </a>
        </div>
        <p>
This Saturday, August 1st, I will be speaking at <a href="http://www.dayofdotnet.org/Lansing/2009/" target="_blank">Lansing
Day of .NET 2009</a>, at the Breslin Student Events Center at Michigan State University,
East Lansing, Michigan. This session will be the same ASP.NET Page Life Cycle talk
that I gave <a title="Speaking at CodeStock 2009 on ASP.NET Page Life Cycle" href="http://www.cptloadtest.com/2009/05/18/Speaking-At-CodeStock-2009-On-ASPNET-Page-Life-Cycle.aspx" target="_blank">last
month</a> at CodeStock.
</p>
        <blockquote>
          <h3>Dev Basics: The ASP.NET Page Life Cycle
</h3>
          <p>
            <em>Jay Harris / Session Level: 100 
<br /></em>When a request occurs for an ASP.NET page, the response is processed through
a series of events before being sent to the client browser. These events, known as
the Page Life Cycle, are a complicated headache when used improperly, manifesting
as odd exceptions, incorrect data, performance issues, and general confusion. It seems
simple when reading yet-another-book-on-ASP.NET, but never when applied in the real
world. In this session, we decompose this mess, and turn the Life Cycle into an effective
and productive tool. No ASP.NET MVC, no Dynamic Data, no MonoRail, no technologies
of tomorrow, just the basics of ASP.NET, using the tools we have available in the
office, today.
</p>
        </blockquote>
        <p>
If you can make it, I recommend attending LDODN09. There are some <a title="Lansing Day of .NET 2009 Sessions" href="http://www.dayofdotnet.org/Lansing/2009/Sessions.aspx" target="_blank">great
sessions</a> lined up, and it is all being provided free-of-charge (though the event
organizers are encouraging donations). <a title="Wrap Up on Last Year's Lansing Day of .NET" href="http://www.cptloadtest.com/2008/06/26/Lansing-Day-Of-Net-WrapUp.aspx" target="_blank">Last
year's event</a>, held at Lansing Community College, was the first Lansing Day of
.NET and the first event that I was involved in organizing. It went well, and from
the moment it was over I was looking forward to the next one. I'm not on the organizing
committee this year, but I am still sure that this one is destined to be great as
well. They rented the Breslin Center! If I knew nothing else, that would be enough.
</p>
        <p>
So come out to Lansing Day of .NET this Saturday. Registration is <a title="Lansing Day of .NET Registration" href="http://www.dayofdotnet.org/Lansing/2009/Register.aspx" target="_blank">still
open</a>.
</p>
        <p>
I hope to see you there.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:eeee88a5-b190-41e0-be21-ea20cdb67e8d" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Lansing+Day+of+.NET" rel="tag">Lansing Day
of .NET</a>,<a href="http://technorati.com/tags/Day+of+.NET" rel="tag">Day of .NET</a>,<a href="http://technorati.com/tags/Speaking" rel="tag">Speaking</a>,<a href="http://technorati.com/tags/Events" rel="tag">Events</a>,<a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6e198e97-6c35-49d2-b959-41a3340d8991" />
      </body>
      <title>Speaking at Lansing Day of .NET 2009 on ASP.NET Page Life Cycle</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,6e198e97-6c35-49d2-b959-41a3340d8991.aspx</guid>
      <link>http://www.cptloadtest.com/2009/07/28/Speaking-At-Lansing-Day-Of-NET-2009-On-ASPNET-Page-Life-Cycle.aspx</link>
      <pubDate>Tue, 28 Jul 2009 23:18:18 GMT</pubDate>
      <description>&lt;div style="margin: 0px 0px 10px 30px; float: right"&gt;&lt;a href="http://www.dayofdotnet.org/Lansing/"&gt;&lt;img border="0" alt="Lansing Day of .Net, 1 August 2009 - I&amp;#39;ll be there!" src="http://www.dayofdotnet.org/Lansing/2009/images/ldodn-100x160.png" width="100" height="160" /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;
This Saturday, August 1st, I will be speaking at &lt;a href="http://www.dayofdotnet.org/Lansing/2009/" target="_blank"&gt;Lansing
Day of .NET 2009&lt;/a&gt;, at the Breslin Student Events Center at Michigan State University,
East Lansing, Michigan. This session will be the same ASP.NET Page Life Cycle talk
that I gave &lt;a title="Speaking at CodeStock 2009 on ASP.NET Page Life Cycle" href="http://www.cptloadtest.com/2009/05/18/Speaking-At-CodeStock-2009-On-ASPNET-Page-Life-Cycle.aspx" target="_blank"&gt;last
month&lt;/a&gt; at CodeStock.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;Dev Basics: The ASP.NET Page Life Cycle
&lt;/h3&gt;
&lt;p&gt;
&lt;em&gt;Jay Harris / Session Level: 100 
&lt;br /&gt;
&lt;/em&gt;When a request occurs for an ASP.NET page, the response is processed through
a series of events before being sent to the client browser. These events, known as
the Page Life Cycle, are a complicated headache when used improperly, manifesting
as odd exceptions, incorrect data, performance issues, and general confusion. It seems
simple when reading yet-another-book-on-ASP.NET, but never when applied in the real
world. In this session, we decompose this mess, and turn the Life Cycle into an effective
and productive tool. No ASP.NET MVC, no Dynamic Data, no MonoRail, no technologies
of tomorrow, just the basics of ASP.NET, using the tools we have available in the
office, today.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
If you can make it, I recommend attending LDODN09. There are some &lt;a title="Lansing Day of .NET 2009 Sessions" href="http://www.dayofdotnet.org/Lansing/2009/Sessions.aspx" target="_blank"&gt;great
sessions&lt;/a&gt; lined up, and it is all being provided free-of-charge (though the event
organizers are encouraging donations). &lt;a title="Wrap Up on Last Year&amp;#39;s Lansing Day of .NET" href="http://www.cptloadtest.com/2008/06/26/Lansing-Day-Of-Net-WrapUp.aspx" target="_blank"&gt;Last
year's event&lt;/a&gt;, held at Lansing Community College, was the first Lansing Day of
.NET and the first event that I was involved in organizing. It went well, and from
the moment it was over I was looking forward to the next one. I'm not on the organizing
committee this year, but I am still sure that this one is destined to be great as
well. They rented the Breslin Center! If I knew nothing else, that would be enough.
&lt;/p&gt;
&lt;p&gt;
So come out to Lansing Day of .NET this Saturday. Registration is &lt;a title="Lansing Day of .NET Registration" href="http://www.dayofdotnet.org/Lansing/2009/Register.aspx" target="_blank"&gt;still
open&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
I hope to see you there.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:eeee88a5-b190-41e0-be21-ea20cdb67e8d" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Lansing+Day+of+.NET" rel="tag"&gt;Lansing Day
of .NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Day+of+.NET" rel="tag"&gt;Day of .NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Speaking" rel="tag"&gt;Speaking&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Events" rel="tag"&gt;Events&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=6e198e97-6c35-49d2-b959-41a3340d8991" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,6e198e97-6c35-49d2-b959-41a3340d8991.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>Speaking</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=1d359ae8-9825-413f-92dc-2b533314f154</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,1d359ae8-9825-413f-92dc-2b533314f154.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,1d359ae8-9825-413f-92dc-2b533314f154.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1d359ae8-9825-413f-92dc-2b533314f154</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The first installment of this series goes back to the beginning and describes each
of the events within ASP.NET Page Life Cycle. Understanding the basic fundamentals
of the ASP.NET Page Life Cycle, including the order and scope of influence for each
of the Page Life Cycle events, will help ensure that you are executing your custom
code at the right time, and in the right order, rather than stepping on yourself by
conflicting with core ASP.NET framework functionality. But this is only part of the
story, since there is more to the ASP.NET Page Life Cycle than just the page, itself.
</p>
        <h3>ASP.NET Page &amp; WebControl Event Execution Order
</h3>
        <p>
Pages would be nothing but a sea of crazy peach gradient backgrounds without the controls
to display content and to interact with the user. In addition to the order of the
various page events, it is often helpful to know the order in which a page and its
controls execute a single event. Does Page.Load execute before Control.Load? Does
Page.Init execute before Control.Init? Does myTextBox.TextChanged fire before myButton.Click?
And what about myTextBox1.TextChanged versus myTextBox2.TextChanged?
</p>
        <p>
Knowing the execution order of events within the control tree will make you a better
ASP.NET developer. If you cannot answer each of those questions above (and maybe even
if you can), keep reading.
</p>
        <blockquote>
          <h3>About the Series
</h3>
          <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
          <p>
Part 1: <a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Events
of the ASP.NET Page Life Cycle</a><br />
Part 2: ASP.NET Page &amp; WebControl Event Execution Order 
<br />
Part 3: <a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx">Getting
Started with ASP.NET Data Binding</a><br />
Part 4: <a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">Wiring
Events to your Page</a></p>
        </blockquote>
        <h3>Event Execution Order within the Control Hierarchy 
</h3>
        <p>
At the core of the control-level event execution order is where the events fire with
respect to the page. The majority of the events in the ASP.NET Page Life Cycle execute
from the top, down, which is also referred to as outside-in. That is, the event is
first executed on the page, such as Page.Load, then executed recursively through each
of the page's controls, Control.Load, to the controls within controls, and so on.
The two exceptions to this rule are Initialization and Unload. With these two events,
the event is fired first on the child control, then on the container control, and
finally on the page, known as a bottom-up or inside-out order.
</p>
        <p>
But what if a control is dynamically added to the page during a later event? In this
case, a control will fire events to catch up to the page (though a control will never
exceed beyond what Page Event is currently executing). In other words, if a control
is dynamically added during the PreInit page event, the control will immediately fire
its own PreInit. However, if a control is dynamically added during the PreLoad event,
it will fire PreInit, Init, InitComplete, and PreLoad, all in quick succession.
</p>
        <pre class="csharp:nocontrols" name="code">private void Page_PreInit(object sender, EventArgs e)
{
    Trace.Write("Executing Page PreInitialization");
    var textbox = new TextBox();
    textbox.Init += Control_Init;
    textbox.Load += Control_Load;
    textbox.ID += "TextBoxFromPreInit";
    form1.Controls.Add(textbox);
}

private void Page_Init(object sender, EventArgs e)
{
    Trace.Write("Executing Page Initialization (Should occur after controls)");
}

private void Page_Load(object sender, EventArgs e)
{
    Trace.Write("Executing Page Load (Should occur before controls)");
    var textbox = new TextBox();
    textbox.Init += Control_Init;
    textbox.Load += Control_Load;
    textbox.ID += "TextBoxFromLoad";
    form1.Controls.Add(textbox);
}

private void Control_Init(object sender, EventArgs e)
{
    Trace.Write("Executing Control Init for " + ((Control)sender).UniqueID);
}

private void Control_Load(object sender, EventArgs e)
{
    Trace.Write("Executing Control Load for " + ((Control)sender).UniqueID);
}

/*
Output: 

Begin PreInit		
Executing Page PreInitialization
End PreInit
Begin Init
Executing Control Init for TextBoxFromPreInit
Executing Page Initialization (Should occur after controls)
End Init
Begin Load
Executing Page Load (Should occur before controls)
Executing Control Init for TextBoxFromLoad
Executing Control Load for TextBoxFromPreInit
Executing Control Load for TextBoxFromLoad
End Load
*/</pre>
        <h3>Event Execution Order for Sibling WebControls
</h3>
        <p>
The event execution order of parent and child controls is simple and straightforward.
As if to maintain balance in The Force, the event execution order for sibling controls
is a bit complicated. For siblings, this order is governed by three main and cascading
criteria: the type of event that is being executed, the Page Event executing when
the control was added to the page, and the index of the control within the parent's
(or page's) Controls collection.
</p>
        <p>
First, the event type is the primary governor of when an event is fired. Just like
the order of Page Events, Initialize events always occur before Load events, and Load
events always occur before Render events. The complication surrounds the several control-specific
"PostBack Events," such as Click or TextChanged, as there are three PostBack
event types: Changed Events, Validation Events, and actual PostBack Events. The first
that fire are Changed Events, which include any event where the value changes, such
as TextBox.TextChanged or DropDownList.SelectedIndexChanged. Changed events should
include any custom Value manipulation for each of your form controls. Once the values
are defined, Validation events are executed to assist with ensuring data integrity.
Finally, once all values are defined and validated, PostBack Events, such as Button.Command
or Button.Click, are executed. In most cases, these PostBack events will include the
form submission logic, such as sending the email, transmitting data through a Web
Service, or saving data to a database. The Changed Events type of events always fire
before Validation events, which always fire before the PostBack Events types; TextBox.TextChanged
before Validator.Validate before Button.Click.
</p>
        <p>
If the events are the same, such as two TextBox controls that are both executing TextChanged,
the second criteria to determine sibling event execution is when the control was added
to the page. If a control was added in any of the Initialization events (PreInit,
Init, InitComplete), it is executed first. If a control was added in any of the Load
events, it is executed second. So, for the two TextBoxes, the TextChanged event for
the TextBox added during Initialization will be fired before the same event for the
TextBox added during Load. (txtAddedDuringInit.TextChanged will fire before txtAddedDuringLoad.TextChanged.)
</p>
        <p>
If the executing event is the same, and the controls were added during the same Page
Event, the final criterion for sibling execution is the index within the Controls
collection. After the above two criteria are considered, events that still have equal
weight are executed according to their index in their parent's Controls collection.
</p>
        <pre class="csharp:nocontrols" name="code">private void Page_Init(object sender, EventArgs e)
{
    TextBox textbox;
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromInit1";
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromInit2";
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromInit3At0";
    form1.Controls.AddAt(0, textbox);
}

private void Page_Load(object sender, EventArgs e)
{
    TextBox textbox;
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromLoad1";
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromLoad2";
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += "TextBoxFromLoad3At0";
    form1.Controls.AddAt(0, textbox);
}

private void Control_TextChanged(object sender, EventArgs e)
{
    Trace.Write("Executing Control TextChanged for " + ((Control) sender).UniqueID
                + " / Position: " + form1.Controls.IndexOf((Control) sender));
}

/*
Trace Output: 

Begin Raise ChangedEvents
Executing Control TextChanged for TextBoxFromInit3At0 / Position: 1
Executing Control TextChanged for TextBoxFromInit1 / Position: 2
Executing Control TextChanged for TextBoxFromInit2 / Position: 3
Executing Control TextChanged for TextBoxFromLoad3At0 / Position: 0
Executing Control TextChanged for TextBoxFromLoad1 / Position: 4
Executing Control TextChanged for TextBoxFromLoad2 / Position: 5
End Raise ChangedEvents
*/</pre>
        <p>
So, to address the questions from above: Page.Load does execute before Control.Load,
as the Load event is executed outside-in, however, Page.Init executes after Control.Init,
as the Init event is executed inside-out. The TextChanged event on myTextBox is fired
prior to myButton.Click, as control ChangedEvents are executed before control PostBackEvents.
And finally, regarding myTextBox1.TextChanged versus myTextBox2.TextChanged, it depends;
the order is dependent upon where the controls exist within the entire hierarchy,
when the controls were created, and upon their position within the Controls collection.
</p>
        <p>
The execution order of control events within the page life cycle is a complicated
mess, and fortunately does not come in to play often. But for when it does, it is
important to know how everything plays together. I find that most often, the order
is important when dynamically adding controls to the page outside of DataBinding (though
I would consider this a design smell), when creating custom WebControls, or when working
with control Changed Events and validation. Still, as with before, committing this
to memory (or at least a link to a reference, such as this post) will help with making
you a better ASP.NET developer and with creating higher quality applications.
</p>
        <p>
So what's next? <a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx">Part
1</a> covered the base ASP.NET Page Life Cycle, and this post covers the execution
order of events on the page. <a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx">As
this series continues</a>, we will discuss the details of the DataBinding events,
and will dig in to some tips, tricks, and traps when developing ASP.NET applications. 
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d359ae8-9825-413f-92dc-2b533314f154" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 2 [WebControl Execution Order]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,1d359ae8-9825-413f-92dc-2b533314f154.aspx</guid>
      <link>http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx</link>
      <pubDate>Tue, 14 Jul 2009 22:26:15 GMT</pubDate>
      <description>&lt;p&gt;
The first installment of this series goes back to the beginning and describes each
of the events within ASP.NET Page Life Cycle. Understanding the basic fundamentals
of the ASP.NET Page Life Cycle, including the order and scope of influence for each
of the Page Life Cycle events, will help ensure that you are executing your custom
code at the right time, and in the right order, rather than stepping on yourself by
conflicting with core ASP.NET framework functionality. But this is only part of the
story, since there is more to the ASP.NET Page Life Cycle than just the page, itself.
&lt;/p&gt;
&lt;h3&gt;ASP.NET Page &amp;amp; WebControl Event Execution Order
&lt;/h3&gt;
&lt;p&gt;
Pages would be nothing but a sea of crazy peach gradient backgrounds without the controls
to display content and to interact with the user. In addition to the order of the
various page events, it is often helpful to know the order in which a page and its
controls execute a single event. Does Page.Load execute before Control.Load? Does
Page.Init execute before Control.Init? Does myTextBox.TextChanged fire before myButton.Click?
And what about myTextBox1.TextChanged versus myTextBox2.TextChanged?
&lt;/p&gt;
&lt;p&gt;
Knowing the execution order of events within the control tree will make you a better
ASP.NET developer. If you cannot answer each of those questions above (and maybe even
if you can), keep reading.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;About the Series
&lt;/h3&gt;
&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;p&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Events
of the ASP.NET Page Life Cycle&lt;/a&gt; 
&lt;br /&gt;
Part 2: ASP.NET Page &amp;amp; WebControl Event Execution Order 
&lt;br /&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx"&gt;Getting
Started with ASP.NET Data Binding&lt;/a&gt; 
&lt;br /&gt;
Part 4: &lt;a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;Wiring
Events to your Page&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;h3&gt;Event Execution Order within the Control Hierarchy 
&lt;/h3&gt;
&lt;p&gt;
At the core of the control-level event execution order is where the events fire with
respect to the page. The majority of the events in the ASP.NET Page Life Cycle execute
from the top, down, which is also referred to as outside-in. That is, the event is
first executed on the page, such as Page.Load, then executed recursively through each
of the page's controls, Control.Load, to the controls within controls, and so on.
The two exceptions to this rule are Initialization and Unload. With these two events,
the event is fired first on the child control, then on the container control, and
finally on the page, known as a bottom-up or inside-out order.
&lt;/p&gt;
&lt;p&gt;
But what if a control is dynamically added to the page during a later event? In this
case, a control will fire events to catch up to the page (though a control will never
exceed beyond what Page Event is currently executing). In other words, if a control
is dynamically added during the PreInit page event, the control will immediately fire
its own PreInit. However, if a control is dynamically added during the PreLoad event,
it will fire PreInit, Init, InitComplete, and PreLoad, all in quick succession.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;private void Page_PreInit(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Page PreInitialization&amp;quot;);
    var textbox = new TextBox();
    textbox.Init += Control_Init;
    textbox.Load += Control_Load;
    textbox.ID += &amp;quot;TextBoxFromPreInit&amp;quot;;
    form1.Controls.Add(textbox);
}

private void Page_Init(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Page Initialization (Should occur after controls)&amp;quot;);
}

private void Page_Load(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Page Load (Should occur before controls)&amp;quot;);
    var textbox = new TextBox();
    textbox.Init += Control_Init;
    textbox.Load += Control_Load;
    textbox.ID += &amp;quot;TextBoxFromLoad&amp;quot;;
    form1.Controls.Add(textbox);
}

private void Control_Init(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Control Init for &amp;quot; + ((Control)sender).UniqueID);
}

private void Control_Load(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Control Load for &amp;quot; + ((Control)sender).UniqueID);
}

/*
Output: 

Begin PreInit		
Executing Page PreInitialization
End PreInit
Begin Init
Executing Control Init for TextBoxFromPreInit
Executing Page Initialization (Should occur after controls)
End Init
Begin Load
Executing Page Load (Should occur before controls)
Executing Control Init for TextBoxFromLoad
Executing Control Load for TextBoxFromPreInit
Executing Control Load for TextBoxFromLoad
End Load
*/&lt;/pre&gt;
&lt;h3&gt;Event Execution Order for Sibling WebControls
&lt;/h3&gt;
&lt;p&gt;
The event execution order of parent and child controls is simple and straightforward.
As if to maintain balance in The Force, the event execution order for sibling controls
is a bit complicated. For siblings, this order is governed by three main and cascading
criteria: the type of event that is being executed, the Page Event executing when
the control was added to the page, and the index of the control within the parent's
(or page's) Controls collection.
&lt;/p&gt;
&lt;p&gt;
First, the event type is the primary governor of when an event is fired. Just like
the order of Page Events, Initialize events always occur before Load events, and Load
events always occur before Render events. The complication surrounds the several control-specific
&amp;quot;PostBack Events,&amp;quot; such as Click or TextChanged, as there are three PostBack
event types: Changed Events, Validation Events, and actual PostBack Events. The first
that fire are Changed Events, which include any event where the value changes, such
as TextBox.TextChanged or DropDownList.SelectedIndexChanged. Changed events should
include any custom Value manipulation for each of your form controls. Once the values
are defined, Validation events are executed to assist with ensuring data integrity.
Finally, once all values are defined and validated, PostBack Events, such as Button.Command
or Button.Click, are executed. In most cases, these PostBack events will include the
form submission logic, such as sending the email, transmitting data through a Web
Service, or saving data to a database. The Changed Events type of events always fire
before Validation events, which always fire before the PostBack Events types; TextBox.TextChanged
before Validator.Validate before Button.Click.
&lt;/p&gt;
&lt;p&gt;
If the events are the same, such as two TextBox controls that are both executing TextChanged,
the second criteria to determine sibling event execution is when the control was added
to the page. If a control was added in any of the Initialization events (PreInit,
Init, InitComplete), it is executed first. If a control was added in any of the Load
events, it is executed second. So, for the two TextBoxes, the TextChanged event for
the TextBox added during Initialization will be fired before the same event for the
TextBox added during Load. (txtAddedDuringInit.TextChanged will fire before txtAddedDuringLoad.TextChanged.)
&lt;/p&gt;
&lt;p&gt;
If the executing event is the same, and the controls were added during the same Page
Event, the final criterion for sibling execution is the index within the Controls
collection. After the above two criteria are considered, events that still have equal
weight are executed according to their index in their parent's Controls collection.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;private void Page_Init(object sender, EventArgs e)
{
    TextBox textbox;
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromInit1&amp;quot;;
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromInit2&amp;quot;;
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromInit3At0&amp;quot;;
    form1.Controls.AddAt(0, textbox);
}

private void Page_Load(object sender, EventArgs e)
{
    TextBox textbox;
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromLoad1&amp;quot;;
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromLoad2&amp;quot;;
    form1.Controls.Add(textbox);
    textbox = new TextBox();
    textbox.TextChanged += Control_TextChanged;
    textbox.ID += &amp;quot;TextBoxFromLoad3At0&amp;quot;;
    form1.Controls.AddAt(0, textbox);
}

private void Control_TextChanged(object sender, EventArgs e)
{
    Trace.Write(&amp;quot;Executing Control TextChanged for &amp;quot; + ((Control) sender).UniqueID
                + &amp;quot; / Position: &amp;quot; + form1.Controls.IndexOf((Control) sender));
}

/*
Trace Output: 

Begin Raise ChangedEvents
Executing Control TextChanged for TextBoxFromInit3At0 / Position: 1
Executing Control TextChanged for TextBoxFromInit1 / Position: 2
Executing Control TextChanged for TextBoxFromInit2 / Position: 3
Executing Control TextChanged for TextBoxFromLoad3At0 / Position: 0
Executing Control TextChanged for TextBoxFromLoad1 / Position: 4
Executing Control TextChanged for TextBoxFromLoad2 / Position: 5
End Raise ChangedEvents
*/&lt;/pre&gt;
&lt;p&gt;
So, to address the questions from above: Page.Load does execute before Control.Load,
as the Load event is executed outside-in, however, Page.Init executes after Control.Init,
as the Init event is executed inside-out. The TextChanged event on myTextBox is fired
prior to myButton.Click, as control ChangedEvents are executed before control PostBackEvents.
And finally, regarding myTextBox1.TextChanged versus myTextBox2.TextChanged, it depends;
the order is dependent upon where the controls exist within the entire hierarchy,
when the controls were created, and upon their position within the Controls collection.
&lt;/p&gt;
&lt;p&gt;
The execution order of control events within the page life cycle is a complicated
mess, and fortunately does not come in to play often. But for when it does, it is
important to know how everything plays together. I find that most often, the order
is important when dynamically adding controls to the page outside of DataBinding (though
I would consider this a design smell), when creating custom WebControls, or when working
with control Changed Events and validation. Still, as with before, committing this
to memory (or at least a link to a reference, such as this post) will help with making
you a better ASP.NET developer and with creating higher quality applications.
&lt;/p&gt;
&lt;p&gt;
So what's next? &lt;a title="Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]" href="http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx"&gt;Part
1&lt;/a&gt; covered the base ASP.NET Page Life Cycle, and this post covers the execution
order of events on the page. &lt;a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx"&gt;As
this series continues&lt;/a&gt;, we will discuss the details of the DataBinding events,
and will dig in to some tips, tricks, and traps when developing ASP.NET applications. 
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8f035b16-2463-44d0-b6aa-1c67e951fb4c" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=1d359ae8-9825-413f-92dc-2b533314f154" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,1d359ae8-9825-413f-92dc-2b533314f154.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=d08d861c-a8fb-487e-9ead-0f64b80640ff</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,d08d861c-a8fb-487e-9ead-0f64b80640ff.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,d08d861c-a8fb-487e-9ead-0f64b80640ff.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=d08d861c-a8fb-487e-9ead-0f64b80640ff</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a "Hello, World!"
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
</p>
        <blockquote>
          <h3>Posts in this Series
</h3>
          <p>
Part 1: Events of the ASP.NET Page Life Cycle 
<br />
Part 2: <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">ASP.NET
Page &amp; WebControl Event Execution Order</a><br />
Part 3: <a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx">Getting
Started with ASP.NET Data Binding</a><br />
Part 4: <a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx">Wiring
Events to your Page</a></p>
        </blockquote>
        <p>
A little help on the Page Life Cycle is never a bad thing. In this series, I will
go over the events that make up the ASP.NET Page Life Cycle, as well as some tips
and tricks on how to get the most out of this event structure while avoiding the traps
and pitfalls. Rather than pursuing broad coverage of the entire ASP.NET Framework,
we'll dive deeply into the "small" portion that is the ASP.NET Page Life
Cycle.
</p>
        <h3>Events of the ASP.NET Page Life Cycle
</h3>
        <p>
I want to start at the beginning. The primary make-up of the Page Life Cycle is the
events that process any ASP.NET requests. Unlike the public static void main of a
WinForms application, where everything based on methods, the execution of a page request
is the execution of these events. These events, which execute in a particular order,
handle the entire request, including loading all of the controls, processing all of
the form data, handling all user-initiated actions, and rendering the page to the
web browser. Knowing the order in which these events are executed, as well as the
responsibility of each event in processing your request, is important for developing
solid, quality ASP.NET applications.
</p>
        <h4>Start
</h4>
        <p>
This is where the page object is instantiated, and where the initial properties of
the page are set. Page properties such as Response and Request, UICulture (similar
to the UICulture property within a WinForms thread), and the value of IsPostBack are
all determined and assigned. No controls are available at this time, so do not try
to set the value of that TextBox control, as it doesn't exist, yet. Fortunately, no
event handlers can be attached to this event, anyway, so there isn't much you can
do to customize this processing or to access that TextBox's value property; "Move
along. There is nothing to see here." But, be aware that this event does occur
after the Constructor, so if you try to access properties such as IsPostBack prior
to the Start event, they have yet to be assigned, and will likely be incorrect.
</p>
        <h4>Page Initialization
</h4>
        <p>
During page initialization, the controls are created, initialized, and added to the
Page's controls collection. This is the first time that you can access a control by
its UniqueID. Do note that all control properties are set to their code values, be
it from code-behind or code-in-front, regardless of what may be available in ViewState
and Form Post values. Control state has yet to be restored, so ViewState and Form
Post values have not yet been pushed to the controls. Finally, Initialization (specifically,
PreInit) is the only time that the Theme and Master Page can be programmatically modified.
</p>
        <h4>Page Load
</h4>
        <p>
Page Load is where control state is restored. If the request is a PostBack, rather
than a new request, all available property values are restored from ViewState and
Form Post data and pushed to the applicable controls. Under most scenarios, this is
where you're going to get what you need from the Database, such as pulling a value
from the query string and loading an item with the matching identity.
</p>
        <h4>Validation
</h4>
        <p>
The Validation event only applies to PostBack requests, and only when Validators are
present in the control collection. The Validate method is executed for each Validator
present, through which the IsValid property is set for each Validator. These IsValid
property values are then cascaded up to the Page's IsValid property. Be aware that
even if all Validators on the page are disabled, the Validation event will still fire;
if a Validator is present, Validate is executed, without regard to any other property.
Also, note that the Validation event is a child of the Page's Load event, so it is
executed within the Page Load event chain, after Page Load, but prior to PostBack
Events and LoadComplete.
</p>
        <h4>PostBack Events
</h4>
        <p>
Once Validation is complete (if applicable), all PostBack events are executed, including
the OnChange event of a DropDownList and the OnClick event of a command button. Post
Back Events are also a child of the Page's Load event, executing after Validation
and before LoadComplete.
</p>
        <h4>Render
</h4>
        <p>
Finally, once all of the data is processed and Post Back events handled, the Page
is rendered within the Web Browser. The Render event consists of saving all control
property data to ViewState, processing the Page and each Control into HTML, and writing
the HTML to the output stream. This is the last opportunity to modify the HTML output.
</p>
        <blockquote>
          <h4>Remembering the Order
</h4>
          <p>
If you are having trouble remembering the order, instead try and remember this simple
mnemonic: <strong>SILVER</strong>; Start, Initialize, Load, Validation, Events, Render.
</p>
        </blockquote>
        <p>
If you are doing a lot of ASP.NET programming, or anticipate that you will be in the
near future, try to commit to memory the order of each of these events, and their
scope of influence. Understanding these basic fundamentals of the ASP.NET Page Life
Cycle will help ensure that you are executing your custom code at the right time,
and in the right order, rather than stepping on yourself by conflicting with the core
functionality.
</p>
        <p>
Now that we know the order of execution on Page Events, what is the order of the Controls?
Does Page.Load execute before Control.Load? How about the order of sibling controls?
What is the order of myTextBox1.TextChanged versus myTextBox2.TextChanged? Also, what
are some things to look out for? <a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx">As
this series continues</a>, we will discuss the details of event execution order within
the ASP.NET Page Life Cycle, as well as some tips, trick, and traps when developing
ASP.NET applications.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f31df0e8-d581-43ef-94f3-a3ebdce80ce6" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag">Page
Life Cycle</a>,<a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag">Event
Life Cycle</a>,<a href="http://technorati.com/tags/Dev+Basics" rel="tag">Dev Basics</a>,<a href="http://technorati.com/tags/Back+to+Basics" rel="tag">Back
to Basics</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=d08d861c-a8fb-487e-9ead-0f64b80640ff" />
      </body>
      <title>Dev Basics: ASP.NET Page Life Cycle, Part 1 [Events]</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,d08d861c-a8fb-487e-9ead-0f64b80640ff.aspx</guid>
      <link>http://www.cptloadtest.com/2009/06/23/Dev-Basics-ASPNET-Page-Life-Cycle-Part-1-Events.aspx</link>
      <pubDate>Tue, 23 Jun 2009 03:53:57 GMT</pubDate>
      <description>&lt;p&gt;
When a request occurs for an ASP.NET page, the response is processed through a series
of events before being sent to the client browser. These events, known as the ASP.NET
Page Life Cycle, are a complicated headache when used improperly, manifesting as odd
exceptions, incorrect data, performance issues, and general confusion. It seems simple
when reading yet-another-book-on-ASP.NET, but never when applied in the real world.
What is covered in a few short pages in many ASP.NET books (and sometimes even just
a few short paragraphs), is much more complicated outside of a &amp;quot;Hello, World!&amp;quot;
application and inside of the complex demands of the enterprise applications that
developers create and maintain in their day-to-day work life. As close to the core
as the life cycle is to any ASP.NET web application, the complications and catches
behind this system never seems to get wide coverage on study guides or other documentation.
But, they should.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;Posts in this Series
&lt;/h3&gt;
&lt;p&gt;
Part 1: Events of the ASP.NET Page Life Cycle 
&lt;br /&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;ASP.NET
Page &amp;amp; WebControl Event Execution Order&lt;/a&gt; 
&lt;br /&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2010/03/24/Dev-Basics-ASPNET-Page-Life-Cycle-Part-3-Data-Binding.aspx"&gt;Getting
Started with ASP.NET Data Binding&lt;/a&gt; 
&lt;br /&gt;
Part 4: &lt;a href="http://www.cptloadtest.com/2011/04/26/Dev-Basics-ASPNET-Page-Life-Cycle-Part-4-Event-Wireup.aspx"&gt;Wiring
Events to your Page&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
A little help on the Page Life Cycle is never a bad thing. In this series, I will
go over the events that make up the ASP.NET Page Life Cycle, as well as some tips
and tricks on how to get the most out of this event structure while avoiding the traps
and pitfalls. Rather than pursuing broad coverage of the entire ASP.NET Framework,
we'll dive deeply into the &amp;quot;small&amp;quot; portion that is the ASP.NET Page Life
Cycle.
&lt;/p&gt;
&lt;h3&gt;Events of the ASP.NET Page Life Cycle
&lt;/h3&gt;
&lt;p&gt;
I want to start at the beginning. The primary make-up of the Page Life Cycle is the
events that process any ASP.NET requests. Unlike the public static void main of a
WinForms application, where everything based on methods, the execution of a page request
is the execution of these events. These events, which execute in a particular order,
handle the entire request, including loading all of the controls, processing all of
the form data, handling all user-initiated actions, and rendering the page to the
web browser. Knowing the order in which these events are executed, as well as the
responsibility of each event in processing your request, is important for developing
solid, quality ASP.NET applications.
&lt;/p&gt;
&lt;h4&gt;Start
&lt;/h4&gt;
&lt;p&gt;
This is where the page object is instantiated, and where the initial properties of
the page are set. Page properties such as Response and Request, UICulture (similar
to the UICulture property within a WinForms thread), and the value of IsPostBack are
all determined and assigned. No controls are available at this time, so do not try
to set the value of that TextBox control, as it doesn't exist, yet. Fortunately, no
event handlers can be attached to this event, anyway, so there isn't much you can
do to customize this processing or to access that TextBox's value property; &amp;quot;Move
along. There is nothing to see here.&amp;quot; But, be aware that this event does occur
after the Constructor, so if you try to access properties such as IsPostBack prior
to the Start event, they have yet to be assigned, and will likely be incorrect.
&lt;/p&gt;
&lt;h4&gt;Page Initialization
&lt;/h4&gt;
&lt;p&gt;
During page initialization, the controls are created, initialized, and added to the
Page's controls collection. This is the first time that you can access a control by
its UniqueID. Do note that all control properties are set to their code values, be
it from code-behind or code-in-front, regardless of what may be available in ViewState
and Form Post values. Control state has yet to be restored, so ViewState and Form
Post values have not yet been pushed to the controls. Finally, Initialization (specifically,
PreInit) is the only time that the Theme and Master Page can be programmatically modified.
&lt;/p&gt;
&lt;h4&gt;Page Load
&lt;/h4&gt;
&lt;p&gt;
Page Load is where control state is restored. If the request is a PostBack, rather
than a new request, all available property values are restored from ViewState and
Form Post data and pushed to the applicable controls. Under most scenarios, this is
where you're going to get what you need from the Database, such as pulling a value
from the query string and loading an item with the matching identity.
&lt;/p&gt;
&lt;h4&gt;Validation
&lt;/h4&gt;
&lt;p&gt;
The Validation event only applies to PostBack requests, and only when Validators are
present in the control collection. The Validate method is executed for each Validator
present, through which the IsValid property is set for each Validator. These IsValid
property values are then cascaded up to the Page's IsValid property. Be aware that
even if all Validators on the page are disabled, the Validation event will still fire;
if a Validator is present, Validate is executed, without regard to any other property.
Also, note that the Validation event is a child of the Page's Load event, so it is
executed within the Page Load event chain, after Page Load, but prior to PostBack
Events and LoadComplete.
&lt;/p&gt;
&lt;h4&gt;PostBack Events
&lt;/h4&gt;
&lt;p&gt;
Once Validation is complete (if applicable), all PostBack events are executed, including
the OnChange event of a DropDownList and the OnClick event of a command button. Post
Back Events are also a child of the Page's Load event, executing after Validation
and before LoadComplete.
&lt;/p&gt;
&lt;h4&gt;Render
&lt;/h4&gt;
&lt;p&gt;
Finally, once all of the data is processed and Post Back events handled, the Page
is rendered within the Web Browser. The Render event consists of saving all control
property data to ViewState, processing the Page and each Control into HTML, and writing
the HTML to the output stream. This is the last opportunity to modify the HTML output.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h4&gt;Remembering the Order
&lt;/h4&gt;
&lt;p&gt;
If you are having trouble remembering the order, instead try and remember this simple
mnemonic: &lt;strong&gt;SILVER&lt;/strong&gt;; Start, Initialize, Load, Validation, Events, Render.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
If you are doing a lot of ASP.NET programming, or anticipate that you will be in the
near future, try to commit to memory the order of each of these events, and their
scope of influence. Understanding these basic fundamentals of the ASP.NET Page Life
Cycle will help ensure that you are executing your custom code at the right time,
and in the right order, rather than stepping on yourself by conflicting with the core
functionality.
&lt;/p&gt;
&lt;p&gt;
Now that we know the order of execution on Page Events, what is the order of the Controls?
Does Page.Load execute before Control.Load? How about the order of sibling controls?
What is the order of myTextBox1.TextChanged versus myTextBox2.TextChanged? Also, what
are some things to look out for? &lt;a href="http://www.cptloadtest.com/2009/07/14/Dev-Basics-ASPNET-Page-Life-Cycle-Part-2-WebControl-Execution-Order.aspx"&gt;As
this series continues&lt;/a&gt;, we will discuss the details of event execution order within
the ASP.NET Page Life Cycle, as well as some tips, trick, and traps when developing
ASP.NET applications.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f31df0e8-d581-43ef-94f3-a3ebdce80ce6" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page+Life+Cycle" rel="tag"&gt;Page
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Event+Life+Cycle" rel="tag"&gt;Event
Life Cycle&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Dev+Basics" rel="tag"&gt;Dev Basics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Back+to+Basics" rel="tag"&gt;Back
to Basics&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=d08d861c-a8fb-487e-9ead-0f64b80640ff" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,d08d861c-a8fb-487e-9ead-0f64b80640ff.aspx</comments>
      <category>ASP.Net</category>
      <category>Dev Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Next month, I will be speaking at <a href="http://www.codestock.org/">CodeStock</a>,
a developer conference in Knoxville, Tennessee, held June 26-27. We will be discussing
the ASP.NET Page Life Cycle, to help get over the fears and troubles with validation,
event handing, data binding, and the conflicts between page load and page initialization.
</p>
        <blockquote>
          <h3>Dev Basics: The ASP.NET Page Life Cycle
</h3>
          <p>
            <em>Jay Harris / Session Level: 100 
<br /></em>When a request occurs for an ASP.NET page, the response is processed through
a series of events before being sent to the client browser. These events, known as
the Page Life Cycle, are a complicated headache when used improperly, manifesting
as odd exceptions, incorrect data, performance issues, and general confusion. It seems
simple when reading yet-another-book-on-ASP.NET, but never when applied in the real
world. In this session, we decompose this mess, and turn the Life Cycle into an effective
and productive tool. No ASP.NET MVC, no Dynamic Data, no MonoRail, no technologies
of tomorrow, just the basics of ASP.NET, using the tools we have available in the
office, today.
</p>
        </blockquote>
        <p>
It's a long drive from Michigan to Knoxville, but the conference is worth the trip
(the first of two Tennessee conferences I will be attending this year). A few other
local speakers will be making the trip to Knoxville, as well. Check out the <a title="CodeStock Session List" href="http://www.codestock.org/Sessions.aspx">full
session list</a> for more information, and while you are at it, <a title="Register for CodeStock" href="http://www.codestock.org/Pages/Register.aspx">register
for the event</a> if you haven't already done so; the cost is only $25 if you sign
up before the end of May. I was there last year for the first CodeStock, and I had
a great time; I'm excited about this years event, not only because I am speaking,
but to see what other new things that people are talking about, catch up with friends,
and to meet new people in the community.
</p>
        <p>
I hope to see you there.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:79086fe6-58e7-4b4a-9394-194cfd2007b6" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/CodeStock" rel="tag">CodeStock</a>,<a href="http://technorati.com/tags/Speaking" rel="tag">Speaking</a>,<a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8" />
      </body>
      <title>Speaking at CodeStock 2009 on ASP.NET Page Life Cycle</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8.aspx</guid>
      <link>http://www.cptloadtest.com/2009/05/18/Speaking-At-CodeStock-2009-On-ASPNET-Page-Life-Cycle.aspx</link>
      <pubDate>Mon, 18 May 2009 13:27:01 GMT</pubDate>
      <description>&lt;p&gt;
Next month, I will be speaking at &lt;a href="http://www.codestock.org/"&gt;CodeStock&lt;/a&gt;,
a developer conference in Knoxville, Tennessee, held June 26-27. We will be discussing
the ASP.NET Page Life Cycle, to help get over the fears and troubles with validation,
event handing, data binding, and the conflicts between page load and page initialization.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;h3&gt;Dev Basics: The ASP.NET Page Life Cycle
&lt;/h3&gt;
&lt;p&gt;
&lt;em&gt;Jay Harris / Session Level: 100 
&lt;br /&gt;
&lt;/em&gt;When a request occurs for an ASP.NET page, the response is processed through
a series of events before being sent to the client browser. These events, known as
the Page Life Cycle, are a complicated headache when used improperly, manifesting
as odd exceptions, incorrect data, performance issues, and general confusion. It seems
simple when reading yet-another-book-on-ASP.NET, but never when applied in the real
world. In this session, we decompose this mess, and turn the Life Cycle into an effective
and productive tool. No ASP.NET MVC, no Dynamic Data, no MonoRail, no technologies
of tomorrow, just the basics of ASP.NET, using the tools we have available in the
office, today.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
It's a long drive from Michigan to Knoxville, but the conference is worth the trip
(the first of two Tennessee conferences I will be attending this year). A few other
local speakers will be making the trip to Knoxville, as well. Check out the &lt;a title="CodeStock Session List" href="http://www.codestock.org/Sessions.aspx"&gt;full
session list&lt;/a&gt; for more information, and while you are at it, &lt;a title="Register for CodeStock" href="http://www.codestock.org/Pages/Register.aspx"&gt;register
for the event&lt;/a&gt; if you haven't already done so; the cost is only $25 if you sign
up before the end of May. I was there last year for the first CodeStock, and I had
a great time; I'm excited about this years event, not only because I am speaking,
but to see what other new things that people are talking about, catch up with friends,
and to meet new people in the community.
&lt;/p&gt;
&lt;p&gt;
I hope to see you there.
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:79086fe6-58e7-4b4a-9394-194cfd2007b6" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/CodeStock" rel="tag"&gt;CodeStock&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Speaking" rel="tag"&gt;Speaking&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,e8d18cc4-127b-4fe1-a86c-f8cd56b3edb8.aspx</comments>
      <category>ASP.Net</category>
      <category>Events</category>
      <category>Speaking</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=71ba8b42-6bcc-49e3-9f2f-48de47d8f275</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,71ba8b42-6bcc-49e3-9f2f-48de47d8f275.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,71ba8b42-6bcc-49e3-9f2f-48de47d8f275.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=71ba8b42-6bcc-49e3-9f2f-48de47d8f275</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Did you know that <em>yourdomain.com</em> and <em>www.yourdomain.com</em> are actually
different sites? Are they both serving the same content? If so, it may be negatively
impacting your search engine rankings.
</p>
        <h3>Subdomains and the Synonymous 'WWW'
</h3>
        <p>
Sub-domains are the prefix to a domain (<em>http://subdomain.yourdomain.com</em>),
and are treated by browsers, computers, domain name systems (DNS), search engines,
and the general internet as separate, individual web sites. Google's primary web presence, <a href="http://www.google.com">http://www.google.com</a>,
is very different than Google Mail, <a href="http://mail.google.com">http://mail.google.com</a>,
or Google Documents, <a href="http://docs.google.com">http://docs.google.com</a>,
all because of subdomains. However, what many do not realize is that <em>www</em> is,
itself, a subdomain.
</p>
        <p>
A domain, on its own, requires no <em>www</em> prefix; a subdomain-less <em>http://yourdomain.com</em> should
be sufficient for serving up a web site. And since <em>www</em> is a subdomain, dropping
the prefix could potentially return a different response. There are some sites that
will fail to return without the prefix, and some sites that fail with it, but the
most common practice is that the <em>www</em> subdomain is synonymous for no subdomain
at all.
</p>
        <h3>The Synonymous WWW and SEO
</h3>
        <p>
The issue with having two synonymous URLs (<em>http://yourdomain.com</em> and <em>http://www.yourdomain.com</em>)
is that search engines may interpret them as separate sites, even if they are serving
the same content. The two addresses are technically independent and are potentially
serving unique content; to a cautious search engine, even if pages appear to contain
the same content, there may be something different under the covers. This means your
audience's search results returns two entries for the same content. Some users will
happen to click on <em>yourdomain.com</em> while others navigate to <em>www.yourdomain.com</em>,
splitting your traffic, your page hits, your search ranking between two sites, unnecessarily.
</p>
        <p>
HTTP Redirects will cure the issue. If you access <a href="http://google.com">http://google.com</a>,
your browser is instantly redirected to <a href="http://www.google.com">http://www.google.com</a>.
This is done through a HTTP 301 permanent redirect. Search Spiders recognize HTTP
response codes, and understand the 301 as a "use this other URL instead" command.
Many search engines, such as Google, will then update all page entries for the original
URI (<em>http://yourdomain.com</em>) and replace it with the 301's destination URL
(<em>http://www.yourdomain.com</em>). If there is already an entry for the destination
URL, the two entries will be merged together. The search entries for <em>yourdomain.com</em> and <em>www.yourdomain.com</em> will
now share traffic, share page hits, and share search ranking. Instead of having two
entries on the second and third pages of search results, combining these entries may
be just enough to place you on the first page of results.
</p>
        <blockquote>In addition to combining search entries for subdomains, you can also combine
root-level domains through HTTP 301. On this site, in addition to adding the <em>www</em> prefix
if no subdomain is specified, <a href="http://captainloadtest.com">captainloadtest.com</a> will
HTTP 301 redirect to <a href="http://www.cptloadtest.com">www.cptloadtest.com</a>.</blockquote>
        <h3>Combining the Synonyms
</h3>
        <p>
We need a way to implement an HTTP 301 redirect at the domain level for all requests
to a site; however, often we are using applications that may not grant us access to
the source, or we don't have the access into IIS through our host to set up redirects
for ourselves. <a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx">URL
Rewrite, Part 2</a> covers a great <a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx">drop-in
redirect module</a> by <a href="http://www.pluralsight.com/community/blogs/fritz/">Fritz
Onion</a> that uses a stand-alone assembly with a few additions in web.config to HTTP
301 redirect paths in your domain (it also supports HTTP 302 redirects). This module
is perfect for converting a WordPress blog post URL, such as <a href="http://www.cptloadtest.com/?p=56">cptloadtest.com/?p=56</a>,
to a DasBlog blog post URL like <a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx">cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx</a>.
However, to redirect domains and subdomains, the module must go a step further and
redirect based on matches against the entire URL, such as directing http:// to https://
or captainloadtest.com to cptloadtest.com, which it does not support. It's time for
some modifications.
</p>
        <pre class="csharp:nocontrols:linenumber[20]" name="code">private void OnBeginRequest(object src, EventArgs e) {
  HttpApplication app = src as HttpApplication;
  string reqUrl = app.Request.Url.AbsoluteUri;
  redirections redirs
    = (redirections) ConfigurationManager.GetSection("redirections");

  foreach (Add a in redirs.Adds) {
    Regex regex = new Regex(a.targetUrl, RegexOptions.IgnoreCase);
    if (regex.IsMatch(reqUrl)) {
      string targetUrl = regex.Replace(reqUrl, a.destinationUrl, 1);

      if (a.permanent) {
        app.Response.StatusCode = 301; // make a permanent redirect
        app.Response.AddHeader("Location", targetUrl);
        app.Response.End();
      }
      else
        app.Response.Redirect(targetUrl);

      break;
    }    
  }
}</pre>
        <p>
By converting <em>app.Request.RawURL</em> to <em>app.Request.AbsoluteUri</em>, the
regular expression will now match against the entire URL, rather than just the requested
path. There is one downside to this change: the value is the actual path processed,
not necessarily what was in the originally requested URL. To this effect, the value
of <em>AbsoluteUri</em> for requesting <a href="http://www.cptloadtest.com?p=56">http://www.cptloadtest.com?p=56</a> is
actually <a href="http://www.cptloadtest.com/default.aspx?p=56">http://www.cptloadtest.com/default.aspx?p=56</a>;
by requesting the root directory, the default page is being processed, not the directory
itself, so <em>default.aspx</em> is added to the URL. Keep this in mind when setting
up your redirection rules. Also, the original code converted the URL to lower case;
with my modifications, I chose to maintain the case of the URL, since sometimes case
matters, and instead ignore case in the regular expression match using <em>RegexOptions.IgnoreCase</em>.
Finally, I made some other minor enhancements, like using the ConfigurationManager,
since ConfigurationSettings is now obsolete, and reusing the matching Regex instance
for replacements.
</p>
        <blockquote>
          <p>
            <strong>Download:</strong>
            <a href="http://www.cptloadtest.com/content/binary/RedirectModule.zip">RedirectModule.zip</a>
          </p>
          <p>
Includes:
</p>
          <ul>
            <li>
Source code for the drop-in Redirect Module 
</li>
            <li>
Sample web.config that uses the module 
</li>
            <li>
Compiled version of redirectmodule.dll</li>
          </ul>
          <p>
The code is based on the original <a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx">Redirect
Module</a> by <a href="http://www.pluralsight.com/community/blogs/fritz/default.aspx">Fritz
Onion</a> and the <a href="http://alt.pluralsight.com/wiki/default.aspx/Craig/XmlSerializerSectionHandler.html">Xml
Serializer Section Handler</a> by <a href="http://www.pluralsight.com/community/blogs/craig/default.aspx">Craig
Andera</a>. As always, this code is provided with no warranties or guarantees. Use
at your own risk. Your mileage may vary. Thanks to Fritz Onion for the original work,
and allowing me extend his code further.
</p>
        </blockquote>
        <p>
The usage is the same as Fritz Onion's original module. Drop the assembly into your
site's bin, and place a few lines into the web.config. The example below contains
the rules as they would apply to this site, 301 redirecting <a href="http://www.captainloadtest.com">http://www.captainloadtest.com</a> to <a href="http://www.cptloadtest.com">http://www.cptloadtest.com</a>,
and adding the <em>www</em> subdomain to any domain requests that have no subdomain.
</p>
        <pre class="xml:nocontrols:linenumber[20]" name="code">&lt;?xml version="1.0"?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;section name="redirections"
      type="Pluralsight.Website.XmlSerializerSectionHandler, redirectmodule" /&gt;
  &lt;/configSections&gt;
  &lt;!-- Redirect Rules --&gt;
  &lt;redirections type="Pluralsight.Website.redirections, redirectmodule"&gt;
    &lt;!-- Domain Redirects //--&gt;
    &lt;add targetUrl="captainloadtest\.com/Default\.aspx"
      destinationUrl="cptloadtest.com/" permanent="true" /&gt;
    &lt;add targetUrl="captainloadtest\.com"
      destinationUrl="cptloadtest.com" permanent="true" /&gt;

    &lt;!-- Add 'WWW' to the domain request //--&gt;
    &lt;add targetUrl="://cptloadtest\.com/Default\.aspx"
      destinationUrl="://www.$1.com/" permanent="true" /&gt;
    &lt;add targetUrl="://cptloadtest\.com"
      destinationUrl="://www.$1.com" permanent="true" /&gt;

    &lt;!-- ...More Redirects --&gt;
  &lt;/redirections&gt;
  &lt;system.web&gt;
    &lt;httpModules&gt;
      &lt;add name="RedirectModule"
        type="Pluralsight.Website.RedirectModule, redirectmodule" /&gt;
    &lt;/httpModules&gt;
  &lt;/system.web&gt;
&lt;/configuration&gt;</pre>
        <p>
The component is easy to use, and can redirect your site traffic to any URL you choose.
Neither code changes to the application nor configuration changes to IIS are needed.
By using this module to combine synonymous versions of your URLs, such as alternate
domains or subdomains, you will improve your page ranking through combining duplicate
search result entries. One more step towards your own search engine optimization goals.
</p>
        <h3>URL Rewrite
</h3>
        <ul>
          <li>
Part 1: <a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx">HTTP
302, Temporarily Relocated</a></li>
          <li>
Part 2: <a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx">HTTP
301, Moved Permanently</a></li>
          <li>
Part 3: Improving SEO and the 'www' subdomain</li>
        </ul>
        <div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:796ee61e-98c9-4ab1-8aef-6f3caa04ea6e" style="margin: 0px; padding: 0px; display: inline;">Technorati
Tags: <a href="http://technorati.com/tags/SEO" rel="tag">SEO</a>,<a href="http://technorati.com/tags/Search%20Engine%20Optimization" rel="tag">Search
Engine Optimization</a>,<a href="http://technorati.com/tags/Page%20Rank" rel="tag">Page
Rank</a>,<a href="http://technorati.com/tags/HTTP%20301" rel="tag">HTTP 301</a>,<a href="http://technorati.com/tags/ASP.NET" rel="tag">ASP.NET</a>,<a href="http://technorati.com/tags/Fritz%20Onion" rel="tag">Fritz
Onion</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=71ba8b42-6bcc-49e3-9f2f-48de47d8f275" />
      </body>
      <title>URL Rewrite, Part 3: Improving SEO and the 'www' subdomain</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,71ba8b42-6bcc-49e3-9f2f-48de47d8f275.aspx</guid>
      <link>http://www.cptloadtest.com/2008/12/04/URL-Rewrite-Part-3-Improving-SEO-And-The-Www-Subdomain.aspx</link>
      <pubDate>Thu, 04 Dec 2008 21:43:10 GMT</pubDate>
      <description>&lt;p&gt;
Did you know that &lt;em&gt;yourdomain.com&lt;/em&gt; and &lt;em&gt;www.yourdomain.com&lt;/em&gt; are actually
different sites? Are they both serving the same content? If so, it may be negatively
impacting your search engine rankings.
&lt;/p&gt;
&lt;h3&gt;Subdomains and the Synonymous 'WWW'
&lt;/h3&gt;
&lt;p&gt;
Sub-domains are the prefix to a domain (&lt;em&gt;http://subdomain.yourdomain.com&lt;/em&gt;),
and are treated by browsers, computers, domain name systems (DNS), search engines,
and the general internet as separate, individual web sites. Google's primary web presence, &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;,
is very different than Google Mail, &lt;a href="http://mail.google.com"&gt;http://mail.google.com&lt;/a&gt;,
or Google Documents, &lt;a href="http://docs.google.com"&gt;http://docs.google.com&lt;/a&gt;,
all because of subdomains. However, what many do not realize is that &lt;em&gt;www&lt;/em&gt; is,
itself, a subdomain.
&lt;/p&gt;
&lt;p&gt;
A domain, on its own, requires no &lt;em&gt;www&lt;/em&gt; prefix; a subdomain-less &lt;em&gt;http://yourdomain.com&lt;/em&gt; should
be sufficient for serving up a web site. And since &lt;em&gt;www&lt;/em&gt; is a subdomain, dropping
the prefix could potentially return a different response. There are some sites that
will fail to return without the prefix, and some sites that fail with it, but the
most common practice is that the &lt;em&gt;www&lt;/em&gt; subdomain is synonymous for no subdomain
at all.
&lt;/p&gt;
&lt;h3&gt;The Synonymous WWW and SEO
&lt;/h3&gt;
&lt;p&gt;
The issue with having two synonymous URLs (&lt;em&gt;http://yourdomain.com&lt;/em&gt; and &lt;em&gt;http://www.yourdomain.com&lt;/em&gt;)
is that search engines may interpret them as separate sites, even if they are serving
the same content. The two addresses are technically independent and are potentially
serving unique content; to a cautious search engine, even if pages appear to contain
the same content, there may be something different under the covers. This means your
audience's search results returns two entries for the same content. Some users will
happen to click on &lt;em&gt;yourdomain.com&lt;/em&gt; while others navigate to &lt;em&gt;www.yourdomain.com&lt;/em&gt;,
splitting your traffic, your page hits, your search ranking between two sites, unnecessarily.
&lt;/p&gt;
&lt;p&gt;
HTTP Redirects will cure the issue. If you access &lt;a href="http://google.com"&gt;http://google.com&lt;/a&gt;,
your browser is instantly redirected to &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;.
This is done through a HTTP 301 permanent redirect. Search Spiders recognize HTTP
response codes, and understand the 301 as a "use this other URL instead" command.
Many search engines, such as Google, will then update all page entries for the original
URI (&lt;em&gt;http://yourdomain.com&lt;/em&gt;) and replace it with the 301's destination URL
(&lt;em&gt;http://www.yourdomain.com&lt;/em&gt;). If there is already an entry for the destination
URL, the two entries will be merged together. The search entries for &lt;em&gt;yourdomain.com&lt;/em&gt; and &lt;em&gt;www.yourdomain.com&lt;/em&gt; will
now share traffic, share page hits, and share search ranking. Instead of having two
entries on the second and third pages of search results, combining these entries may
be just enough to place you on the first page of results.
&lt;/p&gt;
&lt;blockquote&gt;In addition to combining search entries for subdomains, you can also combine
root-level domains through HTTP 301. On this site, in addition to adding the &lt;em&gt;www&lt;/em&gt; prefix
if no subdomain is specified, &lt;a href="http://captainloadtest.com"&gt;captainloadtest.com&lt;/a&gt; will
HTTP 301 redirect to &lt;a href="http://www.cptloadtest.com"&gt;www.cptloadtest.com&lt;/a&gt;.&lt;/blockquote&gt; 
&lt;h3&gt;Combining the Synonyms
&lt;/h3&gt;
&lt;p&gt;
We need a way to implement an HTTP 301 redirect at the domain level for all requests
to a site; however, often we are using applications that may not grant us access to
the source, or we don't have the access into IIS through our host to set up redirects
for ourselves. &lt;a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx"&gt;URL
Rewrite, Part 2&lt;/a&gt; covers a great &lt;a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx"&gt;drop-in
redirect module&lt;/a&gt; by &lt;a href="http://www.pluralsight.com/community/blogs/fritz/"&gt;Fritz
Onion&lt;/a&gt; that uses a stand-alone assembly with a few additions in web.config to HTTP
301 redirect paths in your domain (it also supports HTTP 302 redirects). This module
is perfect for converting a WordPress blog post URL, such as &lt;a href="http://www.cptloadtest.com/?p=56"&gt;cptloadtest.com/?p=56&lt;/a&gt;,
to a DasBlog blog post URL like &lt;a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx"&gt;cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx&lt;/a&gt;.
However, to redirect domains and subdomains, the module must go a step further and
redirect based on matches against the entire URL, such as directing http:// to https://
or captainloadtest.com to cptloadtest.com, which it does not support. It's time for
some modifications.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols:linenumber[20]" name="code"&gt;private void OnBeginRequest(object src, EventArgs e) {
  HttpApplication app = src as HttpApplication;
  string reqUrl = app.Request.Url.AbsoluteUri;
  redirections redirs
    = (redirections) ConfigurationManager.GetSection("redirections");

  foreach (Add a in redirs.Adds) {
    Regex regex = new Regex(a.targetUrl, RegexOptions.IgnoreCase);
    if (regex.IsMatch(reqUrl)) {
      string targetUrl = regex.Replace(reqUrl, a.destinationUrl, 1);

      if (a.permanent) {
        app.Response.StatusCode = 301; // make a permanent redirect
        app.Response.AddHeader("Location", targetUrl);
        app.Response.End();
      }
      else
        app.Response.Redirect(targetUrl);

      break;
    }    
  }
}&lt;/pre&gt;
&lt;p&gt;
By converting &lt;em&gt;app.Request.RawURL&lt;/em&gt; to &lt;em&gt;app.Request.AbsoluteUri&lt;/em&gt;, the
regular expression will now match against the entire URL, rather than just the requested
path. There is one downside to this change: the value is the actual path processed,
not necessarily what was in the originally requested URL. To this effect, the value
of &lt;em&gt;AbsoluteUri&lt;/em&gt; for requesting &lt;a href="http://www.cptloadtest.com?p=56"&gt;http://www.cptloadtest.com?p=56&lt;/a&gt; is
actually &lt;a href="http://www.cptloadtest.com/default.aspx?p=56"&gt;http://www.cptloadtest.com/default.aspx?p=56&lt;/a&gt;;
by requesting the root directory, the default page is being processed, not the directory
itself, so &lt;em&gt;default.aspx&lt;/em&gt; is added to the URL. Keep this in mind when setting
up your redirection rules. Also, the original code converted the URL to lower case;
with my modifications, I chose to maintain the case of the URL, since sometimes case
matters, and instead ignore case in the regular expression match using &lt;em&gt;RegexOptions.IgnoreCase&lt;/em&gt;.
Finally, I made some other minor enhancements, like using the ConfigurationManager,
since ConfigurationSettings is now obsolete, and reusing the matching Regex instance
for replacements.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Download:&lt;/strong&gt; &lt;a href="http://www.cptloadtest.com/content/binary/RedirectModule.zip"&gt;RedirectModule.zip&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Includes:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Source code for the drop-in Redirect Module 
&lt;/li&gt;
&lt;li&gt;
Sample web.config that uses the module 
&lt;/li&gt;
&lt;li&gt;
Compiled version of redirectmodule.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The code is based on the original &lt;a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx"&gt;Redirect
Module&lt;/a&gt; by &lt;a href="http://www.pluralsight.com/community/blogs/fritz/default.aspx"&gt;Fritz
Onion&lt;/a&gt; and the &lt;a href="http://alt.pluralsight.com/wiki/default.aspx/Craig/XmlSerializerSectionHandler.html"&gt;Xml
Serializer Section Handler&lt;/a&gt; by &lt;a href="http://www.pluralsight.com/community/blogs/craig/default.aspx"&gt;Craig
Andera&lt;/a&gt;. As always, this code is provided with no warranties or guarantees. Use
at your own risk. Your mileage may vary. Thanks to Fritz Onion for the original work,
and allowing me extend his code further.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The usage is the same as Fritz Onion's original module. Drop the assembly into your
site's bin, and place a few lines into the web.config. The example below contains
the rules as they would apply to this site, 301 redirecting &lt;a href="http://www.captainloadtest.com"&gt;http://www.captainloadtest.com&lt;/a&gt; to &lt;a href="http://www.cptloadtest.com"&gt;http://www.cptloadtest.com&lt;/a&gt;,
and adding the &lt;em&gt;www&lt;/em&gt; subdomain to any domain requests that have no subdomain.
&lt;/p&gt;
&lt;pre class="xml:nocontrols:linenumber[20]" name="code"&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;configuration&amp;gt;
  &amp;lt;configSections&amp;gt;
    &amp;lt;section name="redirections"
      type="Pluralsight.Website.XmlSerializerSectionHandler, redirectmodule" /&amp;gt;
  &amp;lt;/configSections&amp;gt;
  &amp;lt;!-- Redirect Rules --&amp;gt;
  &amp;lt;redirections type="Pluralsight.Website.redirections, redirectmodule"&amp;gt;
    &amp;lt;!-- Domain Redirects //--&amp;gt;
    &amp;lt;add targetUrl="captainloadtest\.com/Default\.aspx"
      destinationUrl="cptloadtest.com/" permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="captainloadtest\.com"
      destinationUrl="cptloadtest.com" permanent="true" /&amp;gt;

    &amp;lt;!-- Add 'WWW' to the domain request //--&amp;gt;
    &amp;lt;add targetUrl="://cptloadtest\.com/Default\.aspx"
      destinationUrl="://www.$1.com/" permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="://cptloadtest\.com"
      destinationUrl="://www.$1.com" permanent="true" /&amp;gt;

    &amp;lt;!-- ...More Redirects --&amp;gt;
  &amp;lt;/redirections&amp;gt;
  &amp;lt;system.web&amp;gt;
    &amp;lt;httpModules&amp;gt;
      &amp;lt;add name="RedirectModule"
        type="Pluralsight.Website.RedirectModule, redirectmodule" /&amp;gt;
    &amp;lt;/httpModules&amp;gt;
  &amp;lt;/system.web&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;p&gt;
The component is easy to use, and can redirect your site traffic to any URL you choose.
Neither code changes to the application nor configuration changes to IIS are needed.
By using this module to combine synonymous versions of your URLs, such as alternate
domains or subdomains, you will improve your page ranking through combining duplicate
search result entries. One more step towards your own search engine optimization goals.
&lt;/p&gt;
&lt;h3&gt;URL Rewrite
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx"&gt;HTTP
302, Temporarily Relocated&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx"&gt;HTTP
301, Moved Permanently&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Part 3: Improving SEO and the 'www' subdomain&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:796ee61e-98c9-4ab1-8aef-6f3caa04ea6e" style="margin: 0px; padding: 0px; display: inline;"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/SEO" rel="tag"&gt;SEO&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Search%20Engine%20Optimization" rel="tag"&gt;Search
Engine Optimization&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Page%20Rank" rel="tag"&gt;Page
Rank&lt;/a&gt;,&lt;a href="http://technorati.com/tags/HTTP%20301" rel="tag"&gt;HTTP 301&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET" rel="tag"&gt;ASP.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Fritz%20Onion" rel="tag"&gt;Fritz
Onion&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=71ba8b42-6bcc-49e3-9f2f-48de47d8f275" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,71ba8b42-6bcc-49e3-9f2f-48de47d8f275.aspx</comments>
      <category>ASP.Net</category>
      <category>Blogging</category>
      <category>Programming</category>
      <category>SEO</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=69b08e29-c468-4517-8503-bb188c4e3888</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,69b08e29-c468-4517-8503-bb188c4e3888.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,69b08e29-c468-4517-8503-bb188c4e3888.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=69b08e29-c468-4517-8503-bb188c4e3888</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A few months ago I switched my blogging engine from WordPress to DasBlog, and was
left with a pile of broken post links. The WordPress format of formatting URLs, such
as <a href="http://www.cptloadtest.com/?p=56">cptloadtest.com/?p=56</a>, no longer
applied; DasBlog has its own way of building the URL: <a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx">cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx</a>.
Other people, with links to my posts on their own blogs, and search engines, with
search results pointing to the old format, no longer directed people to the proper
page. Fortunately, the old WordPress way directed users to the domain root, so they
saw the main page instead of a 404, but what I really needed was a way to get users
to the content they came for. But, I wanted a way that didn't involve customizing
DasBlog code.
</p>
        <p>
          <a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx">Part
1</a> discusses some client-side options for redirecting traffic. As a developer,
I would only need to add some quick JavaScript or HTML to my pages to reroute traffic,
but I wanted a better way. I wanted Google's links to be updated, rather than just
for the link to correctly route. I want a solution that worked without JavaScript.
I wanted a solution that didn't download the page twice, as any client-side processor
would do. I wanted the HTTP 301.
</p>
        <p>
HTTP 301, Move Permanently, is a server-side redirection of traffic. No client-side
processing is involved. And Google recognizes that this is a permanent redirect, as
it's name implies, so the Google result link is updated to the new URL. But since
this is a server-side process, and I don't have full access to IIS settings with my
hosting provider, I needed some code.
</p>
        <pre class="csharp:nocontrols" name="code">response.StatusCode = 301;
response.AddHeader("Location", "http://www.cptloadtest.com");
response.End();</pre>
        <blockquote>
          <p>
You may have noticed that, unlike the title of this blog post, I did not use a URL
rewrite. HTTP 301 (and 302) redirects are HTTP Response Codes that are returned to
the client browser, and the client then requests the new URL. A URL Rewrite leaves
the original URL in place, and renders a different page instead, without visibility
to the client browser or end-user. If I were to URL rewrite my blog from page1.aspx
to page2.aspx, the URL in your address bar would still list page1.aspx, even though
the content was from page2.aspx. The end-user (and the search engine) never know about
the URL for page2.aspx. However, with a HTTP redirect, the content and the address
bar would both be page2.aspx.
</p>
        </blockquote>
        <p>
I ran through Google to see if anyone had solved this problem in the past. I came
across a <a href="http://www.hanselman.com/blog/PermanentRedirectsWithHTTP301.aspx">Scott
Hanselman post</a> on HTTP 301, which mentions a fantastic <a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx">drop-in
redirect module</a> by <a href="http://www.pluralsight.com/community/blogs/fritz/">Fritz
Onion</a>. I was elated when I found this module, and it was very easy to implement.
Drop the assembly into your application's bin, add a few lines into the web.config,
and enjoy redirection bliss. The module also supports both HTTP 302 and HTTP 301 redirects
through the "permanent" attribute in the redirection rules.
</p>
        <pre class="xml:nocontrols" name="code">&lt;?xml version="1.0"?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;section name="redirections"
      type="Pluralsight.Website.XmlSerializerSectionHandler, redirectmodule" /&gt;
  &lt;/configSections&gt;
  &lt;!-- Redirect Rules --&gt;
  &lt;redirections type="Pluralsight.Website.redirections, redirectmodule"&gt;
    &lt;!-- Wordpress Post Redirects //--&gt;
    &lt;add targetUrl="/default\.aspx\?p=86"
      destinationUrl="/2007/09/22/ManagingMultipleEnvironmentConfigurationsThroughNAnt.aspx"
      permanent="true" /&gt;
    &lt;add targetUrl="/default\.aspx\?p=74"
      destinationUrl="/2007/02/09/Flash8DepthManagerBaffledByThoseWithoutDepth.aspx"
      permanent="true" /&gt;
    &lt;add targetUrl="/default\.aspx\?p=72"
      destinationUrl="/2006/10/20/IE7Released.aspx"
      permanent="true" /&gt;
    &lt;add targetUrl="/default\.aspx\?p=70"
      destinationUrl="/2006/10/17/ClearingFlashIDEWSDLCache.aspx"
      permanent="true" /&gt;
    &lt;add targetUrl="/default\.aspx\?p=69"
      destinationUrl="/2006/10/10/CruiseControlNetV11ReleasedOct1.aspx"
      permanent="true" /&gt;

    &lt;!-- ...More Redirects --&gt;
  &lt;/redirections&gt;
  &lt;system.web&gt;
    &lt;httpModules&gt;
      &lt;add name="RedirectModule"
        type="Pluralsight.Website.RedirectModule, redirectmodule" /&gt;
    &lt;/httpModules&gt;
  &lt;/system.web&gt;
&lt;/configuration&gt;</pre>
        <p>
The entire implementation took only a few minutes. It seemed like it took longer to
upload the changes to the blog than it did to make the local edits. The solution is
also very clean, as it required no modification to the existing application beyond
integrating the configuration blocks into the web.config. Blog traffic will now seamlessly
redirect from links that use the old WordPress URL to post that use the new DasBlog
URL. Search Engines also update their links, and the page ranking for each post isn't
lost into the ether just because the URL changed. All is well with the world, again.
</p>
        <p>
          <strong>Next up: </strong>
          <a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx">Part
3</a>. URL redirection also plays a role in Search Engine Optimization. In Part 3,
I will go over some ways that you can use HTTP redirects to improve your search engine
listings, as well as discuss some improvements I made to Fritz Onion's redirect module
in the name of SEO.
</p>
        <h3>URL Rewrite
</h3>
        <ul>
          <li>
Part 1: <a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx">HTTP
302, Temporarily Relocated</a></li>
          <li>
Part 2: HTTP 301, Moved Permanently 
</li>
          <li>
Part 3: <a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx">Improving
SEO and the 'www' subdomain</a></li>
        </ul>
        <div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3f1baf1f-daae-4ec5-b4e1-96bdabb5dd96" style="margin: 0px; padding: 0px; display: inline;">Technorati
Tags: <a href="http://technorati.com/tags/HTTP%20301" rel="tag">HTTP 301</a>,<a href="http://technorati.com/tags/HTTP%20302" rel="tag">HTTP
302</a>,<a href="http://technorati.com/tags/ASP.Net" rel="tag">ASP.Net</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=69b08e29-c468-4517-8503-bb188c4e3888" />
      </body>
      <title>URL Rewrite, Part 2: HTTP 301, Moved Permanently</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,69b08e29-c468-4517-8503-bb188c4e3888.aspx</guid>
      <link>http://www.cptloadtest.com/2008/11/11/URL-Rewrite-Part-2-HTTP-301-Moved-Permanently.aspx</link>
      <pubDate>Tue, 11 Nov 2008 17:36:34 GMT</pubDate>
      <description>&lt;p&gt;
A few months ago I switched my blogging engine from WordPress to DasBlog, and was
left with a pile of broken post links. The WordPress format of formatting URLs, such
as &lt;a href="http://www.cptloadtest.com/?p=56"&gt;cptloadtest.com/?p=56&lt;/a&gt;, no longer
applied; DasBlog has its own way of building the URL: &lt;a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx"&gt;cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx&lt;/a&gt;.
Other people, with links to my posts on their own blogs, and search engines, with
search results pointing to the old format, no longer directed people to the proper
page. Fortunately, the old WordPress way directed users to the domain root, so they
saw the main page instead of a 404, but what I really needed was a way to get users
to the content they came for. But, I wanted a way that didn't involve customizing
DasBlog code.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx"&gt;Part
1&lt;/a&gt; discusses some client-side options for redirecting traffic. As a developer,
I would only need to add some quick JavaScript or HTML to my pages to reroute traffic,
but I wanted a better way. I wanted Google's links to be updated, rather than just
for the link to correctly route. I want a solution that worked without JavaScript.
I wanted a solution that didn't download the page twice, as any client-side processor
would do. I wanted the HTTP 301.
&lt;/p&gt;
&lt;p&gt;
HTTP 301, Move Permanently, is a server-side redirection of traffic. No client-side
processing is involved. And Google recognizes that this is a permanent redirect, as
it's name implies, so the Google result link is updated to the new URL. But since
this is a server-side process, and I don't have full access to IIS settings with my
hosting provider, I needed some code.
&lt;/p&gt;
&lt;pre class="csharp:nocontrols" name="code"&gt;response.StatusCode = 301;
response.AddHeader("Location", "http://www.cptloadtest.com");
response.End();&lt;/pre&gt;
&lt;blockquote&gt; 
&lt;p&gt;
You may have noticed that, unlike the title of this blog post, I did not use a URL
rewrite. HTTP 301 (and 302) redirects are HTTP Response Codes that are returned to
the client browser, and the client then requests the new URL. A URL Rewrite leaves
the original URL in place, and renders a different page instead, without visibility
to the client browser or end-user. If I were to URL rewrite my blog from page1.aspx
to page2.aspx, the URL in your address bar would still list page1.aspx, even though
the content was from page2.aspx. The end-user (and the search engine) never know about
the URL for page2.aspx. However, with a HTTP redirect, the content and the address
bar would both be page2.aspx.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I ran through Google to see if anyone had solved this problem in the past. I came
across a &lt;a href="http://www.hanselman.com/blog/PermanentRedirectsWithHTTP301.aspx"&gt;Scott
Hanselman post&lt;/a&gt; on HTTP 301, which mentions a fantastic &lt;a href="http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/21/1651.aspx"&gt;drop-in
redirect module&lt;/a&gt; by &lt;a href="http://www.pluralsight.com/community/blogs/fritz/"&gt;Fritz
Onion&lt;/a&gt;. I was elated when I found this module, and it was very easy to implement.
Drop the assembly into your application's bin, add a few lines into the web.config,
and enjoy redirection bliss. The module also supports both HTTP 302 and HTTP 301 redirects
through the "permanent" attribute in the redirection rules.
&lt;/p&gt;
&lt;pre class="xml:nocontrols" name="code"&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;configuration&amp;gt;
  &amp;lt;configSections&amp;gt;
    &amp;lt;section name="redirections"
      type="Pluralsight.Website.XmlSerializerSectionHandler, redirectmodule" /&amp;gt;
  &amp;lt;/configSections&amp;gt;
  &amp;lt;!-- Redirect Rules --&amp;gt;
  &amp;lt;redirections type="Pluralsight.Website.redirections, redirectmodule"&amp;gt;
    &amp;lt;!-- Wordpress Post Redirects //--&amp;gt;
    &amp;lt;add targetUrl="/default\.aspx\?p=86"
      destinationUrl="/2007/09/22/ManagingMultipleEnvironmentConfigurationsThroughNAnt.aspx"
      permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="/default\.aspx\?p=74"
      destinationUrl="/2007/02/09/Flash8DepthManagerBaffledByThoseWithoutDepth.aspx"
      permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="/default\.aspx\?p=72"
      destinationUrl="/2006/10/20/IE7Released.aspx"
      permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="/default\.aspx\?p=70"
      destinationUrl="/2006/10/17/ClearingFlashIDEWSDLCache.aspx"
      permanent="true" /&amp;gt;
    &amp;lt;add targetUrl="/default\.aspx\?p=69"
      destinationUrl="/2006/10/10/CruiseControlNetV11ReleasedOct1.aspx"
      permanent="true" /&amp;gt;

    &amp;lt;!-- ...More Redirects --&amp;gt;
  &amp;lt;/redirections&amp;gt;
  &amp;lt;system.web&amp;gt;
    &amp;lt;httpModules&amp;gt;
      &amp;lt;add name="RedirectModule"
        type="Pluralsight.Website.RedirectModule, redirectmodule" /&amp;gt;
    &amp;lt;/httpModules&amp;gt;
  &amp;lt;/system.web&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;p&gt;
The entire implementation took only a few minutes. It seemed like it took longer to
upload the changes to the blog than it did to make the local edits. The solution is
also very clean, as it required no modification to the existing application beyond
integrating the configuration blocks into the web.config. Blog traffic will now seamlessly
redirect from links that use the old WordPress URL to post that use the new DasBlog
URL. Search Engines also update their links, and the page ranking for each post isn't
lost into the ether just because the URL changed. All is well with the world, again.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Next up: &lt;/strong&gt;&lt;a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx"&gt;Part
3&lt;/a&gt;. URL redirection also plays a role in Search Engine Optimization. In Part 3,
I will go over some ways that you can use HTTP redirects to improve your search engine
listings, as well as discuss some improvements I made to Fritz Onion's redirect module
in the name of SEO.
&lt;/p&gt;
&lt;h3&gt;URL Rewrite
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
Part 1: &lt;a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx"&gt;HTTP
302, Temporarily Relocated&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
Part 2: HTTP 301, Moved Permanently 
&lt;/li&gt;
&lt;li&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx"&gt;Improving
SEO and the 'www' subdomain&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3f1baf1f-daae-4ec5-b4e1-96bdabb5dd96" style="margin: 0px; padding: 0px; display: inline;"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/HTTP%20301" rel="tag"&gt;HTTP 301&lt;/a&gt;,&lt;a href="http://technorati.com/tags/HTTP%20302" rel="tag"&gt;HTTP
302&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.Net" rel="tag"&gt;ASP.Net&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=69b08e29-c468-4517-8503-bb188c4e3888" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,69b08e29-c468-4517-8503-bb188c4e3888.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=8e086b43-7342-4790-acfd-0b35ae0451c7</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,8e086b43-7342-4790-acfd-0b35ae0451c7.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,8e086b43-7342-4790-acfd-0b35ae0451c7.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8e086b43-7342-4790-acfd-0b35ae0451c7</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A few months ago I switched my blogging engine from WordPress to DasBlog. Though I
do feel that WordPress is a more mature engine, with a more robust feature set and
a better plug-in community, I wanted a site that was built on .NET. Being a .NET developer,
I wanted something I could tinker with,  modify, and adjust to my liking.
</p>
        <h3>Redirecting WordPress URL Format to DasBlog
</h3>
        <p>
One of the pain points of the switch was with the differences in how the two blogging
engines generated their URLs. As an example, my post on the <a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx">Collapse
All macro for the Visual Studio Solution Explorer</a>, originally written in WordPress,
went from a URL of <a href="http://www.cptloadtest.com/?p=56">cptloadtest.com/?p=56</a> to <a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx">cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx</a>.
A few sites, such as <a href="http://www.chinhdo.com/20070920/top-11-visual-studio-2005-ide-tips-and-tricks-to-make-you-a-more-productive-developer/">Chinhdo.com</a>,
link to the post's old URL, which without effort to put a redirect in place would
return HTTP 404: File Not Found under the new engine.
</p>
        <p>
Enter URL redirects. I needed a way to translate one URL into another. More specifically,
since WordPress retrieves all of its posts by query string (using the 'p' key), I
needed to translate a specific query string key/value pair into a URL. But, I didn't
want to have to recompile any code.
</p>
        <p>
There are a view client-side redirecting options available:
</p>
        <h3>Meta Refresh
</h3>
        <pre class="html:nocontrols" name="code">&lt;meta http-equiv="refresh" content="0;url=http://www.cptloadtest.com/"&gt;</pre>
        <p>
The Old-School HTML way of redirecting a page. Nothing but HTML is needed. Only access
to the base HTML page is required. Place the above HTML within your &lt;HEAD&gt; tag,
reformatting the content value with "X;url=myURL", where X is the number of seconds
to wait before initiating the redirect, and myURL is the destination URL. Any browsers
will obey Meta-Refresh, regardless of JavaScript's enabled setting, but the simple
implementation brings with it simple features. Meta-Refresh will redirect any request
the page, regardless of the query string, and there is no simple way to enable it
only for the specific key/value query string pair. Because all WordPress requests
are to the domain root--a single page--and referenced by query string, this would
not work for my needs. If the old request was instead to a unique former page, such
as /post56.html, I could go create a new copy of post56.html as just a stub, and have
each page Meta-Refresh to the post's new location. But even if this did apply, it's
too much work to create 56 stub pages for 56 former posts.
</p>
        <h3>JavaScript Redirect
</h3>
        <pre class="javascript:nocontrols" name="code">&lt;script type="text/javascript"&gt;
&lt;!--
window.location = "http://www.cptloadtest.com/"
//--&gt;
&lt;/script&gt;</pre>
        <p>
This one requires JavaScript to work. Since the query string is on the domain root,
the request is serving up Default.aspx and applying the query string to it. DasBlog
doesn't observe the "p" query string key, so it will not perform any action against
it, but the query string is still available to client-side scripts. I can add JavaScript
code to perform regular expression matches on the URL (window.location), and upon
match rewrite the old URL to the new and redirect. It's relatively simple to create
new match patterns for each of my 56 posts, and I can place all of my new client-side
code atop Default.aspx.
</p>
        <pre class="javascript:nocontrols" name="code">var oldPath = "/?p=56";
var newPath = "/2006/05/31/VSNetMacroCollapseAll.aspx";
if (document.URL.indexOf(oldPath) &gt; 0)
{
  window.location.replace(document.URL.replace(oldPath, newPath));
}</pre>
        <p>
However, since the patterns are in client-side code, they are visible to end-users
who view source. Users will also see page-flicker, as the Default.aspx is served up
using the old URL, only to be redirected and refreshed under the new URL; a side-effect
of downloading the page twice is that my bandwidth is also doubled for that single
request, since users downloaded the page twice.
</p>
        <h3>What it all means
</h3>
        <p>
All of the options above result in functionality similar a HTTP 302, otherwise known
as a Temporary Redirect. This class of redirect is meant for simple forwarding or
consolidation that is either of temporary nature or part of intended functional implementation.
An example of where this would be used is if after one page is finished processing,
another should be returned, such as if authentication is complete on a login page
and the client should be redirected to a home page or landing page. With a 302, the
original page is still a valid page, and users should still go to it in the future,
but under certain scenarios there is an alternate page should be used.
</p>
        <p>
The alternative to a 302 is a HTTP 301, otherwise known as Moved or a Permanent Redirect.
The 301 is for files which have permanently changed locations and come with an implied
"please update your links" notice. The HTTP 301 is ultimately what I was looking for.
cptloadtest.com/?p=56 is a defunct URL that will never again see light of day; users,
web sites, and (most importantly) search engines should update their references to
the new DasBlog format of the post's URL. Client-side coding doesn't have the ability
to create an HTTP 301, so it was beginning to look like I may either have to modify
DasBlog code to get my 301s or live without. But, I found a way; this site now has
all of the 301 goodness I craved, while keeping the DasBlog code pure.
</p>
        <p>
It's all about HTTP Modules.
</p>
        <p>
In <a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx">Part
2</a>, I will go over how to use HTTP Modules to implement both HTTP 301 and HTTP
302 redirects, all with simply dropping a file into your application bin and adding
a new section to your web.config. No compile required.
</p>
        <h3>URL Rewrite
</h3>
        <ul>
          <li>
Part 1: HTTP 302, Temporarily Relocated 
</li>
          <li>
Part 2: <a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx">HTTP
301, Moved Permanently</a></li>
          <li>
Part 3: <a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx">Improving
SEO and the 'www' subdomain</a></li>
        </ul>
        <div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:92bfc308-01b6-42cf-9a1e-c8cef5065b89" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati
Tags: <a href="http://technorati.com/tags/HTTP%20302" rel="tag">HTTP 302</a>,<a href="http://technorati.com/tags/URL%20Redirects" rel="tag">URL
Redirects</a></div>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=8e086b43-7342-4790-acfd-0b35ae0451c7" />
      </body>
      <title>URL Rewrite, Part 1: HTTP 302, Temporarily Relocated</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,8e086b43-7342-4790-acfd-0b35ae0451c7.aspx</guid>
      <link>http://www.cptloadtest.com/2008/11/06/URL-Rewrite-Part-1-HTTP-302-Temporarily-Relocated.aspx</link>
      <pubDate>Thu, 06 Nov 2008 15:01:33 GMT</pubDate>
      <description>&lt;p&gt;
A few months ago I switched my blogging engine from WordPress to DasBlog. Though I
do feel that WordPress is a more mature engine, with a more robust feature set and
a better plug-in community, I wanted a site that was built on .NET. Being a .NET developer,
I wanted something I could tinker with,&amp;nbsp; modify, and adjust to my liking.
&lt;/p&gt;
&lt;h3&gt;Redirecting WordPress URL Format to DasBlog
&lt;/h3&gt;
&lt;p&gt;
One of the pain points of the switch was with the differences in how the two blogging
engines generated their URLs. As an example, my post on the &lt;a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx"&gt;Collapse
All macro for the Visual Studio Solution Explorer&lt;/a&gt;, originally written in WordPress,
went from a URL of &lt;a href="http://www.cptloadtest.com/?p=56"&gt;cptloadtest.com/?p=56&lt;/a&gt; to &lt;a href="http://www.cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx"&gt;cptloadtest.com/2006/05/31/VSNetMacroCollapseAll.aspx&lt;/a&gt;.
A few sites, such as &lt;a href="http://www.chinhdo.com/20070920/top-11-visual-studio-2005-ide-tips-and-tricks-to-make-you-a-more-productive-developer/"&gt;Chinhdo.com&lt;/a&gt;,
link to the post's old URL, which without effort to put a redirect in place would
return HTTP 404: File Not Found under the new engine.
&lt;/p&gt;
&lt;p&gt;
Enter URL redirects. I needed a way to translate one URL into another. More specifically,
since WordPress retrieves all of its posts by query string (using the 'p' key), I
needed to translate a specific query string key/value pair into a URL. But, I didn't
want to have to recompile any code.
&lt;/p&gt;
&lt;p&gt;
There are a view client-side redirecting options available:
&lt;/p&gt;
&lt;h3&gt;Meta Refresh
&lt;/h3&gt;
&lt;pre class="html:nocontrols" name="code"&gt;&amp;lt;meta http-equiv="refresh" content="0;url=http://www.cptloadtest.com/"&amp;gt;&lt;/pre&gt;
&lt;p&gt;
The Old-School HTML way of redirecting a page. Nothing but HTML is needed. Only access
to the base HTML page is required. Place the above HTML within your &amp;lt;HEAD&amp;gt; tag,
reformatting the content value with "X;url=myURL", where X is the number of seconds
to wait before initiating the redirect, and myURL is the destination URL. Any browsers
will obey Meta-Refresh, regardless of JavaScript's enabled setting, but the simple
implementation brings with it simple features. Meta-Refresh will redirect any request
the page, regardless of the query string, and there is no simple way to enable it
only for the specific key/value query string pair. Because all WordPress requests
are to the domain root--a single page--and referenced by query string, this would
not work for my needs. If the old request was instead to a unique former page, such
as /post56.html, I could go create a new copy of post56.html as just a stub, and have
each page Meta-Refresh to the post's new location. But even if this did apply, it's
too much work to create 56 stub pages for 56 former posts.
&lt;/p&gt;
&lt;h3&gt;JavaScript Redirect
&lt;/h3&gt;
&lt;pre class="javascript:nocontrols" name="code"&gt;&amp;lt;script type="text/javascript"&amp;gt;
&amp;lt;!--
window.location = "http://www.cptloadtest.com/"
//--&amp;gt;
&amp;lt;/script&amp;gt;&lt;/pre&gt;
&lt;p&gt;
This one requires JavaScript to work. Since the query string is on the domain root,
the request is serving up Default.aspx and applying the query string to it. DasBlog
doesn't observe the "p" query string key, so it will not perform any action against
it, but the query string is still available to client-side scripts. I can add JavaScript
code to perform regular expression matches on the URL (window.location), and upon
match rewrite the old URL to the new and redirect. It's relatively simple to create
new match patterns for each of my 56 posts, and I can place all of my new client-side
code atop Default.aspx.
&lt;/p&gt;
&lt;pre class="javascript:nocontrols" name="code"&gt;var oldPath = "/?p=56";
var newPath = "/2006/05/31/VSNetMacroCollapseAll.aspx";
if (document.URL.indexOf(oldPath) &amp;gt; 0)
{
  window.location.replace(document.URL.replace(oldPath, newPath));
}&lt;/pre&gt;
&lt;p&gt;
However, since the patterns are in client-side code, they are visible to end-users
who view source. Users will also see page-flicker, as the Default.aspx is served up
using the old URL, only to be redirected and refreshed under the new URL; a side-effect
of downloading the page twice is that my bandwidth is also doubled for that single
request, since users downloaded the page twice.
&lt;/p&gt;
&lt;h3&gt;What it all means
&lt;/h3&gt;
&lt;p&gt;
All of the options above result in functionality similar a HTTP 302, otherwise known
as a Temporary Redirect. This class of redirect is meant for simple forwarding or
consolidation that is either of temporary nature or part of intended functional implementation.
An example of where this would be used is if after one page is finished processing,
another should be returned, such as if authentication is complete on a login page
and the client should be redirected to a home page or landing page. With a 302, the
original page is still a valid page, and users should still go to it in the future,
but under certain scenarios there is an alternate page should be used.
&lt;/p&gt;
&lt;p&gt;
The alternative to a 302 is a HTTP 301, otherwise known as Moved or a Permanent Redirect.
The 301 is for files which have permanently changed locations and come with an implied
"please update your links" notice. The HTTP 301 is ultimately what I was looking for.
cptloadtest.com/?p=56 is a defunct URL that will never again see light of day; users,
web sites, and (most importantly) search engines should update their references to
the new DasBlog format of the post's URL. Client-side coding doesn't have the ability
to create an HTTP 301, so it was beginning to look like I may either have to modify
DasBlog code to get my 301s or live without. But, I found a way; this site now has
all of the 301 goodness I craved, while keeping the DasBlog code pure.
&lt;/p&gt;
&lt;p&gt;
It's all about HTTP Modules.
&lt;/p&gt;
&lt;p&gt;
In &lt;a href="http://www.cptloadtest.com/2008/11/06/URLRewritePart1HTTP302TemporarilyRelocated.aspx"&gt;Part
2&lt;/a&gt;, I will go over how to use HTTP Modules to implement both HTTP 301 and HTTP
302 redirects, all with simply dropping a file into your application bin and adding
a new section to your web.config. No compile required.
&lt;/p&gt;
&lt;h3&gt;URL Rewrite
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
Part 1: HTTP 302, Temporarily Relocated 
&lt;li&gt;
Part 2: &lt;a href="http://www.cptloadtest.com/2008/11/11/URLRewritePart2HTTP301MovedPermanently.aspx"&gt;HTTP
301, Moved Permanently&lt;/a&gt; 
&lt;li&gt;
Part 3: &lt;a href="http://www.cptloadtest.com/2008/12/04/URLRewritePart3ImprovingSEOAndTheWwwSubdomain.aspx"&gt;Improving
SEO and the 'www' subdomain&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:92bfc308-01b6-42cf-9a1e-c8cef5065b89" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/HTTP%20302" rel="tag"&gt;HTTP 302&lt;/a&gt;,&lt;a href="http://technorati.com/tags/URL%20Redirects" rel="tag"&gt;URL
Redirects&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=8e086b43-7342-4790-acfd-0b35ae0451c7" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,8e086b43-7342-4790-acfd-0b35ae0451c7.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=05ff8148-029e-40d7-9fa1-2583ba42830b</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,05ff8148-029e-40d7-9fa1-2583ba42830b.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,05ff8148-029e-40d7-9fa1-2583ba42830b.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=05ff8148-029e-40d7-9fa1-2583ba42830b</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Many .Net developers that have experience in presentation-layer development have previous
exposure to this one, but to those who haven’t, I must share: Request.ApplicationPath
is evil! Request.Application path should never be used. Ever. I hate it!
</p>
        <p>
It’s misgivings are most commonly exposed when concatenating strings to the end of
it to create a URL of some sort.
</p>
        <blockquote>
          <p>
sURL = Request.ApplicationPath &amp; “/someDirectory/somePage.aspx”
</p>
        </blockquote>
        <p>
This is evil. Neither will work in all situations due to the method’s inconsistency
with trailing slashes.
</p>
        <p>
If the application root is in a sub-directory, say “http://server/myRoot/index.aspx”,
then the appPath is “/myRoot”. Note: there is no slash at the end, and the above sURL
gets a value of “/myRoot/someDirectory/somePage.aspx”.
</p>
        <p>
If the application root is not in a sub-directory, say “http://server/index.aspx”,
then the appPath is “/”. Note: there is a slash at the end, and the above sURL gets
a value or “//someDirectory/somePage.aspx”. In this case, rather than requesting “http://server/someDirectory/somePage.aspx”,
wonderful Internet Explorer requests “http://somedirectory/somePage.aspx”. (I don’t
fault IE for this. I commend it. It is actually one of the few cases where IE doesn’t
kludge together a band-aid for Developer mistakes.)
</p>
        <p>
So, don’t use Request.ApplicationPath. Ever. With no exception. You could make a method,
perhaps MyUtilities.ApplicationPath, that checks to see if the return contains a trailing
‘/’, and if it does, give it the axe. This will turn your domain-root appPath to an
empty string. Use your new method rather than Request.ApplicationPath, and all will
be well with the world. <strong>But, don’t do it!</strong> Don’t make a new method.
That just continues the evilness.
</p>
        <p>
Use <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuicontrolclassresolveurltopic.asp">Page.ResolveURL</a>(”~/someDirectory/somePage.aspx”)
or the counterpart Control.ResolveURL if you want it to be relative to the control’s
location. This is the only scenario you should use. ApplicationPath is evil!
</p>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=05ff8148-029e-40d7-9fa1-2583ba42830b" />
      </body>
      <title>Request.ApplicationPath is evil!</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,05ff8148-029e-40d7-9fa1-2583ba42830b.aspx</guid>
      <link>http://www.cptloadtest.com/2006/07/26/RequestApplicationPath-Is-Evil.aspx</link>
      <pubDate>Wed, 26 Jul 2006 12:19:23 GMT</pubDate>
      <description>&lt;p&gt;
Many .Net developers that have experience in presentation-layer development have previous
exposure to this one, but to those who haven’t, I must share: Request.ApplicationPath
is evil! Request.Application path should never be used. Ever. I hate it!
&lt;/p&gt;
&lt;p&gt;
It’s misgivings are most commonly exposed when concatenating strings to the end of
it to create a URL of some sort.
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;
sURL = Request.ApplicationPath &amp;amp; “/someDirectory/somePage.aspx”
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is evil. Neither will work in all situations due to the method’s inconsistency
with trailing slashes.
&lt;/p&gt;
&lt;p&gt;
If the application root is in a sub-directory, say “http://server/myRoot/index.aspx”,
then the appPath is “/myRoot”. Note: there is no slash at the end, and the above sURL
gets a value of “/myRoot/someDirectory/somePage.aspx”.
&lt;/p&gt;
&lt;p&gt;
If the application root is not in a sub-directory, say “http://server/index.aspx”,
then the appPath is “/”. Note: there is a slash at the end, and the above sURL gets
a value or “//someDirectory/somePage.aspx”. In this case, rather than requesting “http://server/someDirectory/somePage.aspx”,
wonderful Internet Explorer requests “http://somedirectory/somePage.aspx”. (I don’t
fault IE for this. I commend it. It is actually one of the few cases where IE doesn’t
kludge together a band-aid for Developer mistakes.)
&lt;/p&gt;
&lt;p&gt;
So, don’t use Request.ApplicationPath. Ever. With no exception. You could make a method,
perhaps MyUtilities.ApplicationPath, that checks to see if the return contains a trailing
‘/’, and if it does, give it the axe. This will turn your domain-root appPath to an
empty string. Use your new method rather than Request.ApplicationPath, and all will
be well with the world. &lt;strong&gt;But, don’t do it!&lt;/strong&gt; Don’t make a new method.
That just continues the evilness.
&lt;/p&gt;
&lt;p&gt;
Use &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuicontrolclassresolveurltopic.asp"&gt;Page.ResolveURL&lt;/a&gt;(”~/someDirectory/somePage.aspx”)
or the counterpart Control.ResolveURL if you want it to be relative to the control’s
location. This is the only scenario you should use. ApplicationPath is evil!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=05ff8148-029e-40d7-9fa1-2583ba42830b" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,05ff8148-029e-40d7-9fa1-2583ba42830b.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=ccfd87ed-d405-4ede-9ae3-999e231d2df9</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,ccfd87ed-d405-4ede-9ae3-999e231d2df9.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,ccfd87ed-d405-4ede-9ae3-999e231d2df9.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ccfd87ed-d405-4ede-9ae3-999e231d2df9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
NAnt hates .Net’s resource files, or .resx. Don’t get me wrong–it handles them just
fine–but large quantities of resx will really bog it down.
</p>
        <p>
Visual Studio loves resx. The IDE will automatically create a resource file for you
when you open pages and controls in the ‘designer’ view. Back when we still used Visual
SourceSafe as our SCM, Visual Studio happily checked the file in and forgot about
it. Now, our 500+ page application has 500+ resource files. Most of these 500+ resource
files contain zero resources, making them useless, pointless, and a detriment to the
build.
</p>
        <p>
This morning I went through the build log, noting every resx that contained zero resources,
and deleted all of these useless files.
</p>
        <p>
The compile time dropped by 5 minutes.
</p>
        <p>
          <em>Moral of the story:</em> Be weary of Visual Studio. With regards to resx, VS is
a malware program that’s just filling your hard drive with junk. If you use resx,
great, but if you don’t, delete them all. NAnt will love you for it.
</p>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=ccfd87ed-d405-4ede-9ae3-999e231d2df9" />
      </body>
      <title>NAnt slowdowns: Visual Studio, the .resx machine</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,ccfd87ed-d405-4ede-9ae3-999e231d2df9.aspx</guid>
      <link>http://www.cptloadtest.com/2006/02/15/NAnt-Slowdowns-Visual-Studio-The-Resx-Machine.aspx</link>
      <pubDate>Wed, 15 Feb 2006 16:31:31 GMT</pubDate>
      <description>&lt;p&gt;
NAnt hates .Net’s resource files, or .resx. Don’t get me wrong–it handles them just
fine–but large quantities of resx will really bog it down.
&lt;/p&gt;
&lt;p&gt;
Visual Studio loves resx. The IDE will automatically create a resource file for you
when you open pages and controls in the ‘designer’ view. Back when we still used Visual
SourceSafe as our SCM, Visual Studio happily checked the file in and forgot about
it. Now, our 500+ page application has 500+ resource files. Most of these 500+ resource
files contain zero resources, making them useless, pointless, and a detriment to the
build.
&lt;/p&gt;
&lt;p&gt;
This morning I went through the build log, noting every resx that contained zero resources,
and deleted all of these useless files.
&lt;/p&gt;
&lt;p&gt;
The compile time dropped by 5 minutes.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Moral of the story:&lt;/em&gt; Be weary of Visual Studio. With regards to resx, VS is
a malware program that’s just filling your hard drive with junk. If you use resx,
great, but if you don’t, delete them all. NAnt will love you for it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=ccfd87ed-d405-4ede-9ae3-999e231d2df9" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,ccfd87ed-d405-4ede-9ae3-999e231d2df9.aspx</comments>
      <category>ASP.Net</category>
      <category>Continuous Integration</category>
      <category>NAnt</category>
      <category>Programming</category>
      <category>Task Automation</category>
      <category>Visual Studio</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=185d4fd2-87ca-49d8-b7f9-b06b3068559e</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,185d4fd2-87ca-49d8-b7f9-b06b3068559e.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,185d4fd2-87ca-49d8-b7f9-b06b3068559e.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=185d4fd2-87ca-49d8-b7f9-b06b3068559e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
“It compiles! Ship it!”
</p>
        <p>
Microsoft has sent Visual Studio 2005 to the printers. That brings .Net 2.0 to the
table in all of its glory. The official release date is still November 7, and though
it is available now to all of us MSDN subscribers (though the site is too flooded
to ping, let alone download), there is still some question on if the media will be
ready in time to go in all of the pretty little VS05 boxes at your local Microsoft
store.
</p>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=185d4fd2-87ca-49d8-b7f9-b06b3068559e" />
      </body>
      <title>Visual Studio 2005 goes gold</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,185d4fd2-87ca-49d8-b7f9-b06b3068559e.aspx</guid>
      <link>http://www.cptloadtest.com/2005/10/28/Visual-Studio-2005-Goes-Gold.aspx</link>
      <pubDate>Fri, 28 Oct 2005 17:40:03 GMT</pubDate>
      <description>&lt;p&gt;
“It compiles! Ship it!”
&lt;/p&gt;
&lt;p&gt;
Microsoft has sent Visual Studio 2005 to the printers. That brings .Net 2.0 to the
table in all of its glory. The official release date is still November 7, and though
it is available now to all of us MSDN subscribers (though the site is too flooded
to ping, let alone download), there is still some question on if the media will be
ready in time to go in all of the pretty little VS05 boxes at your local Microsoft
store.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=185d4fd2-87ca-49d8-b7f9-b06b3068559e" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,185d4fd2-87ca-49d8-b7f9-b06b3068559e.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
      <category>Tools</category>
      <category>Visual Studio</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=4bf11370-eed0-41d9-92c8-d4c90bfd1e0e</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,4bf11370-eed0-41d9-92c8-d4c90bfd1e0e.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,4bf11370-eed0-41d9-92c8-d4c90bfd1e0e.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=4bf11370-eed0-41d9-92c8-d4c90bfd1e0e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The default settings of NUnit, TestRunner, and Test Driven Development all want different
copies of the app.config at different locations. If ProjectName creates ProjectName.dll,
then NUnit wants ProjectName.config, TR wants ProjectName.dll.config, and TDD wants
TargetDir\ProjectName.dll.config. This is a lot of work to put in the post-build event
of every unit test project, and can be even more work when another testing tool comes
along that wants yet a new config filename. The best way to manage all of these file
copies is through a common post-build event call.
</p>
        <p>
Many probably opt for a NAnt script, but we found that passing in the required paths
can sometimes cause NAnt to get confused, and it won’t properly parse the parameter
listing. So, we went with a command file, instead.
</p>
        <p>
          <strong>CopyConfigs.cmd</strong>
        </p>
        <div style="border: 1px solid rgb(51, 51, 51); padding: 7px; background: white none repeat scroll 0% 50%; font-size: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
          <p style="margin: 0px;">
            <span style="color: green;">rem for nunit</span>
          </p>
          <p style="margin: 0px;">
            <span style="color: blue;">copy</span> “%~1App.config” “%~1%~2.config”
</p>
          <p style="margin: 0px;">
 
</p>
          <p style="margin: 0px;">
            <span style="color: green;">rem for testrunner</span>
          </p>
          <p style="margin: 0px;">
            <span style="color: blue;">copy</span> “%~1App.config” “%~1%~2.dll.config”
</p>
          <p style="margin: 0px;">
 
</p>
          <p style="margin: 0px;">
            <span style="color: green;">rem for testdrivendevelopment</span>
          </p>
          <p style="margin: 0px;">
            <span style="color: blue;">copy</span> “%~1App.config” “%~3.config”
</p>
        </div>
        <p>
          <strong>VS.Net Post Build Event</strong>
        </p>
        <div style="border: 1px solid rgb(51, 51, 51); padding: 7px; background: white none repeat scroll 0% 50%; font-size: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
          <p style="margin: 0px;">
            <span style="color: blue;">call</span> “C:\MyPath\CopyConfigs.cmd” “$(ProjectDir)”
“$(ProjectName) “$(TargetPath)”
</p>
        </div>
        <p>
VS.Net already includes a series of NAnt-like properties for project names, project
directories, target [assembly] filenames, etc; these come in handy for creating a
universal script. Placing the path references in quotes allows for spaces and other
characters (Except more quotes) in the path. Executing the command file through a
call allows us a little more versatility with the argument references (%~1 removes
the surrounding quotes from the argument value, allowing us to append a few together
without jacking the subsequent path).
</p>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4bf11370-eed0-41d9-92c8-d4c90bfd1e0e" />
      </body>
      <title>Managing app.config for testing tools</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,4bf11370-eed0-41d9-92c8-d4c90bfd1e0e.aspx</guid>
      <link>http://www.cptloadtest.com/2005/08/08/Managing-Appconfig-For-Testing-Tools.aspx</link>
      <pubDate>Mon, 08 Aug 2005 17:48:20 GMT</pubDate>
      <description>&lt;p&gt;
The default settings of NUnit, TestRunner, and Test Driven Development all want different
copies of the app.config at different locations. If ProjectName creates ProjectName.dll,
then NUnit wants ProjectName.config, TR wants ProjectName.dll.config, and TDD wants
TargetDir\ProjectName.dll.config. This is a lot of work to put in the post-build event
of every unit test project, and can be even more work when another testing tool comes
along that wants yet a new config filename. The best way to manage all of these file
copies is through a common post-build event call.
&lt;/p&gt;
&lt;p&gt;
Many probably opt for a NAnt script, but we found that passing in the required paths
can sometimes cause NAnt to get confused, and it won’t properly parse the parameter
listing. So, we went with a command file, instead.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CopyConfigs.cmd&lt;/strong&gt;
&lt;/p&gt;
&lt;div style="border: 1px solid rgb(51, 51, 51); padding: 7px; background: white none repeat scroll 0% 50%; font-size: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: green;"&gt;rem for nunit&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;copy&lt;/span&gt; “%~1App.config” “%~1%~2.config”
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: green;"&gt;rem for testrunner&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;copy&lt;/span&gt; “%~1App.config” “%~1%~2.dll.config”
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: green;"&gt;rem for testdrivendevelopment&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;copy&lt;/span&gt; “%~1App.config” “%~3.config”
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;strong&gt;VS.Net Post Build Event&lt;/strong&gt;
&lt;/p&gt;
&lt;div style="border: 1px solid rgb(51, 51, 51); padding: 7px; background: white none repeat scroll 0% 50%; font-size: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;call&lt;/span&gt; “C:\MyPath\CopyConfigs.cmd” “$(ProjectDir)”
“$(ProjectName) “$(TargetPath)”
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
VS.Net already includes a series of NAnt-like properties for project names, project
directories, target [assembly] filenames, etc; these come in handy for creating a
universal script. Placing the path references in quotes allows for spaces and other
characters (Except more quotes) in the path. Executing the command file through a
call allows us a little more versatility with the argument references (%~1 removes
the surrounding quotes from the argument value, allowing us to append a few together
without jacking the subsequent path).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=4bf11370-eed0-41d9-92c8-d4c90bfd1e0e" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,4bf11370-eed0-41d9-92c8-d4c90bfd1e0e.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
      <category>Task Automation</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=06a099a6-6f14-4f25-a729-caacec5191bc</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,06a099a6-6f14-4f25-a729-caacec5191bc.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,06a099a6-6f14-4f25-a729-caacec5191bc.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=06a099a6-6f14-4f25-a729-caacec5191bc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In code deployment, it is often necessary to perform tasks that must be executed locally
on the destination box, such as installing through an MSI or installing assemblies
to the GAC through GACUtils. Thankfully, there is a way that this can all be done
remotely, with any process, as long as it can be accessed through a command prompt.
The destination computer will think that you are working on it directly, though it
may just be your NAnt script doing the work for you.
</p>
        <p>
          <a href="http://www.sysinternals.com/">SysInternals</a> publishes a tool called <a href="http://www.sysinternals.com/Utilities/PsExec.html">PsExec</a>.
It allows you to execute a program remotely on a remote machine. To the remote machine
the process is running locally. Because of this, you can use traditional command line
tools to run programs and utilities, such as GACUtil to install a new assembly to
the GAC on a remote box–a feature that most other options don’t support.
</p>
        <blockquote>
          <p>
            <code>PSExec \MyServer "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\GACUtil.exe"
"C:\bin\MyAssembly.dll"</code>
          </p>
          <p>
Note: the paths are all ‘local’ paths on \\MyServer, just as you would enter from
a command prompt in an RDP session to MyServer. You will also need to customize the
paths to include whatever location and framework version your remote machine uses.
</p>
        </blockquote>
        <p>
As for me, because our web applications make thorough use of the GAC, our only deployment
method is a VS.Net Deployment project to create a MSI. We have NAnt scripts that upload
the MSI to remote machines then execute PsExec run the installation (MSIExec) in unattended
mode. It has brought our deployment time down from a manual 30-45 minutes to an automated
15 minutes, and allows code managers to spend those 30-45 minutes doing something
else. We save even more time when we deploy to production, which contains an 8-server
web farm.
</p>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=06a099a6-6f14-4f25-a729-caacec5191bc" />
      </body>
      <title>Remote deployment to the GAC</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,06a099a6-6f14-4f25-a729-caacec5191bc.aspx</guid>
      <link>http://www.cptloadtest.com/2005/07/26/Remote-Deployment-To-The-GAC.aspx</link>
      <pubDate>Tue, 26 Jul 2005 15:39:49 GMT</pubDate>
      <description>&lt;p&gt;
In code deployment, it is often necessary to perform tasks that must be executed locally
on the destination box, such as installing through an MSI or installing assemblies
to the GAC through GACUtils. Thankfully, there is a way that this can all be done
remotely, with any process, as long as it can be accessed through a command prompt.
The destination computer will think that you are working on it directly, though it
may just be your NAnt script doing the work for you.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.sysinternals.com/"&gt;SysInternals&lt;/a&gt; publishes a tool called &lt;a href="http://www.sysinternals.com/Utilities/PsExec.html"&gt;PsExec&lt;/a&gt;.
It allows you to execute a program remotely on a remote machine. To the remote machine
the process is running locally. Because of this, you can use traditional command line
tools to run programs and utilities, such as GACUtil to install a new assembly to
the GAC on a remote box–a feature that most other options don’t support.
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;
&lt;code&gt;PSExec \MyServer "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\GACUtil.exe"
"C:\bin\MyAssembly.dll"&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Note: the paths are all ‘local’ paths on \\MyServer, just as you would enter from
a command prompt in an RDP session to MyServer. You will also need to customize the
paths to include whatever location and framework version your remote machine uses.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
As for me, because our web applications make thorough use of the GAC, our only deployment
method is a VS.Net Deployment project to create a MSI. We have NAnt scripts that upload
the MSI to remote machines then execute PsExec run the installation (MSIExec) in unattended
mode. It has brought our deployment time down from a manual 30-45 minutes to an automated
15 minutes, and allows code managers to spend those 30-45 minutes doing something
else. We save even more time when we deploy to production, which contains an 8-server
web farm.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=06a099a6-6f14-4f25-a729-caacec5191bc" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,06a099a6-6f14-4f25-a729-caacec5191bc.aspx</comments>
      <category>ASP.Net</category>
      <category>Tools</category>
    </item>
    <item>
      <trackback:ping>http://www.cptloadtest.com/Trackback.aspx?guid=3de55271-4364-4ef3-ae9a-d1d11113ed56</trackback:ping>
      <pingback:server>http://www.cptloadtest.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.cptloadtest.com/PermaLink,guid,3de55271-4364-4ef3-ae9a-d1d11113ed56.aspx</pingback:target>
      <dc:creator>Jay Harris</dc:creator>
      <wfw:comment>http://www.cptloadtest.com/CommentView,guid,3de55271-4364-4ef3-ae9a-d1d11113ed56.aspx</wfw:comment>
      <wfw:commentRss>http://www.cptloadtest.com/SyndicationService.asmx/GetEntryCommentsRss?guid=3de55271-4364-4ef3-ae9a-d1d11113ed56</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Scott Hanselman has a good post today about the HttpOnly cookie attribute. It secures
the cookie from access via the DOM. “The value of this property is questionable since
any sniffer or Fiddler could easily remove it. That said, it could slow down the average
script kiddie for 15 seconds.”
</p>
        <p>
Read Scott’s <a href="http://www.hanselman.com/blog/HttpOnlyCookiesOnASPNET11.aspx">full
blog entry</a>.
</p>
        <p>
Here’s the meat-and-potatoes of what Scott came up with; it’s for your global.asax:
</p>
        <pre name="code" class="csharp:nocontrols">protected void Application_EndRequest(Object sender, EventArgs e)
{
    foreach(string cookie in Response.Cookies)
    {
        const string HTTPONLY = ";HttpOnly";
        string path = Response.Cookies[cookie].Path;
        if (path.EndsWith(HTTPONLY) == false)
        {
            //force HttpOnly to be added to the cookie
            Response.Cookies[cookie].Path += HTTPONLY;
        }
    }
}</pre>
        <img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=3de55271-4364-4ef3-ae9a-d1d11113ed56" />
      </body>
      <title>“HttpOnly Cookies on ASP.NET 1.1″ - a little added security</title>
      <guid isPermaLink="false">http://www.cptloadtest.com/PermaLink,guid,3de55271-4364-4ef3-ae9a-d1d11113ed56.aspx</guid>
      <link>http://www.cptloadtest.com/2005/07/22/HttpOnly-Cookies-On-ASPNET-11-A-Little-Added-Security.aspx</link>
      <pubDate>Fri, 22 Jul 2005 18:01:58 GMT</pubDate>
      <description>&lt;p&gt;
Scott Hanselman has a good post today about the HttpOnly cookie attribute. It secures
the cookie from access via the DOM. “The value of this property is questionable since
any sniffer or Fiddler could easily remove it. That said, it could slow down the average
script kiddie for 15 seconds.”
&lt;/p&gt;
&lt;p&gt;
Read Scott’s &lt;a href="http://www.hanselman.com/blog/HttpOnlyCookiesOnASPNET11.aspx"&gt;full
blog entry&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Here’s the meat-and-potatoes of what Scott came up with; it’s for your global.asax:
&lt;/p&gt;
&lt;pre name="code" class="csharp:nocontrols"&gt;protected void Application_EndRequest(Object sender, EventArgs e)
{
    foreach(string cookie in Response.Cookies)
    {
        const string HTTPONLY = ";HttpOnly";
        string path = Response.Cookies[cookie].Path;
        if (path.EndsWith(HTTPONLY) == false)
        {
            //force HttpOnly to be added to the cookie
            Response.Cookies[cookie].Path += HTTPONLY;
        }
    }
}&lt;/pre&gt;&lt;img width="0" height="0" src="http://www.cptloadtest.com/aggbug.ashx?id=3de55271-4364-4ef3-ae9a-d1d11113ed56" /&gt;</description>
      <comments>http://www.cptloadtest.com/CommentView,guid,3de55271-4364-4ef3-ae9a-d1d11113ed56.aspx</comments>
      <category>ASP.Net</category>
      <category>Programming</category>
    </item>
  </channel>
</rss>