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

<channel>
	<title>YTechie.com &#187; asp.net</title>
	<atom:link href="http://www.ytechie.com/category/aspnet/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ytechie.com</link>
	<description>Productive software development using ASP.NET, C#, Adobe Flex, and other technologies and tools.</description>
	<lastBuildDate>Fri, 06 Nov 2009 21:16:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Slick inline trace logging in ASP.NET</title>
		<link>http://www.ytechie.com/2009/03/slick-inline-trace-logging-in-aspnet.html</link>
		<comments>http://www.ytechie.com/2009/03/slick-inline-trace-logging-in-aspnet.html#comments</comments>
		<pubDate>Tue, 03 Mar 2009 18:02:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2009/03/slick-inline-trace-logging-in-aspnet.html</guid>
		<description><![CDATA[I’m going to show you a slick way to configure log4net to your trace log, and then make it extremely simple to view the trace log for a page while viewing that page. Ultimately, we’ll end up with something that looks like this:
 
It may be difficult to see from the screenshot, but I’m looking [...]]]></description>
			<content:encoded><![CDATA[<p>I’m going to show you a slick way to configure <a href="http://logging.apache.org/log4net/index.html" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/logging.apache.org/log4net/index.html?referer=');">log4net</a> to your trace log, and then make it extremely simple to view the trace log for a page <strong>while</strong> viewing that page. Ultimately, we’ll end up with something that looks like this:</p>
<p><a href="http://www.ytechie.com/post-images/2009/03/pagetracing.png"><img title="page-tracing" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="446" alt="page-tracing" src="http://www.ytechie.com/post-images/2009/03/pagetracing-thumb.png" width="485" border="0" /></a> </p>
<p>It may be difficult to see from the screenshot, but I’m looking at a standard ASP.NET page, but there is tracing information at the bottom. This tracing information is the same information you would see if you configured tracing your <em>web.config</em>, and then used the <em>trace.axd</em> page. However, we’re displaying it right along with the page request. To allow tracing to be enabled with a simple URL parameter, you can add the following code into your <em>Global.asax</em> file:</p>
<div class="wlWriterEditableSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d2c077e4-6d53-4f24-9efe-01190af67dec" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">protected void Application_BeginRequest(object s, EventArgs e)
{
	if (Context.Request.QueryString["trace"] == "true")
		Context.Trace.IsEnabled = true;
}</pre>
</div>
<p>Of course in a public environment, it may be wise to add security or at least obfuscate the parameter. The trace information contains server information that may be helpful to someone trying to compromise the server.</p>
<p>Now that we have tracing information, how can we get our log messages to show up? Log4net provides an appender for logging to the ASP.NET tracing feature:</p>
<p><div class="wlWriterEditableSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:ab0b4ffe-4201-497b-9353-12ce443cab15" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="xml">&lt;appender name="AspNetTraceAppender"
	type="log4net.Appender.AspNetTraceAppender" &gt;
	&lt;layout type="log4net.Layout.PatternLayout"&gt;
		&lt;conversionPattern
			value="[Thread #%thread] %-5level - %message%newline" /&gt;
	&lt;/layout&gt;
&lt;/appender&gt;</pre>
</div>
<p>Don’t forget to wire up the adapter so that log4net knows to use it:</p>
<div class="wlWriterEditableSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:7b11243d-0aa8-44f5-87a2-4660a3edb830" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="xml">&lt;root&gt;
	&lt;level value="DEBUG" /&gt;
	&lt;appender-ref ref="AspNetTraceAppender" /&gt;
&lt;/root&gt;</pre>
</div>
<p>Now, in your code, you can simply log messages as you normally would:</p>
<div class="wlWriterEditableSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d73b9a80-cf72-43f8-9911-0efa3e535a00" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">_log.Debug("Loading User Data");</pre>
</div>
<p><strong>Conclusion</strong></p>
<p>I’ve found this type of configuration very useful in my ASP.NET applications. It lets me analyze how long each portion of the page generation is taking so that I can find bottlenecks. It also motivates me to write a fair amount of logging, since I’ll see a benefit during development, as well as after deploying it into the wild.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2009/03/slick-inline-trace-logging-in-aspnet.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cloud Computing (and Azure) &#8211; Right for your site?</title>
		<link>http://www.ytechie.com/2008/11/cloud-computing-and-azure-right-for-your-site.html</link>
		<comments>http://www.ytechie.com/2008/11/cloud-computing-and-azure-right-for-your-site.html#comments</comments>
		<pubDate>Tue, 11 Nov 2008 17:49:37 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/11/cloud-computing-and-azure-right-for-your-site.html</guid>
		<description><![CDATA[Everyone seems to be getting excited lately about the prospect of cloud computing. Just like many others, I get excited by the idea that I wouldn&#8217;t have to worry about adding servers to scale up. Theoretically, a guy (or girl) could make the next YouTube, in his basement, for free. However, there is one huge [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone seems to be getting excited lately about the prospect of cloud computing. Just like many others, I get excited by the idea that I wouldn&#8217;t have to worry about adding servers to scale up. Theoretically, a guy (or girl) could make the next YouTube, in his basement, for <em>free</em>. However, there is one huge advantage that most people ignore, and that&#8217;s the fact that&#8217;s also <strong>perfect for a small scale website</strong>.</p>
<p align="center"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="175" alt="iStock_000004135866XSmall" src="http://www.ytechie.com/post-images/2008/11/istock-000004135866xsmall.jpg" width="240" border="0"/> </p>
<p>I&#8217;ve tried or considered many different ways of hosting my content:</p>
<ul>
<li><strong>Shared hosting</strong> &#8211; Cheaply host your sites, but be at the mercy of their IT guy messing with your computer and rebooting it for automatic updates. Also, in my experience, the performance is terrible if your traffic spikes. They typically have hundreds of users on the same server as you, and you all get to compete for performance.  </li>
<li><strong>Dedicated hosting</strong> &#8211; This is what I use now, because it ensures that I get the full performance of a machine. The disadvantage is that I have a single point of failure, and I have to manage the machine myself. </li>
<li><strong>Hosting from home</strong> &#8211; Yes, people actually do this. If you have a high enough upload speed it shouldn&#8217;t be too bad. The problem is that your connection typically won&#8217;t be able to handle traffic spikes. You&#8217;ll also potentially be a victim of power or Internet outages, where professional hosts would have redundant systems in place (in theory).</li>
</ul>
<p>Now, let&#8217;s talk about cloud computing. That magical cloud that many don&#8217;t understand. There are two potentially viable cloud computing methods available right now:</p>
<ul>
<li><strong>Cloud virtual machines</strong> &#8211; Amazon&#8217;s EC2 solution is probably the most popular in this category. Basically, you can create, start, and stop virtual machines remotely. You just pay an hourly rate while the computer is running. You can even upgrade and downgrade the hardware as needed. The advantage is that you can treat the computer like a physical machine and configure and use it however you like. The disadvantage is that maintaining individual machines can be time consuming and is not necessarily part of your core business. </li>
<li><strong>Cloud application server</strong> &#8211; Instead of creating virtual machines, a cloud application server runs your application directly. You no longer worry about the constraints of a physical machine. You application could potentially be run on dozens or hundreds of servers simultaneously. The major advantage is that there is little to no maintenance, because that is the job of the provider.</li>
</ul>
<p>I see the cloud application server as having some of the greatest advantages. You&#8217;re free to write your application with a level of abstraction, which allows you to solve the problems you really want to solve.</p>
<p>One of the most well known cloud application services is the <a href="http://code.google.com/appengine/" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/code.google.com/appengine/?referer=');">Google App Engine</a>, which currently supports Python applications. Microsoft joined the game recently with <a href="http://www.microsoft.com/azure/default.mspx" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.microsoft.com/azure/default.mspx?referer=');">Azure</a> for ASP.NET.</p>
<p>As I mentioned, not only do application servers let your applications scale up, they let you <strong>pay only for what you use</strong>. This is great for the small to medium website&#8217;s that are stuck with bad shared hosting or difficult-to-manage dedicated hosting. The fact is that most sites get a <strong>few hundred visitors daily or less</strong>. If you start to think about how often a page is actually requested, you&#8217;ll realize that it&#8217;s not very much. Even with 500 users requesting 5 pages each in a 12 hour period can easily be handled with a very low end server from years ago.</p>
<p>The reason that application servers are so much more efficient than shared hosting is because they&#8217;re built from the ground up to spread the load around. This results in higher utilization, but more headroom for any single application. Shared hosting providers can move users between servers, but it&#8217;s usually a manual, and often difficult process. You&#8217;re bound to a specific physical machine (unless it&#8217;s VPS hosting), and if it goes down, so does your site.</p>
<p>Cloud computing is also a great way to handle traffic spikes such as the Digg effect. Let&#8217;s say that you only have 500 visitors today, but might get 10, 100, or 1000 times more in a single day. <a href="http://www.lukasbiewald.com/?p=153" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.lukasbiewald.com/?p=153&amp;referer=');">It happened to FaceStat</a>. They went from 10,000 page views per day to almost a million because of a story on the front page of Yahoo. They had to scramble to add application servers and develop a scaling strategy immediately.</p>
<p><strong>Conclusion &#8211; Cloud Application Server Benefits</strong></p>
<p>Cloud computing has tremendous benefits. You no longer have to worry about scaling the underlying hardware, you simply pay as you go, and you can handle traffic spikes with ease. Once cloud computing becomes mainstream and absolutely reliable, there will be few reasons to not use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/11/cloud-computing-and-azure-right-for-your-site.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC Pro&#8217;s and Con&#8217;s</title>
		<link>http://www.ytechie.com/2008/10/aspnet-mvc-pros-and-cons.html</link>
		<comments>http://www.ytechie.com/2008/10/aspnet-mvc-pros-and-cons.html#comments</comments>
		<pubDate>Mon, 20 Oct 2008 13:53:41 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/10/aspnet-mvc-pros-and-cons.html</guid>
		<description><![CDATA[In our current iteration of improving our software development strategy, ASP.NET WebForms simply doesn&#8217;t fit in with the new demands of being unit testable and flexible. It comes as no surprise that ASP.NET MVC has been getting all the headlines lately. Like many others, I&#8217;ve gotten the itch to try it out. In this post [...]]]></description>
			<content:encoded><![CDATA[<p>In our current iteration of improving our software development strategy, ASP.NET WebForms simply doesn&#8217;t fit in with the new demands of being unit testable and flexible. It comes as no surprise that ASP.NET MVC has been getting all the headlines lately. Like many others, I&#8217;ve gotten the itch to try it out. In this post I&#8217;ll talk about what I believe are the main pro&#8217;s and con&#8217;s of the new style for building web applications.</p>
<p align="center"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="180" alt="Scale" src="http://www.ytechie.com/post-images/2008/10/scale.jpg" width="173" border="0" /> </p>
<p><strong>PRO &#8211; No ViewState or &quot;surprise crap&quot;</strong></p>
<p>In traditional ASP.NET WebForms, the luxury of pretending to behave like a Windows Form comes at a price. The ViewState is a reliable way of storing all of the state information for the form. Unfortunately, due to the limitations of the web, this data needs to be a giant string inside of a hidden form field. This ends up adding a substantial number of bytes to the page, making it slower and requiring extra bandwidth. Of course the ViewState is controllable, much like the dinosaurs in Jurassic Park.</p>
<p>Not only is the ViewState gone, but &quot;mystery&quot; generated HTML is also gone. You have strict control over the HTML. This gives you great power, but with great power comes great responsibility. Use it wisely, and you will have elegant XHTML output with no surprises. You need to really know your HTML, which in today&#8217;s web world is a prerequisite anyway.</p>
<p><strong>PRO &#8211; Faster server-side</strong></p>
<p>It&#8217;s hard to get any real performance data about MVC, but it&#8217;s been suggested that it&#8217;s potentially 8000 times faster. Supposedly it&#8217;s due to less processing since it simply processes a &quot;template&quot; instead of having to build a complicated control tree. Even if it&#8217;s twice as fast, or even marginally faster, that would be significant for popular sites, or give at least a slight boost to smaller sites.</p>
<p><strong>PRO &#8211; Simplified model for multiple related views</strong></p>
<p>One thing that I found much easier to do with MVC was to have multiple versions of a page that displayed the same data, but in a slightly different format. For example, on my <a href="http://www.simpletracking.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.simpletracking.com?referer=');">RSS package tracking</a> website, you can look at your tracking information in a full-featured desktop browser, a mobile browser, or an RSS reader. The data being displayed is always the same, but the actual rendered output was different. If I later want to make an iPhone specific version for example, I would simply make a new view, and use an existing controller action.</p>
<p><strong>PRO &#8211; Unit testable</strong></p>
<p>One of the biggest challenges with WebForms was that testing was difficult. Not only was the actual output not easy to test, but the code-behind would tend to be a place that would contain important code that would never get unit tested. With both MVC and WebForms, it&#8217;s best to keep your logic away from the page, but it&#8217;s not always easy or ideal. MVC makes it simple to unit test the logic that is specific to a page. To do so, you simply test the actions in your controllers, which are regular, easy to test classes.</p>
<p><strong>CON &#8211; Difficult to convert an existing site</strong></p>
<p>MVC is not only a framework in this case, but a style. It is possible to convert specific pages as needed, but the cost is high. The problem is that MVC is radically different, so the benefit of converting probably isn&#8217;t worth it for most of your <em>existing</em> pages.</p>
<p>If you decide to convert your site to MVC, you may also run into issues trying to maintain existing page addresses. The specific issue I&#8217;ve ran into is that routes cannot have a trailing slash. If you want to maintain existing URL&#8217;s that have trailing slashes, there is no way to have the built-in routing generate URL&#8217;s with a trailing slash. You may end up losing one of the big advantages that MVC has to offer.</p>
<p><strong>CON &#8211; Not best for SEO out of the box</strong></p>
<p>I&#8217;ve mentioned some of the <a href="http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html" target="_blank">SEO issues</a> before, and all but the trailing slash issue have a reasonable workaround. The routing engine likes to allow multiple addresses to render the same page, instead of enforcing a single address for each page. Luckily, <a href="http://www.hanselman.com/blog/ASPNETMVCAndTheNewIIS7RewriteModule.aspx" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.hanselman.com/blog/ASPNETMVCAndTheNewIIS7RewriteModule.aspx?referer=');">as Scott Hanselman mentions</a>, you can use a URL rewrite engine to bend it to your will. I highly recommend spending some time writing intelligent rules that perform the necessary 301 redirects, because you don&#8217;t want to take chances with SEO (Search Engine Optimization).</p>
<p><strong>CON &#8211; Challenges if you&#8217;re not running IIS7</strong></p>
<p>It&#8217;s clear that the last couple of versions of IIS have been major improvements over their predecessors. IIS7 takes .NET integration to an entirely new level. There is already <a href="http://weblogs.asp.net/omarzabir/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/weblogs.asp.net/omarzabir/archive/2008/06/30/deploy-asp-net-mvc-on-iis-6-solve-404-compression-and-performance-problems.aspx?referer=');">a good page that covers the challenges you&#8217;ll face if you&#8217;re not running IIS6</a>. I&#8217;ll just list them here for brevity:</p>
<ul>
<li>.NET needs to handle all page requests to ensure that the MVC pages will be processed. This leads to bad performance of static files. </li>
<li>HTTP Compression through IIS6 doesn&#8217;t work, because the MVC pages are dynamic. </li>
<li>The homepage gives a 404 when hosted on the root of a domain. </li>
</ul>
<p><strong>Summary</strong></p>
<p>If I needed to build a new site from scratch, and was able to use IIS7, it would be extremely likely that I would choose ASP.NET MVC. It&#8217;s a joy to work with (possibly because it&#8217;s &quot;new&quot;), and just makes sense. If I needed to work with an existing site, I would certainly have to consider the pro&#8217;s and con&#8217;s I mentioned above. ASP.NET MVC gives us an amazing new tool in our huge Microsoft toolbox.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/10/aspnet-mvc-pros-and-cons.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Detecting mobile device user agents in ASP.NET</title>
		<link>http://www.ytechie.com/2008/10/detecting-mobile-device-user-agents-in-aspnet.html</link>
		<comments>http://www.ytechie.com/2008/10/detecting-mobile-device-user-agents-in-aspnet.html#comments</comments>
		<pubDate>Tue, 14 Oct 2008 12:22:29 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/10/detecting-mobile-device-user-agents-in-aspnet.html</guid>
		<description><![CDATA[If you&#8217;re developing a mobile version of your website, usability should be one of your top priorities. Most sites will detect if you&#8217;re using a mobile device, and automatically redirect you to the mobile version. I&#8217;m going to show you how to do this in ASP.NET.
Detecting a mobile device based on user agent
The first issue [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing a mobile version of your website, usability should be one of your top priorities. Most sites will detect if you&#8217;re using a mobile device, and automatically redirect you to the mobile version. I&#8217;m going to show you how to do this in ASP.NET.</p>
<p><strong>Detecting a mobile device based on user agent</strong></p>
<p>The first issue I ran into was coming up with a reliable way to determine if the device is a mobile device based on its user agent. My first urge was to use the detection built into ASP.NET:</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:b0d3e005-fefb-4f13-b3b6-cea039c742fd" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">Request.Browser.IsMobileDevice</pre>
</div>
<p>I immediately lost any trust in this property after it failed to correctly identify Opera Mobile on my Windows Mobile phone. I suspect something similar will happen with the iPhone.</p>
<p>After browsing the <a href="http://www.zytrax.com/tech/web/mobile_ids.html" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.zytrax.com/tech/web/mobile_ids.html?referer=');">dozens of common mobile device user agents</a>, I came up with this method:</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c3b4bae2-2e0f-40ba-957c-d6bc0bc88552" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public static bool IsMobile(string userAgent)
{
	userAgent = userAgent.ToLower();

	return userAgent.Contains("iphone") |
		 userAgent.Contains("ppc") |
		 userAgent.Contains("windows ce") |
		 userAgent.Contains("blackberry") |
		 userAgent.Contains("opera mini") |
		 userAgent.Contains("mobile") |
		 userAgent.Contains("palm") |
		 userAgent.Contains("portable");
}</pre>
</div>
<p>I realize this method isn&#8217;t perfect, but I believe it will correctly identify 99% of the mobile devices out there. You simply pass in the user agent, which is easy to get:</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:0cfecb22-f6ae-42fa-960c-c1597a6dc510" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">Request.UserAgent</pre>
</div>
<p>If you want to be a little more precise, you can come up with your own method by looking at the <a href="http://www.zytrax.com/tech/web/mobile_ids.html" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.zytrax.com/tech/web/mobile_ids.html?referer=');">list of mobile agents</a>, or you can use an updated list of mobile devices with detailed specifications. You can find that type of list <a href="http://wurfl.sourceforge.net/" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/wurfl.sourceforge.net/?referer=');">here</a>.</p>
<p>Have a better way to do it? Have a common device that my method doesn&#8217;t work for? Please let me know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/10/detecting-mobile-device-user-agents-in-aspnet.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Response.Redirect and Output Caching Trouble</title>
		<link>http://www.ytechie.com/2008/10/responseredirect-and-output-caching-trouble.html</link>
		<comments>http://www.ytechie.com/2008/10/responseredirect-and-output-caching-trouble.html#comments</comments>
		<pubDate>Mon, 13 Oct 2008 15:10:54 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/10/responseredirect-and-output-caching-trouble.html</guid>
		<description><![CDATA[I ran into an interesting issue with output caching. If you have a page that uses output caching and that page conditionally sends a redirect response, you need to be careful.

Let&#8217;s say that you have a page that redirects to the mobile version of your website if they have a mobile browser. Let&#8217;s suppose that [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an interesting issue with output caching. If you have a page that uses output caching and that page <strong>conditionally</strong> sends a redirect response, you need to be careful.</p>
<p align="center"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="184" alt="Redirect" src="http://www.ytechie.com/post-images/2008/10/redirect.jpg" width="244" border="0" /></p>
<p>Let&#8217;s say that you have a page that redirects to the mobile version of your website if they have a mobile browser. Let&#8217;s suppose that you&#8217;re using a standard, generic output caching directive:</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c62620a2-51e6-4a82-a112-72d2908d3912" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="xml">&lt;%@ OutputCache Duration="300" VaryByParam="*" %&gt;</pre>
</div>
<p>When the page renders, the rendered HTML will be cached. The next incoming request will get the cached HTML, and the page rendering and code execution will be completely avoided. If you get a visitor that is using a mobile device, <strong>your code to determine if the request should be redirected will never get executed</strong>.</p>
<p>Here is another way to look at this scenario:</p>
<ul>
<li><strong>Request #1:</strong> Page makes the decision to redirect to another page.
<ul>
<li><strong>Result:</strong> Page not cached. </li>
</ul>
</li>
<li><strong>Request #2:</strong> Page makes the decision to render HTML.
<ul>
<li><strong>Result:</strong> This HTML is now rendered and cached. </li>
</ul>
</li>
<li><strong>Request #3: </strong>Doesn&#8217;t matter.
<ul>
<li><strong>Result:</strong> The cached HTML from request #2 is rendered, even if the page should be redirecting. </li>
</ul>
</li>
</ul>
<p>There are a couple of workarounds to keep your redirects working:</p>
<p><strong>Turn off caching</strong></p>
<p>Of course the simplest option is to simply turn of output caching for the page that needs to perform a conditional redirect. It goes without saying that you&#8217;ll lose all the wonderful advantages of output caching.</p>
<p><strong>Use a caching seed</strong></p>
<p>Another option is to write some code in your applications &quot;<a href="http://msdn.microsoft.com/en-us/library/system.web.httpapplication.getvarybycustomstring.aspx" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/system.web.httpapplication.getvarybycustomstring.aspx?referer=');">GetVaryByCustomString</a>&quot; method that determines if it&#8217;s a request that will be redirected. If so, a unique value is returned. This forces the page to re-render because it is not found in the page cache. Unfortunately, the code in your custom string method may get complicated very quickly.</p>
<p>If you&#8217;re lucky, you&#8217;ll be making the redirect decision based on something that can be automatically used to vary the output caching. For example, if your page redirects based on a URL parameter, you can simply use the VaryByParam=&quot;*&quot; in your output cache directive (or specify the specific parameter). Keep in mind that each set of parameters supplied to the page will result in a separate cache entry. This could lead to excessive memory usage for some pages.</p>
<p><strong>Use fragment caching</strong></p>
<p>Of course you could avoid using output caching on the page itself. You simply use the page as a place for your redirect logic, not the content generation. Then, put the actual page generation logic and content into a UserControl. When you use output caching on that control, it won&#8217;t affect the redirect logic in the page. The disadvantage of this technique is that you may have to do extra work separating your content into controls. You also won&#8217;t get the full benefit of caching since some of the control tree still needs to be processed, and some parts of the page such as the MasterPage will not get cached.</p>
<p><strong>Conclusion</strong></p>
<p>Remember, this is only an issue if the page being cached makes a decision to redirect. If the page always performs a redirect, the page will not be cached (why are you using output caching then?). Obviously this issue isn&#8217;t unique to Response.Redirect, but it is an issue you should be aware of.</p>
<p>One mischievous aspect of this issue is that if you test your site in a certain order, you won&#8217;t see a problem. For example, if you first view the page under the circumstances needed to make it redirect, it will work. You&#8217;ll only see the issue if you cause it to <em>not</em> redirect, and <em>then</em> try to get it to redirect.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/10/responseredirect-and-output-caching-trouble.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Removing duplicate page addresses in MVC</title>
		<link>http://www.ytechie.com/2008/10/removing-duplicate-page-addresses-in-mvc.html</link>
		<comments>http://www.ytechie.com/2008/10/removing-duplicate-page-addresses-in-mvc.html#comments</comments>
		<pubDate>Tue, 07 Oct 2008 15:13:29 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/10/removing-duplicate-page-addresses-in-mvc.html</guid>
		<description><![CDATA[As I mentioned before, there are a couple of SEO issues with MVC. I&#8217;ll discuss two ways to get around the trailing slash issue. Recall what the issue was. MVC will happily serve up your URL&#8217;s both with and without a trailing slash:

www.test.com/path 
www.test.com/path/ 

Search engines could potentially index both paths, and count them separately [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned before, there are a couple of SEO issues with MVC. I&#8217;ll discuss two ways to get around the trailing slash issue. <a href="http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html" target="_blank">Recall</a> what the issue was. MVC will happily serve up your URL&#8217;s both with and without a trailing slash:</p>
<ul>
<li>www.test.com/path </li>
<li>www.test.com/path/ </li>
</ul>
<p>Search engines could potentially index both paths, and count them separately in their search results. The first solution is to make one of the versions return a 404 (page not found) response code. Hopefully, this would deter anyone from linking to the non-preferred version. We can do this by creating a custom route constraint, by implementing the <strong>IRouteConstraint</strong> interface:</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:f7d5a192-5e34-47f9-a6f8-3f132961732d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public class TrailingSlashConstraint : IRouteConstraint
{
	private readonly bool _trailingSlash;

	public TrailingSlashConstraint(bool trailingSlash)
	{
		_trailingSlash = trailingSlash;
	}

	public bool Match(HttpContextBase httpContext, Route route, string parameterName,
		RouteValueDictionary values, RouteDirection routeDirection)
	{
		return _trailingSlash == httpContext.Request.Url.LocalPath.EndsWith("/");
	}
}</pre>
</div>
<p>Then, when you define the route, supply an instance of the constraint:</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:ccf04113-0099-4c73-ad6b-5120c382c1a4" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">routes.MapRoute("mobileVersion", "m",
	new { controller = "home", action = "mobile", id = "" },
	new TrailingSlashConstraint(false));</pre>
</div>
<p>The second solution is to redirect the non-preferred version to the preferred version. We can do this by either creating a custom HTTP Module, or by using a canned product like the free <a href="http://urlrewriter.net/" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/urlrewriter.net/?referer=');">UrlRewriter.NET</a>.</p>
<p><strong>Should I use a trailing slash or not?</strong></p>
<p>This is one of the problems with MVC. In ASP.NET webforms, you could use a default page to avoid having to specify a file in the URL. This gets you the nice MVC style paths. The good part was that if someone accessed the version without a trailing slash, it would automatically redirect to the version with the trailing slash. So in ASP.NET WebForms, the trailing slash was obviously preferred since it was the default behavior.</p>
<p>Now, in ASP.NET MVC, they decided to make the default behavior the <strong>opposite</strong>. It does serve up both versions as I mentioned, but when it <em>generates</em> links, they don&#8217;t have a trailing slash.</p>
<p>If you&#8217;re long term solution is to use MVC, I recommend dropping the trailing slashes, just to avoid the pain. I&#8217;m really hoping that by the time the final version of MVC comes out, they&#8217;ll give us an option to define routes that should have a trailing slash.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/10/removing-duplicate-page-addresses-in-mvc.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC, What about SEO?</title>
		<link>http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html</link>
		<comments>http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html#comments</comments>
		<pubDate>Wed, 01 Oct 2008 20:26:51 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html</guid>
		<description><![CDATA[I&#8217;ve started working the the latest preview of the ASP.NET MVC framework. I&#8217;m completely converting one of my sites, because learning by doing is typically the best way. Unfortunately, I&#8217;ve run into some alarming SEO (Search Engine Optimization) issues with this new paradigm (or more specifically, the Microsoft implementation).

Duplicate Content
Duplicate content is a major issue. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started working the the latest preview of the ASP.NET MVC framework. I&#8217;m completely converting one of my sites, because learning by <em>doing </em>is typically the best way. Unfortunately, I&#8217;ve run into some alarming SEO (Search Engine Optimization) issues with this new paradigm (or more specifically, the Microsoft implementation).</p>
<p align="center"><a href="http://www.ytechie.com/post-images/2008/10/istock-000006003382xsmall.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://www.ytechie.com/post-images/2008/10/istock-000006003382xsmall-thumb.jpg" border="0" alt="Duplicate Ducks!" width="184" height="244" /></a></p>
<p><strong>Duplicate Content</strong></p>
<p><a rel="nofollow" href="http://www.webconfs.com/duplicate-content-filter-article-1.php" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.webconfs.com/duplicate-content-filter-article-1.php?referer=');">Duplicate content</a> is a major issue. If a search engine (Google, which we&#8217;re primarily concerned with) finds multiple identical pages, it could be seen as a spam technique. Google likes original content, and penalizes duplicate content.</p>
<p>The problem is that the ASP.NET MVC default routing is too forgiving. If I have a page with this address: &#8220;/controller/action/id&#8221;, the routing engine happily serves it up at &#8220;/controller/action/id/&#8221;. There is no reason to not be strict on this. In ASP.NET WebForms, if you forget the trailing slash, it will automatically perform a 301 (permanent) redirect to the version with the trailing slash.</p>
<p>ASP.NET MVC has a bug (I&#8217;m calling it that) that won&#8217;t let you define a URL as requiring a trailing slash. Below, I&#8217;ve defined a route as a sample. In the URL to match, I have a trailing slash. In the routing code, the trailing slash is removed when it&#8217;s added to the routing table. This also has the side-effect of <em>generating</em> the URL&#8217;s <em>without</em> a trailing slash.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:8f95410d-9fa2-4209-88c4-ede04eecc088" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="c#">routes.MapRoute(
	"Legacy-Firefox",
	"Firefox-Extension/",
	new { controller = "Home", action = "Firefox", id = "" } );</pre>
</div>
<p>Since routes can be configured to reuse actions and controllers, it makes more paths to follow. If I again use the route defined above, I end up with all of these valid addresses that could potentially be linked to and indexed:</p>
<ul>
<li>a.com/Firefox-Extension</li>
<li>a.com/Firefox-Extension/</li>
<li>a.com/Home/Firefox</li>
<li>a.com/Home/Firefox/</li>
<li>a.com/Home/Firefox/anythingyouwant</li>
</ul>
<p><strong>If you&#8217;re lucky,</strong> Google won&#8217;t penalize duplicate content. However, if Google indexes the same content using multiple URL&#8217;s, you won&#8217;t get the benefit of focusing the PageRank. A similar situation occurs with you have a site that can be addressed like &#8220;cnn.com&#8221; and &#8220;www.cnn.com&#8221;. They counted as separate pages, that end up competing for good rank.</p>
<p><strong>Legacy URL&#8217;s</strong></p>
<p>I&#8217;m sure there is a large group of people that are anxiously awaiting to take advantage of the new MVC style development. Many of them will undoubtedly have existing URL&#8217;s they want to preserve.</p>
<p>There are a couple of ways to handle this issue. The search engines would prefer that your URL&#8217;s simply remain the same. This is possible, but requires some fancy routing. The SEO community highly recommends this approach (with good cause).</p>
<p>Another way to handle it is to adopt the new REST style URL&#8217;s that typically make the most sense with an MVC approach: &#8220;/controller/action/id&#8221;. Then, setup 301 redirects from the old addresses to the new one&#8217;s. <a rel="nofollow" href="http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx?referer=');">This article</a> discusses the technical details. In theory, this should be the best scenario. However, Google themselves say to get the incoming links pointed to the new addresses ASAP. The truth is, this solution sucks. I&#8217;ve actually done this with a site. It was search engine suicide for a couple of months. I eventually got my old position back, <strong>but lost a significant amount of revenue because of it</strong>.</p>
<p>Yet another way that I found is to set up multiple routes so that the content is accessible with both the old and new addresses. If you&#8217;ve been paying attention, you&#8217;ll know that this counts as duplicate content, and is <strong>very, very bad</strong>. I was in shock when <a rel="nofollow" href="http://www.dimecasts.net/Casts/CastDetails/11" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.dimecasts.net/Casts/CastDetails/11?referer=');">I found this approach being advocated</a>.</p>
<p><strong>Conclusion</strong></p>
<p>I&#8217;m not saying the routing system is completely wrong, I just think it would be set up so that the easy way of migrating a site is the correct way, or as close to it as possible. I don&#8217;t want to have to write custom routing. At the very least, come up with a way to designate that a particular action has a single path (and cancel out additional paths in other routes). It would also be nice if there was a way to use the old style urlMapping section in the web.config for legacy URL&#8217;s.</p>
<p>If I&#8217;m completely wrong about how the routing works, let me know. It&#8217;s difficult to find good information (which is understandable right now), and I&#8217;m admittedly still in an early learning stage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>ASP.NET Changing Session ID&#8217;s for each request</title>
		<link>http://www.ytechie.com/2008/07/aspnet-changing-session-ids-for-each-request.html</link>
		<comments>http://www.ytechie.com/2008/07/aspnet-changing-session-ids-for-each-request.html#comments</comments>
		<pubDate>Wed, 30 Jul 2008 19:40:54 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/07/aspnet-changing-session-ids-for-each-request.html</guid>
		<description><![CDATA[I ran into an issue where ASP.NET was changing the Session.SessionId for every request from the same user. A quick Google search revealed 2.3 million pages. I&#8217;ll summarize one of the main reasons this can happen, and discuss 2 ways to fix it.
 
I&#8217;ve been working on a search function for a website I&#8217;m working [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an issue where ASP.NET was changing the Session.SessionId for every request from the same user. A quick Google search revealed 2.3 million pages. I&#8217;ll summarize one of the main reasons this can happen, and discuss 2 ways to fix it.</p>
<p><a href="http://www.ytechie.com/post-images/2008/07/hand-counting.jpg"><img height="97" alt="Hand-Counting" src="http://www.ytechie.com/post-images/2008/07/hand-counting-thumb.jpg" width="340" border="0" /></a> </p>
<p>I&#8217;ve been working on a search function for a website I&#8217;m working on. We&#8217;re taking the <a href="http://en.wikipedia.org/wiki/Lean_software_development" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Lean_software_development?referer=');">Lean software approach</a> and implementing an extremely basic search for now. We&#8217;re going to track the searches that users are making, and will have the data we&#8217;ll need to make a better search in the next version.</p>
<p>In order to know if users are making multiple searches, we&#8217;re storing the ASP.NET session ID with the search record in the database. Much to my dismay, every search request resulted in a different value in <em>Session.SessionId</em>.</p>
<p>The problem lies in the fact that ASP.NET is trying to be extremely efficient storing sessions for users. <strong>If ASP.NET doesn&#8217;t have a reason to remember who you are, it won&#8217;t</strong>. If you think about it, that can save a tremendous amount of work by avoiding session management.</p>
<p>If you want to tell ASP.NET that you want it to track user sessions, you can do one of 2 things:</p>
<ol>
<li>Store something in the session. If you store something in the users session, ASP.NET will be forced to associate that data with your current visit. Example code:
<pre class="c#" name="code">Session[&quot;foo&quot;] = &quot;bar&quot;;</pre>
</li>
<li>Simply by handling the <em>Session_Start</em> event in your <em>Global.asax</em>. The presence of this method will tell ASP.NET to track sessions, even if there is no data in the session.
<pre class="c#" name="code">public void Session_Start(object sender, EventArgs e)
{
}</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/07/aspnet-changing-session-ids-for-each-request.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Locking sessions for multi-threaded access</title>
		<link>http://www.ytechie.com/2008/07/locking-sessions-for-multi-threaded-access.html</link>
		<comments>http://www.ytechie.com/2008/07/locking-sessions-for-multi-threaded-access.html#comments</comments>
		<pubDate>Thu, 10 Jul 2008 16:49:48 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/07/locking-sessions-for-multi-threaded-access.html</guid>
		<description><![CDATA[I recently ran into a situation where I needed to upload some small files from a Flex client application to an ASP.NET web server. I decided to store the uploaded files in the users session while they were in the checkout process. Once the user confirms their order, the images are read from the session [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a situation where I needed to upload some small files from a Flex client application to an ASP.NET web server. I decided to store the uploaded files in the users session while they were in the checkout process. Once the user confirms their order, the images are read from the session and stored to the database.</p>
<p>Here is the original code from the page that accepts each uploaded file, and adds it to a Dictionary in the collection:</p>
<pre class="c#" name="code">if (Session[SESSION_ORDER_FILES] == null)
{
	//Our dictionary hasn't been created, so we do it now
	files = new Dictionary&lt;string, byte[]&gt;();
	Session[SESSION_ORDER_FILES] = files;
}
else
{
	//The dictionary has already been created, just load it
	files = (Dictionary&lt;string , byte[]&gt;) Session[SESSION_ORDER_FILES];
}

//If we have the &quot;_clearPrevious&quot; flag, that means all
//of the files should be removed from this users session
if (_clearPrevious)
	files.Clear();

//If the file name is the same, replace it
if (files.ContainsKey(_fileName))
	files.Remove(_fileName);

files.Add(_fileName, bytes);</pre>
<p>The problem is that we ended up with missing images. The client was sending them, but when the user confirmed their order they were missing images in the session. Since ASP.NET will process page requests in multiple threads, <strong>the session can be accessed in multiple threads</strong>!</p>
<p>Now, we need to find a way to lock them. I questioned whether ASP.NET would give me the same session object each time, or a new instance representing the same session. I whipped up this code in a test page. It saves the previous session reference to the session. I know it&#8217;s a little strange, but since no serialization happens with the session, it gave me a good way to know if the previous session object and the current session object were <strong>the same instance</strong>.</p>
<pre class="c#" name="code">const string SESS_SESS = &quot;test&quot;;
var currSessionObj = Session[SESS_SESS];

if(currSessionObj == null)
	//First page load
	Session[SESS_SESS] = Session;
else
	lblText.Text = (Session[SESS_SESS] == Session).ToString();</pre>
<p>The result of this page was <strong>false</strong>. That means you most certainly do <strong>get a new session instance each time</strong>. Keep in mind that I&#8217;m not saying it&#8217;s a different session, the object you&#8217;re accessing the session with simply changes.</p>
<p><strong>What does this mean?</strong></p>
<p>This means that you have to be careful when there is a chance that you&#8217;re working with session objects in multiple pages, or in a page that could be accessed multiple times simultaneously. Thankfully, there are only a few real-world scenarios where this would be a large concern.</p>
<p>As with any other kind of multi-threaded code, be careful if you&#8217;re checking the session, and then performing an action based on the result. In that case, you&#8217;ll need to lock a global object that is available to all threads that could access that code. Here is an example:</p>
<pre class="c#" name="code">lock(Global.SessionLock)
{
	if(Session[&quot;foo&quot;] == null)
		Session[&quot;foo&quot;] = new Bar();
}</pre>
<p>In your Global class, you&#8217;ll need this field: </p>
<pre class="c#" name="code">static object SessionLock = new object();</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/07/locking-sessions-for-multi-threaded-access.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object does not match target type in GridView</title>
		<link>http://www.ytechie.com/2008/05/object-does-not-match-target-type-in-gridview.html</link>
		<comments>http://www.ytechie.com/2008/05/object-does-not-match-target-type-in-gridview.html#comments</comments>
		<pubDate>Fri, 16 May 2008 19:33:47 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/05/object-does-not-match-target-type-in-gridview.html</guid>
		<description><![CDATA[I created a shopping cart for a website that can display multiple types of items that implement IShoppingCartItem. When the GridView would display items that were different type, I would get this exception:
Exception Details: System.Reflection.TargetException: Object does not match target type.
I found a lot of solutions that I didn&#8217;t really like. For example, every shopping [...]]]></description>
			<content:encoded><![CDATA[<p>I created a shopping cart for a website that can display multiple types of items that implement IShoppingCartItem. When the GridView would display items that were different type, I would get this exception:</p>
<p><strong>Exception Details: System.Reflection.TargetException: Object does not match target type.</strong></p>
<p>I found a lot of solutions that I didn&#8217;t really like. For example, every shopping cart item type could implement <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.itypedlist.aspx" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/system.componentmodel.itypedlist.aspx?referer=');">ITypedList</a>.</p>
<p>What I ended up doing was creating a CartGridItem class that implements IShoppingCartItem:</p>
<pre class="c-sharp" name="code">public class CartGridItem : IShoppingCartItem
{
	private readonly IShoppingCartItem _baseCartItem;
	public CartGridItem(IShoppingCartItem baseCartItem)
	{
		_baseCartItem = baseCartItem;
	}
	#region IShoppingCartItem Members
	public string ProductCode
	{
		get { return _baseCartItem.ProductCode; }
	}
//...end of code sample...</pre>
<p>This has worked great, and I don&#8217;t have to make any changes when I create a new item that implements that interface!</p>
<p align="center"><img height="122" alt="image" src="http://www.ytechie.com/post-images/2008/05/image6.png" width="442" border="0" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/05/object-does-not-match-target-type-in-gridview.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
