<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Taswar Bhatti Blog</title>
	
	<link>http://taswar.zeytinsoft.com</link>
	<description>The synonyms of software simplicity</description>
	<lastBuildDate>Thu, 08 Jul 2010 15:03:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TaswarBhattiBlog" /><feedburner:info uri="taswarbhattiblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Javascript http post data to new window or pop up</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/9JvsudtQ9u8/</link>
		<comments>http://taswar.zeytinsoft.com/2010/07/08/javascript-http-post-data-to-new-window-or-pop-up/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 15:03:52 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[ASP .NET]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=804</guid>
		<description><![CDATA[So was going through some legacy code to fix some security issues. One of them was there were links that were passing the data on url request. e.g NewFile.aspx?uid=1234 Rather than storing data in a session sometimes developers use shortcuts to do this, could be due to the pressure or time limit we have in [...]]]></description>
			<content:encoded><![CDATA[<p>So was going through some legacy code to fix some security issues. One of them was there were links that were passing the data on url request. e.g <strong>NewFile.aspx?uid=1234</strong></p>
<p>Rather than storing data in a session sometimes developers use shortcuts to do this, could be due to the pressure or time limit we have in shipping a product.</p>
<p>Aside from that lets see how we can fix this issue, what we want to accomplish is to post some data without calling server code and we can achieve that by some tricks in javascript.</p>
<p>Lets say you have a link that will say <strong>&lt;a onclick=&#8221;javascript:NewFile()&#8221;&gt;New File&lt;/a&gt;</strong><br />
(<em>Note I know this is not good again its legacy code</em>)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p804code3'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8043"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p804code3"><pre class="javascript" style="font-family:monospace;">   <span style="color: #003366; font-weight: bold;">function</span> NewFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;NewFile.aspx?uid=1234&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;width=800,height=600,left=100,top=100,resizable=yes,scrollbars=yes&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now we want to make a post request to the window and pass in the data.<br />
Here is how we do it, I created a blank html page first and used this javascript.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p804code4'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8044"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code" id="p804code4"><pre class="javascript" style="font-family:monospace;">   <span style="color: #003366; font-weight: bold;">function</span> OpenWindowWithPost<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> windowoption<span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> params<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;post&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">,</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;target&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> params<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>params.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> input <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    input.<span style="color: #660066;">type</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
                    input.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
                    input.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> params<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    form.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">//note I am using a post.htm page since I did not want to make double request to the page </span>
           <span style="color: #006600; font-style: italic;">//it might have some Page_Load call which might screw things up.</span>
            window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;post.htm&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> windowoption<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            document.<span style="color: #660066;">body</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #003366; font-weight: bold;">function</span> NewFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #003366; font-weight: bold;">var</span> param <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'uid'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'1234'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>		    		
      OpenWindowWithPost<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;NewFile.aspx&quot;</span><span style="color: #339933;">,</span> 
      <span style="color: #3366CC;">&quot;width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes&quot;</span><span style="color: #339933;">,</span> 
      <span style="color: #3366CC;">&quot;NewFile&quot;</span><span style="color: #339933;">,</span> param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>By doing so we can pass in the data to the <strong>NewFile.aspx</strong> page with a post request now, also note if you are using <strong>Request.QueryString['uid']</strong> in the <strong>NewFile.aspx</strong> page you will need to change it to <strong>Request['uid']</strong></p>
<p>Hope this helps <img src='http://taswar.zeytinsoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F07%2F08%2Fjavascript-http-post-data-to-new-window-or-pop-up%2F&amp;linkname=Javascript%20http%20post%20data%20to%20new%20window%20or%20pop%20up"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/9JvsudtQ9u8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/07/08/javascript-http-post-data-to-new-window-or-pop-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/07/08/javascript-http-post-data-to-new-window-or-pop-up/</feedburner:origLink></item>
		<item>
		<title>Stubbing out ServiceLocator for unit testing</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/j4NjbeE--kc/</link>
		<comments>http://taswar.zeytinsoft.com/2010/06/30/stubbing-out-servicelocator-for-unit-testing/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:12:00 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[aspmvc]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=797</guid>
		<description><![CDATA[There are certainly many ways to stub out dependency, using constructor injection, etc etc. Here is one way to quickly stub out ServiceLocator rather than implementing ServiceLocatorStub. Lets say you have code that uses a helper and the dependency is not quite the responsibility of that object but its the sub object that uses it. [...]]]></description>
			<content:encoded><![CDATA[<p>There are certainly many ways to stub out dependency, using constructor injection, etc etc. Here is one way to quickly stub out ServiceLocator rather than implementing <a href="http://jfromaniello.blogspot.com/2010/02/testing-code-that-use-servicelocator.html">ServiceLocatorStub</a>.</p>
<p>Lets say you have code that uses a helper and the dependency is not quite the responsibility of that object but its the sub object that uses it. As an example the IAccessHelper uses some security server to check some values. But the AccountController doesnt use it so why inject security service in AccountController? In that case you will call a service locator to get you the AccessHelper object.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p797code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7978"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p797code8"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AccountController<span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>    
     <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
          var accessHelper <span style="color: #008000;">=</span> ServiceLocator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>IAccessHelper<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
           <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>accessHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">IsValid</span><span style="color: #008000;">&#40;</span>user<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//do something</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
Now if you wish to unit test this you will need to do quite a few things to stub it out. The quick way to do this is using property in C#</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p797code9'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7979"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p797code9"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AccountController<span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>    
     <span style="color: #0600FF; font-weight: bold;">public</span> boolean DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
&nbsp;
           <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>AccessHelperObject<span style="color: #008000;">.</span><span style="color: #0000FF;">IsValid</span><span style="color: #008000;">&#40;</span>user<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//do something</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> truel
            <span style="color: #008000;">&#125;</span>
&nbsp;
           <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF; font-weight: bold;">private</span> IAccessHelper _accessHelper<span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">public</span> IAccessHelper AccessHelperObject 
     <span style="color: #008000;">&#123;</span>
       get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _accessHelper <span style="color: #008000;">??</span> ServiceLocator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>IAccessHelper<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
       set <span style="color: #008000;">&#123;</span> _accessHelper <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
So that now when you write your unit test you dont have to care about service locator you can just stub out your IAccessHelper by just passing in a mock or stub to it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p797code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79710"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p797code10"><pre class="csharp" style="font-family:monospace;">  <span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Account_DoSomething<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      var account <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AccoutController<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      var stubAccess <span style="color: #008000;">=</span> MockRepository<span style="color: #008000;">.</span><span style="color: #0000FF;">GenerateStub</span><span style="color: #008000;">&lt;</span>IAccessHelper<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      stubAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Expect</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVaid</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Return</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      account<span style="color: #008000;">.</span><span style="color: #0000FF;">AccessHelperObject</span> <span style="color: #008000;">=</span> stubAccess<span style="color: #008000;">;</span>
      var wasCalled <span style="color: #008000;">=</span> account<span style="color: #008000;">.</span><span style="color: #0000FF;">DoSomething</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">That</span><span style="color: #008000;">&#40;</span>wasCalled, <a href="http://www.google.com/search?q=is+msdn.microsoft.com"><span style="color: #008000;">Is</span></a><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">True</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></p>
<p>This helps one to quickly break out the dependency of an object, that would otherwise take too many lines of code to test out a simple idea.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F06%2F30%2Fstubbing-out-servicelocator-for-unit-testing%2F&amp;linkname=Stubbing%20out%20ServiceLocator%20for%20unit%20testing"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/j4NjbeE--kc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/06/30/stubbing-out-servicelocator-for-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/06/30/stubbing-out-servicelocator-for-unit-testing/</feedburner:origLink></item>
		<item>
		<title>Json A circular reference was detected while serializing an object of type … in my asp mvc app</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/W2IwLRjuAEk/</link>
		<comments>http://taswar.zeytinsoft.com/2010/06/09/json-a-circular-reference-was-detected-while-serializing-an-object-of-type-in-my-asp-mvc-app/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 15:54:03 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[aspmvc]]></category>
		<category><![CDATA[asp mvc]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[serialization]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=790</guid>
		<description><![CDATA[I found this weird error when I was testing a no trivial test case in my app and was not able to find out what was wrong with it. It appears like a Json error in my asp mvc app, although the stack really doesn&#8217;t help ?View Code CSHARP1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p>I found this weird error when I was testing a no trivial test case in my app and was not able to find out what was wrong with it.<br />
It appears like a Json error in my asp mvc app, although the stack really doesn&#8217;t help <img src='http://taswar.zeytinsoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p790code13'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79013"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p790code13"><pre class="csharp" style="font-family:monospace;">ystem<span style="color: #008000;">.</span><span style="color: #0000FF;">InvalidOperationException</span><span style="color: #008000;">:</span> A circular reference was detected <span style="color: #0600FF; font-weight: bold;">while</span> serializing an <span style="color: #6666cc; font-weight: bold;">object</span> of type <span style="color: #666666;">'TermBaseMvc.ViewModels.Udf.UdfValueViewModel'</span><span style="color: #008000;">.</span>
   <span style="color: #0000FF;">at</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeEnumerable</span><span style="color: #008000;">&#40;</span>IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeCustomObject</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeEnumerable</span><span style="color: #008000;">&#40;</span>IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeCustomObject</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeEnumerable</span><span style="color: #008000;">&#40;</span>IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeCustomObject</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeEnumerable</span><span style="color: #008000;">&#40;</span>IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValueInternal</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SerializeValue</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> obj, StringBuilder output, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> obj, SerializationFormat serializationFormat<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Script</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JavaScriptSerializer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span> obj<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">JsonResult</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ExecuteResult</span><span style="color: #008000;">&#40;</span>ControllerContext context<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeActionResult</span><span style="color: #008000;">&#40;</span>ControllerContext controllerContext, ActionResult actionResult<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass14<span style="color: #008000;">.&lt;</span>InvokeActionResultWithFilters<span style="color: #008000;">&gt;</span>b__11<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeActionResultFilter</span><span style="color: #008000;">&#40;</span>IResultFilter filter, ResultExecutingContext preContext, Func`<span style="color: #FF0000;">1</span> continuation<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass14<span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass16<span style="color: #008000;">.&lt;</span>InvokeActionResultWithFilters<span style="color: #008000;">&gt;</span>b__13<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeActionResultFilter</span><span style="color: #008000;">&#40;</span>IResultFilter filter, ResultExecutingContext preContext, Func`<span style="color: #FF0000;">1</span> continuation<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass14<span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass16<span style="color: #008000;">.&lt;</span>InvokeActionResultWithFilters<span style="color: #008000;">&gt;</span>b__13<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeActionResultFilter</span><span style="color: #008000;">&#40;</span>IResultFilter filter, ResultExecutingContext preContext, Func`<span style="color: #FF0000;">1</span> continuation<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass14<span style="color: #008000;">.&lt;&gt;</span>c__DisplayClass16<span style="color: #008000;">.&lt;</span>InvokeActionResultWithFilters<span style="color: #008000;">&gt;</span>b__13<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeActionResultWithFilters</span><span style="color: #008000;">&#40;</span>ControllerContext controllerContext, IList`<span style="color: #FF0000;">1</span> filters, ActionResult actionResult<span style="color: #008000;">&#41;</span>
   at <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mvc</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerActionInvoker</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeAction</span><span style="color: #008000;">&#40;</span>ControllerContext controllerContext, <span style="color: #6666cc; font-weight: bold;">String</span> actionName<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p>
Basically what is happening is Json does not like to have circular reference ie. A -> B -> A<br />
The easiest fix is to use <strong>[ScriptIgnore]</strong> in your ViewModel to tell it not to serialize object that may cause such a circulate issue.</p>
<p>Add the System.web.extensions reference to your viewmodel and make the elements that may cuase issue to scriptIgnore</p>
<p>example</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p790code14'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p790code14"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Script.Serialization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> UserViewModel
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>ScriptIgnore<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span> ManagerOf <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></p>
<p>By doing so now your json would look more like <strong>{ Id: 3, FirstName: &#8216;Taswar&#8217; }</strong>.<br />
Also there is <a href="http://http://james.newtonking.com/pages/json-net.aspx">JSON.NET</a> that provides <strong>[JsonIgnore]</strong> attribute if you are using json .net</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F06%2F09%2Fjson-a-circular-reference-was-detected-while-serializing-an-object-of-type-in-my-asp-mvc-app%2F&amp;linkname=Json%20A%20circular%20reference%20was%20detected%20while%20serializing%20an%20object%20of%20type%20%26%238230%3B%20in%20my%20asp%20mvc%20app"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/W2IwLRjuAEk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/06/09/json-a-circular-reference-was-detected-while-serializing-an-object-of-type-in-my-asp-mvc-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/06/09/json-a-circular-reference-was-detected-while-serializing-an-object-of-type-in-my-asp-mvc-app/</feedburner:origLink></item>
		<item>
		<title>JQuery print part of div only</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/9VXnLEPemew/</link>
		<comments>http://taswar.zeytinsoft.com/2010/05/19/jquery-print-part-of-div-only/#comments</comments>
		<pubDate>Wed, 19 May 2010 16:38:07 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[print div]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=784</guid>
		<description><![CDATA[So here is an interesting jquery plugin called jqprint it allows one to specify any element and sent it to print. Assuming you have an element with id printButton and a content area called divToPrint ?View Code JAVASCRIPT1 2 3 $&#40;'#printButton'&#41;.live&#40;'click', function&#40;&#41; &#123; $&#40;'#divToPrint'&#41;.jqprint&#40;&#41;; &#125;&#41;; This will pop up the print functionality in your browser [...]]]></description>
			<content:encoded><![CDATA[<p>So here is an interesting jquery plugin called <a href="http://www.recoding.it/?p=138">jqprint</a> it allows one to specify any element and sent it to print.</p>
<p>Assuming you have an element with id printButton and a content area called divToPrint</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code17'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78417"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p784code17"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#printButton'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#divToPrint'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jqprint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will pop up the print functionality in your browser and send it with the default media=&#8221;print&#8221; css or else it picks up your default css. Kind of neat.</p>
<p>One thing to be mindful of in <strong>Firefox</strong> is the <strong>overflow attribute</strong> in your css stylesheet if you have <strong>overflow: hidden</strong> you will only <strong>get one page to print only</strong>.</p>
<p>Example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code18'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78418"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p784code18"><pre class="css" style="font-family:monospace;"> body <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Remove the overflow and it would print all the pages of a div.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F05%2F19%2Fjquery-print-part-of-div-only%2F&amp;linkname=JQuery%20print%20part%20of%20div%20only"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/9VXnLEPemew" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/05/19/jquery-print-part-of-div-only/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/05/19/jquery-print-part-of-div-only/</feedburner:origLink></item>
		<item>
		<title>JQuery get ajax call not working in IE</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/l3ox7S6CjEc/</link>
		<comments>http://taswar.zeytinsoft.com/2010/05/05/jquery-get-ajax-call-not-working-in-ie/#comments</comments>
		<pubDate>Wed, 05 May 2010 14:27:55 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=776</guid>
		<description><![CDATA[So for some reason IE just loves to cache things for you when you call an ajax method. Something like ?View Code JAVASCRIPT1 2 3 4 5 &#160; $&#40;'#mybutton'&#41;.live&#40;'click', function&#40;&#41; &#123; $.post&#40;'SaveLanguage', &#123; lang : $&#40;this&#41;.val&#40;&#41;, null, null&#41;; $.get&#40;'GetLanguage', null, function&#40;html&#41; &#123; $&#40;'#myDiv'&#41;.replaceWith&#40;html&#41;; &#125;&#41;; &#125;&#41;; This will work every time in firefox but IE will [...]]]></description>
			<content:encoded><![CDATA[<p>So for some reason IE just loves to cache things for you when you call an ajax method.</p>
<p>Something like</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p776code21'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p77621"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p776code21"><pre class="javascript" style="font-family:monospace;">&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mybutton'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SaveLanguage'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> lang <span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GetLanguage'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replaceWith</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will work every time in firefox but IE will just call the cache version, thus whatever you have changed in the post version would not matter.</p>
<p>To fix this one can use datetime to append to the query string in each call or just use jquery ajaxSetup like</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p776code22'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p77622"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p776code22"><pre class="javascript" style="font-family:monospace;">&nbsp;
$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mybutton'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SaveLanguage'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> lang <span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GetLanguage'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replaceWith</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>or use the $.ajax call and pass in cache: false.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F05%2F05%2Fjquery-get-ajax-call-not-working-in-ie%2F&amp;linkname=JQuery%20get%20ajax%20call%20not%20working%20in%20IE"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/l3ox7S6CjEc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/05/05/jquery-get-ajax-call-not-working-in-ie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/05/05/jquery-get-ajax-call-not-working-in-ie/</feedburner:origLink></item>
		<item>
		<title>JQuery live ‘change’ doesn’t work in IE</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/xtXri4Xk6gA/</link>
		<comments>http://taswar.zeytinsoft.com/2010/05/04/jquery-live-change-doesnt-work-in-ie/#comments</comments>
		<pubDate>Tue, 04 May 2010 15:24:23 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[live event]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=767</guid>
		<description><![CDATA[So was having issues as in why does my checkbox doesn&#8217;t work in IE when I do ?View Code JAVASCRIPT1 2 3 $&#40;'.checkbox'&#41;.live&#40;'change', function&#40;&#41; &#123; alert&#40;'Checkbox clicked'&#41;; &#125;&#41;; And found out that jquery does not bind live &#8220;change&#8221; events, I was told that it works in 1.4.2 but for some reason I still was not [...]]]></description>
			<content:encoded><![CDATA[<p>So was having issues as in why does my checkbox doesn&#8217;t work in IE when I do</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p767code26'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p76726"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p767code26"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.checkbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Checkbox clicked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And found out that jquery does not bind live &#8220;change&#8221; events, I was told that it works in 1.4.2 but for some reason I still was not able to get it working.</p>
<p>The solution that I found was to use the <a href="http://github.com/brandonaaron/livequery">livequery</a> or bind method thus the code would become</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p767code27'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p76727"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p767code27"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.checkbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Checkbox clicked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>or if you are using Ajax then</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p767code28'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p76728"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p767code28"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.checkbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Checkbox clicked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That should do the trick, remember to add jquery.livequery.js if you are using livequery in your file.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F05%2F04%2Fjquery-live-change-doesnt-work-in-ie%2F&amp;linkname=JQuery%20live%20%26%238216%3Bchange%26%238217%3B%20doesn%26%238217%3Bt%20work%20in%20IE"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/xtXri4Xk6gA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/05/04/jquery-live-change-doesnt-work-in-ie/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/05/04/jquery-live-change-doesnt-work-in-ie/</feedburner:origLink></item>
		<item>
		<title>ASP .NET MVC2 Changes: GetControllerInstance &amp; DataAnnotationsModelBinder</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/mdReZ3f1dag/</link>
		<comments>http://taswar.zeytinsoft.com/2010/03/30/asp-net-mvc2-changes-getcontrollerinstance-dataannotationsmodelbinder/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 15:56:07 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[aspmvc]]></category>
		<category><![CDATA[ASP .NET]]></category>
		<category><![CDATA[aspmvc2]]></category>
		<category><![CDATA[mvc2]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=756</guid>
		<description><![CDATA[So was in the process of updating my mvc app, there is a tool that does it for you and also a manual way . But once you have updated there are also 2 more changes that you may need if you are using IoC or DataAnnotations. First you will need to change the method [...]]]></description>
			<content:encoded><![CDATA[<p>So was in the process of updating my mvc app, there is a <a href="http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2-rtm.aspx">tool that does it</a> for you and also a <a href="http://www.asp.net/learn/whitepapers/what-is-new-in-aspnet-mvc/">manual way</a> .</p>
<p>But once you have updated there are also 2 more changes that you may need if you are using <strong>IoC</strong> or <strong>DataAnnotations</strong>.</p>
<p>First you will need to change the method in your Ioc to look for controller</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p756code31'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75631"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p756code31"><pre class="csharp" style="font-family:monospace;">&nbsp;
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CommonServiceLocatorControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> IController GetControllerInstance<span style="color: #008000;">&#40;</span>Type controllerType<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>controllerType <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetControllerInstance</span><span style="color: #008000;">&#40;</span>controllerType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> ServiceLocator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&#40;</span>controllerType<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IController<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>into, notice the <strong>RequestContext</strong> in the parameter</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p756code32'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75632"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p756code32"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CommonServiceLocatorControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
    <span style="color: #008000;">&#123;</span>       
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> IController GetControllerInstance<span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Routing</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RequestContext</span> requestContext, Type controllerType<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>controllerType <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetControllerInstance</span><span style="color: #008000;">&#40;</span>requestContext, controllerType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> ServiceLocator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&#40;</span>controllerType<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IController<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
The other change is in DataAnnotationsModelBinder if you have one, rather than inherit from <strong>Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder</strong> inherit from <strong>DefaultModelBinder</strong></p>
<p>Hope this helps <img src='http://taswar.zeytinsoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F03%2F30%2Fasp-net-mvc2-changes-getcontrollerinstance-dataannotationsmodelbinder%2F&amp;linkname=ASP%20.NET%20MVC2%20Changes%3A%20GetControllerInstance%20%26%23038%3B%20DataAnnotationsModelBinder"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/mdReZ3f1dag" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/03/30/asp-net-mvc2-changes-getcontrollerinstance-dataannotationsmodelbinder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/03/30/asp-net-mvc2-changes-getcontrollerinstance-dataannotationsmodelbinder/</feedburner:origLink></item>
		<item>
		<title>SparkViewEngine: Highlight Table Alt Row</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/Rd2FrUirK_0/</link>
		<comments>http://taswar.zeytinsoft.com/2010/03/29/sparkviewengine-highlight-table-alt-row/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:18:00 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[aspmvc]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[sparkviewengine]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=746</guid>
		<description><![CDATA[Here is how one highlights an alt row with spark, quite clean I tell you. Assuming you have a css that like below. ?View Code CSS1 2 3 4 5 6 tr.alt &#123; text-align: left; padding: 2px; border: 1px solid #000; background: #ADDFFF; &#125; And in your template you will call it like this. ?View [...]]]></description>
			<content:encoded><![CDATA[<p>Here is how one highlights an alt row with spark, quite clean I tell you.<br />
Assuming you have a css that like below.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p746code35'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74635"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p746code35"><pre class="css" style="font-family:monospace;">tr<span style="color: #6666ff;">.alt</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span>	<span style="color: #cc00cc;">#ADDFFF</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And in your template you will call it like this.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p746code36'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74636"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p746code36"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>table<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>tr<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>th<span style="color: #008000;">&gt;</span>ID<span style="color: #008000;">&lt;/</span>th<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>th<span style="color: #008000;">&gt;</span>FirstName<span style="color: #008000;">&lt;/</span>th<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>th<span style="color: #008000;">&gt;</span>LastName<span style="color: #008000;">&lt;/</span>th<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>th<span style="color: #008000;">&gt;</span>Gender<span style="color: #008000;">&lt;/</span>th<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>tr<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>tr each<span style="color: #008000;">=</span><span style="color: #666666;">&quot;var person in people&quot;</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;alt?{personIndex % 2 == 0}&quot;</span><span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>td<span style="color: #008000;">&gt;</span>$<span style="color: #008000;">&#123;</span>person<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&lt;/</span>td<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>td<span style="color: #008000;">&gt;</span>$<span style="color: #008000;">&#123;</span>person<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&lt;/</span>td<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>td<span style="color: #008000;">&gt;</span>$<span style="color: #008000;">&#123;</span>person<span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&lt;/</span>td<span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>td<span style="color: #008000;">&gt;</span>$<span style="color: #008000;">&#123;</span>person<span style="color: #008000;">.</span><span style="color: #0000FF;">Gender</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&lt;/</span>td<span style="color: #008000;">&gt;</span>	
<span style="color: #008000;">&lt;/</span>tr<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>table<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p><strong>Note:</strong> How I am using <strong>personIndex</strong>. It basically goes with the item, thus if you have <strong>var order in Orders<strong>, you would then say <strong>orderIndex</strong></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F03%2F29%2Fsparkviewengine-highlight-table-alt-row%2F&amp;linkname=SparkViewEngine%3A%20Highlight%20Table%20Alt%20Row"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/Rd2FrUirK_0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/03/29/sparkviewengine-highlight-table-alt-row/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/03/29/sparkviewengine-highlight-table-alt-row/</feedburner:origLink></item>
		<item>
		<title>Linq: Sequence contains no matching element</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/g6Cvp0bGecM/</link>
		<comments>http://taswar.zeytinsoft.com/2010/03/26/linq-sequence-contains-no-matching-element/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 15:16:08 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[FirstOrDefault]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=740</guid>
		<description><![CDATA[So sometimes you will get these strange errors or exceptions when using linq. For example if you use ?View Code CSHARP1 var result = myEnumerableCollection.First&#40;x =&#62; x.Name == person.Name&#41;; This will throw InvalidOperationException with Sequence contains no matching element. But if you use ?View Code CSHARP1 2 3 var result = myEnumerableCollection.FirstOrDefault&#40;x =&#62; x.Name == [...]]]></description>
			<content:encoded><![CDATA[<p>So sometimes you will get these strange errors or exceptions when using linq.</p>
<p>For example if you use</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p740code39'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74039"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p740code39"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> myEnumerableCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> person<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>This will throw InvalidOperationException with Sequence contains no matching element.</p>
<p>But if you use</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p740code40'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74040"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p740code40"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> myEnumerableCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> person<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>result <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008080; font-style: italic;">//did not find in the collection something went wrong or .....</span></pre></td></tr></table></div>

<p>Which will return you a null if its not found then you can check if the result is null and continue on with your work <img src='http://taswar.zeytinsoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope this helps <img src='http://taswar.zeytinsoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F03%2F26%2Flinq-sequence-contains-no-matching-element%2F&amp;linkname=Linq%3A%20Sequence%20contains%20no%20matching%20element"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/g6Cvp0bGecM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/03/26/linq-sequence-contains-no-matching-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/03/26/linq-sequence-contains-no-matching-element/</feedburner:origLink></item>
		<item>
		<title>Asp .NET Redirect To SSL HttpModule</title>
		<link>http://feedproxy.google.com/~r/TaswarBhattiBlog/~3/oWOTxBEv9Uw/</link>
		<comments>http://taswar.zeytinsoft.com/2010/02/18/asp-net-redirect-to-ssl-httpmodule/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 17:36:22 +0000</pubDate>
		<dc:creator>taswar</dc:creator>
				<category><![CDATA[ASP .NET]]></category>

		<guid isPermaLink="false">http://taswar.zeytinsoft.com/?p=731</guid>
		<description><![CDATA[There are tones of blog post out there on how to redirect http to https. As in when a user request for http://myserver.com/Login.aspx they will be redirected to https://myserver.com/Login.aspx One can do it with IIS but I find that way a bit ugly since you have create another site and set the 403 to redirect [...]]]></description>
			<content:encoded><![CDATA[<p>There are tones of blog post out there on how to redirect <strong>http to https</strong>. As in when a user request for <strong>http://myserver.com/Login.aspx</strong><br />
they will be redirected to <strong>https://myserver.com/Login.aspx</strong></p>
<p>One can do it with IIS but I find that way a bit ugly since you have create another site and set the 403 to redirect to another location. Not as elegant as apache mod rewrite.</p>
<p>A more elegant approach I found when using asp .net is by using an HttpModule in your code, so that the entire site is redirected. There are other solutions to redirect individual pages but for this post we will focus on redirecting all traffic to SSL.</p>
<p>To begin with we will code up an RedirectToSSL.cs page and place it into our App_Code folder.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p731code43'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p73143"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code" id="p731code43"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> SolutionName<span style="color: #008000;">.</span><span style="color: #0000FF;">App_Code</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RedirectToSSL <span style="color: #008000;">:</span> IHttpModule
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080;">#region IHttpModule Members</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Init<span style="color: #008000;">&#40;</span>HttpApplication context<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            context<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginRequest</span> <span style="color: #008000;">+=</span> ContextBeginRequest<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ContextBeginRequest<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
            HttpApplication application <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> HttpApplication<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>application <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> 
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>application<span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsSecureConnection</span><span style="color: #008000;">&#41;</span> 
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// send user to SSL </span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> serverName <span style="color: #008000;">=</span> HttpUtility<span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncode</span><span style="color: #008000;">&#40;</span>application<span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ServerVariables</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;SERVER_NAME&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> filePath <span style="color: #008000;">=</span> application<span style="color: #008000;">.</span><span style="color: #0000FF;">Request</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FilePath</span><span style="color: #008000;">;</span>
&nbsp;
            application<span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Redirect</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;https://&quot;</span> <span style="color: #008000;">+</span> serverName <span style="color: #008000;">+</span> filePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Then all we have to do is add this line in our web.config file in order to enable it.<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p731code44'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p73144"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p731code44"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpModules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;RedirectToSSL&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;SolutionName.App_Code.RedirectToSSL&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/httpModules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
Replace the solutionName with your project name and voila you now have the entire site in SSL and you don&#8217;t have to worry about talking to an admin for IIS configuration or anything like that.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Ftaswar.zeytinsoft.com%2F2010%2F02%2F18%2Fasp-net-redirect-to-ssl-httpmodule%2F&amp;linkname=Asp%20.NET%20Redirect%20To%20SSL%20HttpModule"><img src="http://taswar.zeytinsoft.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/TaswarBhattiBlog/~4/oWOTxBEv9Uw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://taswar.zeytinsoft.com/2010/02/18/asp-net-redirect-to-ssl-httpmodule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://taswar.zeytinsoft.com/2010/02/18/asp-net-redirect-to-ssl-httpmodule/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.820 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-07-08 10:04:56 -->
