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

<channel>
	<title>Arturito.net &#187; C sharp</title>
	<atom:link href="http://arturito.net/category/c-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://arturito.net</link>
	<description>Come to The Dark Side, We Have Cookies!</description>
	<lastBuildDate>Mon, 30 Jan 2012 16:57:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.MVC 2 &#8211; How to migrate project from  ASP.NET 2.0 .NET Framework 3.5 to  ASP.NET 4.0 .NET Framework 4</title>
		<link>http://arturito.net/2012/01/25/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4/</link>
		<comments>http://arturito.net/2012/01/25/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 10:20:32 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[IIS7]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1145</guid>
		<description><![CDATA[TweetSo you have decided to move your ASP.NET MVC2 project  from the Windows Server configured with  ASP.NET 2.0 .NET Framework 3.5
to  ASP.NET 4.0 .NET Framework 4.
Here is how I did it.
My old setup:
Windows Server 2008 R2
IIS 7.5
 ASP.NET
Application Pool : .NET Framework v2.0.50727
.NET Framework installed:  3.5 SP1
My new setup:
Windows Server 2008 R2
IIS 7.5
ASP.NET
Application Pool : .NET [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1145" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2012%2F01%2F25%2Fasp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4%2F&amp;text=ASP.MVC%202%20%26%238211%3B%20How%20to%20migrate%20project%20from%20%20ASP.NET%202.0%20.NET%20Framework%203.5%20to%20%20ASP.NET%204.0%20.NET%20Framework%204&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2012%2F01%2F25%2Fasp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>So you have decided to move your ASP.NET MVC2 project  from the Windows Server configured with  ASP.NET 2.0 .NET Framework 3.5<br />
to  ASP.NET 4.0 .NET Framework 4.</p>
<p>Here is how I did it.</p>
<p>My old setup:</p>
<p>Windows Server 2008 R2<br />
<strong>IIS 7.5</strong><br />
<strong> ASP.NET</strong><br />
Application Pool :<strong> .NET Framework v2.0.50727</strong><br />
.NET Framework installed:  <strong>3.5 SP1</strong></p>
<p>My new setup:</p>
<p>Windows Server 2008 R2<br />
<strong>IIS 7.5<br />
</strong><strong>ASP.NET<br />
</strong>Application Pool : <strong>.NET Framework v4.0.30319<br />
</strong>.NET Framework installed:<strong>  4.0</strong></p>
<p>The good thing is that the ASP.NET is <strong>backward compatible</strong>. You shoud publish the website on the new setup and it should work straight away.</p>
<p>In my case I had to remove these from<strong> web.config</strong>:</p>
<pre class="brush: xml; title: ; notranslate">

&lt;section name=&quot;scriptResourceHandler&quot; type=&quot;System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirepermission=&quot;false&quot; allowdefinition=&quot;MachineToApplication&quot;&gt;&lt;/section&gt;
&lt;section name=&quot;jsonSerialization&quot; type=&quot;System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirepermission=&quot;false&quot; allowdefinition=&quot;Everywhere&quot;&gt;&lt;/section&gt;&lt;section name=&quot;profileService&quot; type=&quot;System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirepermission=&quot;false&quot; allowdefinition=&quot;MachineToApplication&quot;&gt;&lt;/section&gt;&lt;section name=&quot;authenticationService&quot; type=&quot;System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirepermission=&quot;false&quot; allowdefinition=&quot;MachineToApplication&quot;&gt;&lt;/section&gt;&lt;section name=&quot;roleService&quot; type=&quot;System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirepermission=&quot;false&quot; allowdefinition=&quot;MachineToApplication&quot;&gt;&lt;/section&gt;
</pre>
<p>If it still gives you problems got to <strong>IIS Manager</strong> and click<strong> Error Pages</strong>:</p>
<p><img class="alignnone size-full wp-image-1138" title="asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_IIS" src="http://arturito.net/wp-content/uploads/2012/01/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_IIS.png" alt="" width="446" height="105" /></p>
<p>Select <strong>500</strong> , right click and select <strong>Edit Feature Settings</strong></p>
<p><a href="http://arturito.net/wp-content/uploads/2012/01/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_IIS_Edit.png"><img class="alignnone size-full wp-image-1140" title="asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_IIS_Edit" src="http://arturito.net/wp-content/uploads/2012/01/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_IIS_Edit.png" alt="" width="593" height="421" /></a></p>
<p>Here select &#8220;Detailed errors&#8221;</p>
<p><img class="alignnone size-full wp-image-1139" title="asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_Error_Details" src="http://arturito.net/wp-content/uploads/2012/01/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4_Error_Details.png" alt="" width="583" height="675" /></p>
<p>Now you should get detailed error within your web.config. Also check <strong>Event Viewer</strong> under the section <strong>Administrative Events</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2012/01/25/asp-mvc-2-how-to-migrate-project-from-asp-net-2-0-net-framework-3-5-to-asp-net-4-0-net-framework-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC2  Visual Studio Error &#8211; The project type is not supported by this installation</title>
		<link>http://arturito.net/2011/11/16/asp-net-mvc2-visual-studio-error-the-project-type-is-not-supported-by-this-installation/</link>
		<comments>http://arturito.net/2011/11/16/asp-net-mvc2-visual-studio-error-the-project-type-is-not-supported-by-this-installation/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 08:03:03 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[asp.net mvc2]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1108</guid>
		<description><![CDATA[Tweet

If you get this error when opening a solution in Visual Studio, just go to the project file (YourProject.csproj) which fails to open and change the line
to

]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1108" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F11%2F16%2Fasp-net-mvc2-visual-studio-error-the-project-type-is-not-supported-by-this-installation%2F&amp;text=ASP.NET%20MVC2%20%20Visual%20Studio%20Error%20%26%238211%3B%20The%20project%20type%20is%20not%20supported%20by%20this%20installation&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F11%2F16%2Fasp-net-mvc2-visual-studio-error-the-project-type-is-not-supported-by-this-installation%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><br/><br />
<br/></p>
<p>If you get this error when opening a solution in <strong>Visual Studio</strong>, just go to the project file (<strong>YourProject.csproj</strong>) which fails to open and change the line</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ProjectTypeGuids&gt;{Some_GUID}&lt;/ProjectTypeGuids&gt;
</pre>
<p>to</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ProjectTypeGuids&gt;{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}&lt;/ProjectTypeGuids&gt;
</pre>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/11/16/asp-net-mvc2-visual-studio-error-the-project-type-is-not-supported-by-this-installation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC2 in the web farm &#8211; slow page load with high traffic &#8211; where is the bottleneck?</title>
		<link>http://arturito.net/2011/10/24/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck/</link>
		<comments>http://arturito.net/2011/10/24/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 10:59:12 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[asp.net mvc2]]></category>
		<category><![CDATA[Bottleneck]]></category>
		<category><![CDATA[Web Farm]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1099</guid>
		<description><![CDATA[TweetRecently I have implemented quite complex e-commerce web application using technologies such as: ASP.NET MVC2, Linq2SQL , .NET 3.5 , SQL Server 2008 R2.
We have the whole thing hosted in the web farm.
We have 4 x IIS7 + 1 SQL SERVER 2008 load balanced with MS NLB
Static content is cached by external cache provider &#8211; [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1099" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F10%2F24%2Fasp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck%2F&amp;text=ASP.NET%20MVC2%20in%20the%20web%20farm%20%26%238211%3B%20slow%20page%20load%20with%20high%20traffic%20%26%238211%3B%20where%20is%20the%20bottleneck%3F&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F10%2F24%2Fasp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Recently I have implemented quite complex e-commerce web application using technologies such as: ASP.NET MVC2, Linq2SQL , .NET 3.5 , SQL Server 2008 R2.</p>
<p>We have the whole thing hosted in the web farm.</p>
<p>We have 4 x IIS7 + 1 SQL SERVER 2008 load balanced with MS NLB</p>
<p>Static content is cached by external cache provider &#8211; Akamai, which reduces 86% of the requests.</p>
<p>Each web server has 32 GB of RAM and 4 x quad core CPUs so there are 64 cores on the front-end.</p>
<p>We store session state in tables of SQL server.</p>
<p>It works prefectly with medium traffic (page load = 0.2 -0.3 s)</p>
<p><img class="alignnone" title="asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck-after.jpg" alt="" width="708" height="73" /></p>
<p>but the company does the tv ads and during those ads the traffic hits up to 20,000 -30,000 users within 20-30 seconds.</p>
<p>In this moment the page slows down to 39 seconds. However, the usage of CPU&#8217;s and the memory doesn&#8217;t even arrive to 40% on any machine.</p>
<p><img class="alignnone" title="ASP.NET MVC2 in the web farm - slow page load with high traffic - where is the bottleneck?" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck-before.jpg" alt="" width="689" height="49" /></p>
<p>The bandwidth of the data centre does not arrive to the half of its limits.</p>
<p>Pages which are slow generate data from simple SELECTs of maximum 10 records from 1-2 tables only.</p>
<p>I gone through all possible things: I already had my queries compiled but I also optimized selects, created additional indexes studied a lot the problems of storing session in tables that sql server 2005 had (temdb) and I couldn&#8217;t figure out what was going on.</p>
<p>You could reproduce the the problem by launching stress tools against the web. The worst thing was that we could freeze the web just by launching stress tools from only two pc&#8217;s located in our offices.</p>
<p>I have also posted the question on stackoverflow<br />
<a href="http://stackoverflow.com/questions/7644339/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic" target="_blank">http://stackoverflow.com/questions/7644339/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic<br />
</a>where people suggested to check indexes and use CompiledQuery.</p>
<p>In practice 90% of the bottlenecks are caused by database. After the whole week of tests and studying the problem I have possibly found a bug in the ASP.NET MVC2 library.<br />
It was incredible how silly the thing was:</p>
<p>Let&#8217;s have a look at this code:</p>
<pre class="brush: csharp; title: ; notranslate">

public ActionResult Index(){
var model = new SomeModel();
// Get data
....
return View(model);
}
</pre>
<p>In this code the <strong>View</strong> is generated using file<strong> Index.aspx</strong>.<br />
However, this <strong>View</strong> needs to be found in some way and I guess the search of the this <strong>View</strong> is causing the problem.</p>
<p>Unbelievable but true &#8211; <strong>adding the name of the View removed the problem</strong>:</p>
<pre class="brush: csharp; title: ; notranslate">

public ActionResult Index() {
 var model = new SomeModel();
// Get data
....
return View(&quot;Index&quot;,model);
}
</pre>
<p>I say it again, this problem does exists only when the web has a high number of users.<br />
The MVC version I&#8217;m using is</p>
<p>Version: 2.0.0.0<br />
Runtime: v2.0.50727</p>
<p>I have contacted the geeks responsible for the design and development of this great library to see what they say.</p>
<p>By all means I don&#8217;t  want to discourage anyone to use this library as it is absolutely fantastic and I use it a lot and I will carry on using it.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/10/24/asp-net-mvc2-in-the-web-farm-slow-page-load-with-high-traffic-where-is-the-bottleneck/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to remove Server, X-AspNet-Version, X-AspNetMvc-Version and X-Powered-By from the response header in IIS7</title>
		<link>http://arturito.net/2011/10/21/how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7/</link>
		<comments>http://arturito.net/2011/10/21/how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 07:07:12 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[IIS7]]></category>
		<category><![CDATA[ASP:NET MVC2]]></category>
		<category><![CDATA[headers]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1095</guid>
		<description><![CDATA[TweetYou can check your response headers by using Firebug add-on for Firefox or just hit Ctrl+J in Chrome.

Unwanted info:
Server Microsoft-IIS/7.5
X-AspNetMvc-Version 3.0
X-AspNet-Version 4.0.303319
X-Powered-By ASP.NET
&#160;
1. Removing X-AspNet-Version
In web.config stick this line in
2. Removing X-AspNetMvc-Version
In Global.asax.cs add this line:
3. Removing or changing Server
Add this module class to your project.
And add set it in web config
4. Removing or changing [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1095" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F10%2F21%2Fhow-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7%2F&amp;text=How%20to%20remove%20Server%2C%20X-AspNet-Version%2C%20X-AspNetMvc-Version%20and%20X-Powered-By%20from%20the%20response%20header%20in%20IIS7&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F10%2F21%2Fhow-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>You can check your response headers by using<strong> Firebug</strong> add-on for <strong>Firefox</strong> or just hit <strong>Ctrl+J</strong> in <strong>Chrome</strong>.</p>
<p><img class="alignnone" title="how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7-1.jpg" alt="" width="735" height="516" /></p>
<p>Unwanted info:</p>
<p>Server Microsoft-IIS/7.5<br />
X-AspNetMvc-Version 3.0<br />
X-AspNet-Version 4.0.303319<br />
X-Powered-By ASP.NET</p>
<p>&nbsp;</p>
<p>1. Removing <strong>X-AspNet-Version</strong></p>
<p>In <strong>web.config</strong> stick this line in</p>
<pre class="brush: xml; title: ; notranslate">

&lt;system.web&gt;
      &lt;httpRuntime enableVersionHeader=&quot;false&quot;/&gt;
	...
</pre>
<p>2. Removing <strong>X-AspNetMvc-Version</strong></p>
<p>In <strong>Global.asax.cs</strong> add this line:</p>
<pre class="brush: csharp; title: ; notranslate">

protected void Application_Start()
{
    MvcHandler.DisableMvcResponseHeader = true;
}
</pre>
<p>3. Removing or changing <strong>Server</strong></p>
<p>Add this module class to your project.</p>
<pre class="brush: csharp; title: ; notranslate">

using System;
using System.Web;

namespace Project.Infrastructure.Web.Modules.Http
{
    public class CustomHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }

        public void Dispose() { }

        void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
	     //HttpContext.Current.Response.Headers.Remove(&quot;Server&quot;);
            // Or you can set something funny
	    HttpContext.Current.Response.Headers.Set(&quot;Server&quot;, &quot;CERN httpd&quot;);
        }
    }
}
</pre>
<p>And add set it in <strong>web config</strong></p>
<pre class="brush: xml; title: ; notranslate">

	&lt;system.webServer&gt;
		&lt;modules&gt;
        &lt;add name=&quot;CustomHeaderModule&quot; type=&quot;StrongNamespace.HttpModules.CustomHeaderModule&quot; /&gt;
</pre>
<p>4. Removing or changing<strong> X-Powered-By</strong></p>
<p>Go to <strong>IIS7 Management Console</strong> and open <strong>HTTP Response Headers</strong></p>
<p><img class="alignnone" title="how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7-2.jpg" alt="" width="774" height="344" /></p>
<p>And that should be it!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/10/21/how-to-remove-server-x-aspnet-version-x-aspnetmvc-version-and-x-powered-by-from-the-response-header-in-iis7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master.</title>
		<link>http://arturito.net/2011/08/05/how-to-switch-between-http-and-https-in-asp-net-mvc2-and-allow-ajax-requests-from-site-master/</link>
		<comments>http://arturito.net/2011/08/05/how-to-switch-between-http-and-https-in-asp-net-mvc2-and-allow-ajax-requests-from-site-master/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 16:12:30 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP:NET MVC2]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTTPS]]></category>
		<category><![CDATA[Site.Master]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1055</guid>
		<description><![CDATA[TweetIn order to serve the request throught SSL in ASP.NET MVC2 we can simple use Action attribute [RequireHttps]:
Let&#8217;s say I served a login form here. Then the user logs in.
Note that the redirection ( RedirectToAction(&#8220;List&#8221;, &#8220;Home&#8221;) ) is taking us to HomeController List action.
However the action List in HomeController is not decorated in [RequireHttps] so [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1055" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F08%2F05%2Fhow-to-switch-between-http-and-https-in-asp-net-mvc2-and-allow-ajax-requests-from-site-master%2F&amp;text=How%20to%20Switch%20Between%20HTTP%20and%20HTTPS%20in%20ASP.NET%20MVC2%20And%20Allow%20AJAX%20Requests%20From%20Site%20Master.&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F08%2F05%2Fhow-to-switch-between-http-and-https-in-asp-net-mvc2-and-allow-ajax-requests-from-site-master%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>In order to serve the request throught <strong>SSL</strong> in <strong>ASP.NET MVC2</strong> we can simple use Action attribute <strong>[RequireHttps]</strong>:</p>
<pre class="brush: csharp; title: ; notranslate">
		//LoginController
        [RequireHttps]
        public ActionResult Index()
        {
            return View();
        }
</pre>
<p>Let&#8217;s say I served a login form here. Then the user logs in.</p>
<pre class="brush: csharp; title: ; notranslate">
		//LoginController
        [RequireHttps]
        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            if (model != null &amp;&amp; model.Username == &quot;arturito&quot;)
                 return RedirectToAction(&quot;List&quot;, &quot;Home&quot;);
            else
                return RedirectToAction(&quot;Login&quot;);
        }
</pre>
<p>Note that the redirection (<strong> RedirectToAction(&#8220;List&#8221;, &#8220;Home&#8221;)</strong> ) is taking us to <strong>HomeController</strong> <strong>List</strong> action.<br />
However the action <strong>List</strong> in <strong>HomeController</strong> is not decorated in<strong> [RequireHttps]</strong> so it should return to<strong> HTTP</strong>.</p>
<pre class="brush: csharp; title: ; notranslate">
        //HomeController
		public ActionResult List()
        {
            return View();
        }
</pre>
<p><span style="color: #993300;"><strong>BUT IT DOESN&#8217;T!</strong></span><br />
You will remain in <strong>HTTPS</strong>.</p>
<p>Jeff Widmer worked out a great solution and posted it in his <a title="http://weblogs.asp.net/jeffwids/archive/2010/08/19/how-to-switch-between-http-and-https-in-asp-net-mvc2.aspx" href="http://weblogs.asp.net/jeffwids/archive/2010/08/19/how-to-switch-between-http-and-https-in-asp-net-mvc2.aspx" target="_blank">article</a>.<br />
In his solution we have <strong>BaseController</strong> and we are overriding<strong> OnAuthorization</strong> method</p>
<pre class="brush: csharp; title: ; notranslate">
    public class BaseController : Controller
    {
            protected override void OnAuthorization(AuthorizationContext filterContext)
            {

                //the RequireHttpsAttribute set on the Controller Action will handle redirecting to Https.
                // We just need to handle any requests that are already under SSL but should not be.
                if (Request.IsSecureConnection)
                {
                    Boolean requireHttps = false;
                    requireHttps = filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), false).Count() &gt;= 1;

                    //If this request is under ssl but yet the controller action
                    // does not require it, then redirect to the http version.
                    if (!requireHttps &amp;&amp; !filterContext.IsChildAction)
                    {
                        UriBuilder uriBuilder = new UriBuilder(Request.Url);

                        //change the scheme
                        uriBuilder.Scheme = &quot;http&quot;;
                        uriBuilder.Port = 80;

                        filterContext.Result = this.Redirect(uriBuilder.Uri.AbsoluteUri);
                    }
                }

                base.OnAuthorization(filterContext);
            }

    }
</pre>
<p>But there is one problem left!</p>
<p>Let&#8217;s say that all pages served through <strong>HTTP</strong> and <strong>HTTPS</strong> use <strong>the same site master page</strong> which contains a<strong> link or the form that creates ajax request</strong>.<br />
As an example let&#8217;s look at this simple action:</p>
<pre class="brush: csharp; title: ; notranslate">
		// Home Controller
        [HttpPost]
        public ActionResult Ajax()
        {
            return Content(&quot;This is Ajax&quot;);
        }
</pre>
<p>This works great from HTTP page!</p>
<p><img class="alignnone" title="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." src="http://i1134.photobucket.com/albums/m614/Arturito_Net/01-how-to-switch-between-http-and-https-in-aspnet-mvc2-and-allow-ajax-requests.jpg" alt="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." width="565" height="395" /></p>
<p><img class="alignnone" title="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." src="http://i1134.photobucket.com/albums/m614/Arturito_Net/02-how-to-switch-between-http-and-https-in-aspnet-mvc2-and-allow-ajax-requests.jpg" alt="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." width="565" height="395" /></p>
<p>&#8230;.but when we call the same action from HTTPS page it doesn&#8217;t work!<br />
<img class="alignnone" title="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." src="http://i1134.photobucket.com/albums/m614/Arturito_Net/03-how-to-switch-between-http-and-https-in-aspnet-mvc2-and-allow-ajax-requests.jpg" alt="How to Switch Between HTTP and HTTPS in ASP.NET MVC2 And Allow AJAX Requests From Site Master." width="565" height="529" /></p>
<p>Why? This is called <strong>crossdomain ajax request</strong>, which by default is not allowed for security reasons. There are some methods/libraries/proxies to achieve this but it is not very common yet.</p>
<p>If we decorate it with <strong>[RequireHttps]</strong> attribute:</p>
<pre class="brush: csharp; title: ; notranslate">
		// Home Controller
		[RequireHttps]
        [HttpPost]
        public ActionResult Ajax()
        {
            return Content(&quot;This is Ajax&quot;);
        }
</pre>
<p>This action becomes available only if requested from the page served through HTTPS. We can <strong>no longer</strong> call it from the page served through http.</p>
<p>Well then, let&#8217;s allow to perform all ajax request from pages served by <strong>both HTTP and HTTPS</strong>:</p>
<pre class="brush: csharp; title: ; notranslate">
protected override void OnAuthorization(AuthorizationContext filterContext)
            {

                //the RequireHttpsAttribute set on the Controller Action will handle redirecting to Https.
                // We just need to handle any requests that are already under SSL but should not be.
                if (Request.IsSecureConnection)
                {
                    Boolean requireHttps = false;
                    requireHttps = filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), false).Count() &gt;= 1;

                    // Allow Ajax request from both http and https pages
                    if (Request.IsAjaxRequest())
                    {
                        base.OnAuthorization(filterContext);
                        return;
                    }

                    //If this request is under ssl but yet the controller action
                    // does not require it, then redirect to the http version.
                    if (!requireHttps &amp;&amp; !filterContext.IsChildAction)
                    {
                        UriBuilder uriBuilder = new UriBuilder(Request.Url);

                        //change the scheme
                        uriBuilder.Scheme = &quot;http&quot;;
                        uriBuilder.Port = 80;

                        filterContext.Result = this.Redirect(uriBuilder.Uri.AbsoluteUri);
                    }
                }

                base.OnAuthorization(filterContext);
            }

    }
</pre>
<p>Now whether we are on login page (HTTPS) or any other page (HTTP) <strong>we can create ajax requests in the site master</strong> without decorating ajax actions with additional attributes or messing around<br />
with url scheme in the Views.</p>
<p>Here is a downloadable example: <a title=" http://www.mediafire.com/?0d6dn6w489yalgo" href=" http://www.mediafire.com/?0d6dn6w489yalgo" target="_blank"> http://www.mediafire.com/?0d6dn6w489yalgo</a></p>
<p>GL</p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/08/05/how-to-switch-between-http-and-https-in-asp-net-mvc2-and-allow-ajax-requests-from-site-master/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session</title>
		<link>http://arturito.net/2011/08/03/asp-net-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session/</link>
		<comments>http://arturito.net/2011/08/03/asp-net-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 18:03:48 +0000</pubDate>
		<dc:creator>arturito</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP:NET MVC2]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[Html Helpers]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=1021</guid>
		<description><![CDATA[TweetASP.NET MVC2 makes so many things so easy to program but there are some basic things that are just not there and they should be.
I will be talking about a basic functionality that is a must these days.
I am talking about a menu and highlighting selected menu item.
Let&#8217;s see the problem:
I have 3 links and [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1021" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F08%2F03%2Fasp-net-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session%2F&amp;text=ASP.NET%20MVC%202%20Highlight%20Selected%20Menu%20Item%20on%20the%20Site%20Master%20Without%20Session&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F08%2F03%2Fasp-net-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>ASP.NET MVC2 makes so many things so easy to program but there are some basic things that are just not there and they should be.<br />
I will be talking about a basic functionality that is a must these days.<br />
I am talking about a menu and <strong>highlighting selected menu item</strong>.</p>
<p>Let&#8217;s see the problem:</p>
<p>I have 3 links and with each click I would like to add apropiate css class<strong> to mark my menu item as selected</strong>.</p>
<p><img class="alignnone" title="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/01-aspnet-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session.jpg" alt="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" width="641" height="363" /></p>
<p>I have follwed an article on <a title="http://geekswithblogs.net/bdiaz/archive/2010/04/09/handy-asp.net-mvc-2-extension-methods-ndash-where-am-i.aspx " href="http://geekswithblogs.net/bdiaz/archive/2010/04/09/handy-asp.net-mvc-2-extension-methods-ndash-where-am-i.aspx " target="_blank">bobby&#8217;s blog</a>.<br />
So with this great extension it does work really well:</p>
<pre class="brush: csharp; title: ; notranslate">
  public static class HtmlExtensions
    {
        public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper,
        String linkText, String actionName, String controllerName)
        {
            var tag = new TagBuilder(&quot;li&quot;);
            if (htmlHelper.ViewContext.RequestContext.IsCurrentRoute(
            null, controllerName, actionName) ||
            htmlHelper.ViewContext.RequestContext.IsParentRoute(
            controllerName, actionName))
            {
                tag.AddCssClass(&quot;active&quot;);
            }
            tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString();

            return MvcHtmlString.Create(tag.ToString());
        }
    }
</pre>
<p>In the place of menu we stick this:</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;%= Html.ActionMenuItem(&quot;Home&quot;, &quot;Index&quot;, &quot;Home&quot;) %&gt;
</pre>
<p>However, there was a one little thing missing in it that I needed to have on my web.</p>
<p><img class="alignnone" title="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/02-aspnet-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session.jpg" alt="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" width="563" height="282" /></p>
<p>In the <strong>About</strong> link I have a link to <strong>Biography</strong> . (Biography controller and Index action)<br />
Since the<strong> Biography</strong> page is a part of <strong>About section</strong> I would like the <strong>&#8220;About&#8221;</strong> menu item to remain<strong> highlighted</strong>.<br />
The solution above doesn&#8217;t work in that case.<br />
So I decided to modify it a bit.</p>
<p>Let&#8217;s start from the extension:</p>
<pre class="brush: csharp; title: ; notranslate">
    public static class HtmlExtensions
    {
        public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText, String actionName, String controllerName)
        {
            var tag = new TagBuilder(&quot;li&quot;);

            if (htmlHelper.ViewContext.RequestContext.IsCurrentRoute(null, controllerName, actionName) ||
                htmlHelper.ViewContext.RequestContext.IsParentRoute(controllerName, actionName))
            {
                tag.AddCssClass(&quot;active&quot;); // stick class active
            }
            else
            {
                tag.AddCssClass(&quot;inactive&quot;); // stick class inactive
            }

            tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString();

            return MvcHtmlString.Create(tag.ToString());
        }
    }
 </pre>
<p>We also neeed Request Extensions:</p>
<pre class="brush: csharp; title: ; notranslate">
    public static class RequestExtensions
    {
        public static bool IsCurrentRoute(this RequestContext context, String areaName)
        {
            return context.IsCurrentRoute(areaName, null, null);
        }

        public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName)
        {
            return context.IsCurrentRoute(areaName, controllerName, null);
        }

        public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName, params String[] actionNames)
        {
            var routeData = context.RouteData;
            var routeArea = routeData.DataTokens[&quot;area&quot;] as String;
            var current = false;

            if (((String.IsNullOrEmpty(routeArea) &amp;&amp; String.IsNullOrEmpty(areaName)) || (routeArea == areaName)) &amp;&amp;
                 ((String.IsNullOrEmpty(controllerName)) || (routeData.GetRequiredString(&quot;controller&quot;) == controllerName)) &amp;&amp;
                 ((actionNames == null) || actionNames.Contains(routeData.GetRequiredString(&quot;action&quot;))))
            {
                current = true;
            }

            return current;
        }

        public static bool IsParentRoute(this RequestContext context, String controller, String action)
        {
            var routeData = context.RouteData;
            UrlModel returnUrl = null;
            UrlModel requestUrl = new UrlModel { Action = routeData.GetRequiredString(&quot;action&quot;), Controller = routeData.GetRequiredString(&quot;controller&quot;) };
            UrlModel linkUrl = new UrlModel { Action = action, Controller = controller };

            var urls = UrlMap.GetDictionary();
            urls.TryGetValue(requestUrl, out returnUrl);

            if (returnUrl != null &amp;&amp; returnUrl.Equals(linkUrl))
                return true;
            else
                return false; ;
        }
    }
 </pre>
<p>And now the important part the UrlMap</p>
<pre class="brush: csharp; title: ; notranslate">

    public static class UrlMap
    {
        public static Dictionary GetDictionary()
        {
            Dictionary urls = new Dictionary();
            urls.Add(new UrlModel { Controller = &quot;Biography&quot;, Action = &quot;Index&quot; }, new UrlModel { Controller = &quot;About&quot;, Action = &quot;Index&quot; });
            urls.Add(new UrlModel { Controller = &quot;Contact&quot;, Action = &quot;GetInTouch&quot; }, new UrlModel { Controller = &quot;Contact&quot;, Action = &quot;Index&quot; });
            return urls;
        }
    }
</pre>
<p>Here I specify that if<strong> Biography/Index</strong> is called please mark it the same as <strong>About/Index</strong>.</p>
<p>Don&#8217;t forget to create<strong> UrlModel</strong> class</p>
<pre class="brush: csharp; title: ; notranslate">
public class UrlModel
    {
        public string Action { get; set; }
        public string Controller { get; set; }

        public override bool Equals(object obj)
        {
            return Equals(obj as UrlModel);
        }
        public bool Equals(UrlModel obj)
        {
            return obj != null &amp;&amp; obj.Action == this.Action &amp;&amp; obj.Controller == this.Controller;
        }
        public override int GetHashCode()
        {
            return (Action + Controller).GetHashCode();
        }
    }
</pre>
<p>Now you can stick<strong> HtmlHelper</strong> with menu in any place on the site.</p>
<pre class="brush: csharp; title: ; notranslate">
    &lt;ul id=&quot;menu&quot;&gt;
        &lt;%= Html.ActionMenuItem(&quot;Home&quot;, &quot;Index&quot;, &quot;Home&quot;) %&gt;
        &lt;%= Html.ActionMenuItem(&quot;About&quot;, &quot;Index&quot;, &quot;About&quot;) %&gt;
        &lt;%= Html.ActionMenuItem(&quot;Contact&quot;, &quot;Index&quot;, &quot;Contact&quot;) %&gt;
    &lt;/ul&gt;
</pre>
<p>and add as amny<strong> Url Maps</strong>as you like:</p>
<pre class="brush: csharp; title: ; notranslate">
 public static Dictionary GetDictionary()
        {
            Dictionary urls = new Dictionary();
            urls.Add(new UrlModel { Controller = &quot;Biography&quot;, Action = &quot;Index&quot; }, new UrlModel { Controller = &quot;About&quot;, Action = &quot;Index&quot; });
            urls.Add(new UrlModel { Controller = &quot;Contact&quot;, Action = &quot;GetInTouch&quot; }, new UrlModel { Controller = &quot;Contact&quot;, Action = &quot;Index&quot; });
            return urls;
        }
</pre>
<p>Here is how it looks: <img class="alignnone" title="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/03-aspnet-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session.jpg" alt="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" width="715" height="530" /></p>
<pre></pre>
<p><img class="alignnone" title="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/04-aspnet-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session.jpg" alt="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" width="705" height="400" /></p>
<pre></pre>
<p><img class="alignnone" title="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/05-aspnet-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session.jpg" alt="ASP.NET MVC 2 Highlight Selected Menu Item on the Site Master Without Session" width="705" height="400" /></p>
<pre>Here is a complete solution to download: <a title="http://www.mediafire.com/?0xabi855suro2rl" href="http://www.mediafire.com/?0xabi855suro2rl" target="_blank">http://www.mediafire.com/?0xabi855suro2rl</a>

GL!</pre>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/08/03/asp-net-mvc-2-highlight-selected-menu-item-on-the-site-master-without-session/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC2 jQuery Form Post with JSON Tutorial</title>
		<link>http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/</link>
		<comments>http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 21:11:00 +0000</pubDate>
		<dc:creator>guru</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>

		<guid isPermaLink="false">http://dartharturito.wordpress.com/?p=504</guid>
		<description><![CDATA[TweetIn one of the previous post I covered how to post form with ASP.NET MVC2 using jQuery.
Today I will explain how to return data in JSON format. Why return data in JSON format. Well there are several reasons for it:
1. You can easily update several parts of the page without reloading entire page
Imagine that you have [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton504" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F01%2F25%2Fasp-net-mvc2-jquery-form-post-with-json-tutorial%2F&amp;text=ASP.NET%20MVC2%20jQuery%20Form%20Post%20with%20JSON%20Tutorial&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F01%2F25%2Fasp-net-mvc2-jquery-form-post-with-json-tutorial%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>In one of the previous post I covered how to <a href="http://arturito.net/2010/12/02/asp-net-mvc2-jquery-form-post-tutorial/" target="_blank">post form with ASP.NET MVC2 using jQuery</a>.</p>
<p>Today I will explain how to return data in JSON format. Why return data in JSON format. Well there are several reasons for it:</p>
<p>1. You can easily update several parts of the page without reloading entire page</p>
<p>Imagine that you have ajax login and after successful login the name of the user is updated in the header of the page and content of the saved cart in the right column.</p>
<p>2. It reduces the amount of data sent from the server to the browser.</p>
<p>In a lot of cases developers send data wrapped in html such as:</p>
<hr />
<p><span style="color:#0000ff;"><span style="color:#0000ff;">return</span></span> Content(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;
<p class=\"mark-it-green\">You submitted: &#8220;</span></span> + data.FirstName + <span style="color:#a31515;"><span style="color:#a31515;">&#8221; &#8220;</span></span> + data.LastName +<span style="color:#a31515;"><span style="color:#a31515;">&#8220;
<p>&#8220;</span></span>);</p>
<p><span style="color:#0000ff;"> </span></p>
<hr />
<p>or they send PartialView. But the idea of JSON is to send data only without html tags, which also reduces the traffic.</p>
<p>3. It separates application logic from design</p>
<p>Sending only data using JSON means data doesn’t have to be wrapped in html tags in your controller, you only send pure data. This way one developer can concentrate on developing logic and doesn’t have to think how it will be displayed, leaving web designer to handle presentation part.</p>
<p>Let’s start modifying previous project:</p>
<p>1. Open MVC2JQuery project that can be downloaded here <a href="http://www.mediafire.com/?aruy99z1y54prn4" target="_blank">MVC2JQuery at MediaFire</a></p>
<p>2. Open <strong>RegisterViewModel </strong>and modified it as below:</p>
<hr />
<p><span style="color:#0000ff;">public</span> <span style="color:#0000ff;"><span style="color:#0000ff;">class</span></span> <span style="color:#2b91af;">RegisterViewModel<br />
</span>{<br />
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;"><span style="color:#0000ff;">string</span></span> FirstName { <span style="color:#0000ff;"><span style="color:#0000ff;">get</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">set</span></span> }<br />
<span style="color:#0000ff;"> public</span> <span style="color:#0000ff;"><span style="color:#0000ff;">string</span></span> LastName { <span style="color:#0000ff;"><span style="color:#0000ff;">get</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">set</span></span> }<br />
<span style="color:#0000ff;"><span style="color:#0000ff;">public</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">int</span></span> NumberOfPosts { <span style="color:#0000ff;">get</span> <span style="color:#0000ff;"><span style="color:#0000ff;">set</span></span> }<br />
<span style="color:#0000ff;">public</span> <span style="color:#0000ff;"><span style="color:#0000ff;">string</span></span> LastPost { <span style="color:#0000ff;"><span style="color:#0000ff;">get</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">set</span></span> }<br />
}</p>
<hr />
<p>3. Open <strong>HomeController</strong> and make changes as shown below:</p>
<hr />
<p>[<span style="color:#2b91af;">HttpPost</span>]<br />
<span style="color:#0000ff;"><span style="color:#0000ff;">public</span></span> <span style="color:#2b91af;"><span style="color:#2b91af;">ActionResult</span></span> Index(<span style="color:#2b91af;"><span style="color:#2b91af;">RegisterViewModel</span></span> data)<br />
{</p>
<p><span style="color:#008000;"><span style="color:#008000;">// here you can save data to database</span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;">// and return some feedback to the user</span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p>data.NumberOfPosts = 5;</p>
<p>data.LastPost = <span style="color:#a31515;"><span style="color:#a31515;">&#8220;This is my last post&#8221;</span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;">if</span></span>(Request.IsAjaxRequest())</p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> return</span></span> Json(data);  <span style="color:#008000;"><span style="color:#008000;">// here data object is being converted to JSON format</span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p><span style="color:#008000;"><span style="color:#008000;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;">else</span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"> return</span> Content(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;Please enable JavaScript&#8221;</span></span>);</p>
<p>}</p>
<hr />
<p>4. Also modify Views/<strong>Index</strong> accordingly</p>
<p>In the <head> part:</p>
<hr />
<p><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">type</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;text/javascript&#8221;</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">src</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><%=Url.Content("~/Scripts/jquery-1.4.4.min.js")%><span style="color:#0000ff;"><span style="color:#0000ff;">&#8220;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">type</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;text/javascript&#8221;</span></span> <span style="color:#ff0000;">src</span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><%=Url.Content("~/Scripts/jquery.form.js")%><span style="color:#0000ff;"><span style="color:#0000ff;">&#8220;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span><br />
<span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">type</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;text/javascript&#8221;></span></span></p>
<p>$(document).ready(<span style="color:#0000ff;"><span style="color:#0000ff;">function</span></span>() {<br />
<span style="color:#0000ff;"><span style="color:#0000ff;"> var</span></span> options = {<br />
beforeSubmit: showRequest, // call this function before sending data to the server<br />
success: showResponse, // call this function once we have a response form the server<br />
type: <span style="color:#a31515;"><span style="color:#a31515;">&#8216;post&#8217;</span></span>,<br />
dataType: <span style="color:#a31515;"><span style="color:#a31515;">&#8216;json&#8217;</span></span>, <span style="color:#008000;"><span style="color:#008000;">// important! expecting data type is JSON<br />
</span></span> resetForm: <span style="color:#0000ff;"><span style="color:#0000ff;">true<br />
</span></span>};</p>
<p>$(<span style="color:#a31515;"><span style="color:#a31515;">&#8216;#form-user&#8217;</span></span>).ajaxForm(options);<br />
});</p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;">function</span></span> showRequest(formData, jqForm, options) {<br />
$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#result-user&#8221;</span></span>).empty().html(<span style="color:#a31515;"><span style="color:#a31515;">&#8216;Loading&#8230;.&#8217;</span></span>); <span style="color:#008000;"><span style="color:#008000;">// display loading<br />
</span></span>$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#form-user :input&#8221;</span></span>).attr(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;disabled&#8221;</span></span>, <span style="color:#0000ff;"><span style="color:#0000ff;">true</span></span>); <span style="color:#008000;"><span style="color:#008000;">// disable inputs<br />
</span></span>}</p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;">function</span></span> showResponse(data, statusText, xhr, $form) {<br />
$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#result-user&#8221;</span></span>).empty();<br />
$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#form-user :input&#8221;</span></span>).attr(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;disabled&#8221;</span></span>, <span style="color:#0000ff;"><span style="color:#0000ff;">false</span></span>);<br />
<span style="color:#008000;"> // here &#8216;data&#8217; is an object. See below how it is used:<br />
</span>$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#welcome&#8221;</span></span>).html(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;Hello, &#8220;</span></span> + data.FirstName + <span style="color:#a31515;"><span style="color:#a31515;">&#8221; &#8220;</span></span> + data.LastName);<br />
$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#number&#8221;</span></span>).html(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;Number of posts: &#8220;</span></span> + data.NumberOfPosts);<br />
$(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;#lastpost&#8221;</span></span>).html(data.LastPost);<br />
}<br />
<span style="color:#0000ff;"><span style="color:#0000ff;"></</span></span><span style="color:#a31515;"><span style="color:#a31515;">script</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<hr />
<p>In the <body> part:</p>
<hr />
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">body</span></span><span style="color:#0000ff;">><br />
</span><span style="color:#0000ff;"><%</span> <span style="color:#0000ff;"><span style="color:#0000ff;">using</span></span> (Html.BeginForm(<span style="color:#a31515;"><span style="color:#a31515;">&#8220;Index&#8221;</span></span>,<span style="color:#a31515;"><span style="color:#a31515;">&#8220;Home&#8221;</span></span>,<span style="color:#2b91af;"><span style="color:#2b91af;">FormMethod</span></span>.Post, <span style="color:#0000ff;"><span style="color:#0000ff;">new</span></span> { id=<span style="color:#a31515;"><span style="color:#a31515;">&#8220;form-user&#8221;</span></span>, name=<span style="color:#a31515;"><span style="color:#a31515;">&#8220;form-user&#8221;</span></span>})) {%></p>
<p><<span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">style</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><span style="color:#ff0000;"><span style="color:#ff0000;">float</span></span>:<span style="color:#0000ff;"><span style="color:#0000ff;">left&#8221;</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">id</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;welcome&#8221;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">style</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><span style="color:#ff0000;"><span style="color:#ff0000;">float</span></span>:<span style="color:#0000ff;"><span style="color:#0000ff;">right&#8221;</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">id</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;number&#8221;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">style</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><span style="color:#ff0000;"><span style="color:#ff0000;">clear</span></span>:<span style="color:#0000ff;"><span style="color:#0000ff;">both</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">&#8220;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">fieldset</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">legend</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span>Fields<span style="color:#0000ff;"><span style="color:#0000ff;"></</span></span><span style="color:#a31515;"><span style="color:#a31515;">legend</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><br />
<</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">class</span></span><span style="color:#0000ff;">=&#8221;editor-label&#8221;><br />
<%=</span> Html.LabelFor(model => model.FirstName) %><br />
</<span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><br />
<</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">class</span></span><span style="color:#0000ff;">=&#8221;editor-field&#8221;><br />
<%=</span> Html.TextBoxFor(model => model.FirstName) %><br />
<%<span style="color:#0000ff;"><span style="color:#0000ff;">=</span></span> Html.ValidationMessageFor(model => model.FirstName) %><br />
</<span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><br />
<</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">class</span></span><span style="color:#0000ff;">=&#8221;editor-label&#8221;><br />
<%=</span> Html.LabelFor(model => model.LastName) %><br />
</<span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">class</span></span><span style="color:#0000ff;">=&#8221;editor-field&#8221;><br />
<%=</span> Html.TextBoxFor(model => model.LastName) %><br />
<%<span style="color:#0000ff;"><span style="color:#0000ff;">=</span></span> Html.ValidationMessageFor(model => model.LastName) %><br />
</<span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">p</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"> <</span></span><span style="color:#a31515;"><span style="color:#a31515;">input</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">type</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;submit&#8221;</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">value</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;Save&#8221;</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">/><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"></</span></span><span style="color:#a31515;"><span style="color:#a31515;">p</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"></</span><span style="color:#a31515;"><span style="color:#a31515;">fieldset</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">></span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><span style="color:#0000ff;"><span style="color:#0000ff;"> </span></span></p>
<p><% } %></p>
<p><<span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">id</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;result-user&#8221;></</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;"><</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">style</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;</span></span><span style="color:#ff0000;"><span style="color:#ff0000;">border</span></span>:<span style="color:#0000ff;"><span style="color:#0000ff;">solid</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">1px</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">#ccc</span></span><span style="color:#ff0000;"><span style="color:#ff0000;">margin</span></span>:<span style="color:#0000ff;"><span style="color:#0000ff;">10px</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">&#8220;</span></span> <span style="color:#ff0000;"><span style="color:#ff0000;">id</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">=&#8221;lastpost&#8221;</span></span> <span style="color:#0000ff;"><span style="color:#0000ff;">></</span></span><span style="color:#a31515;"><span style="color:#a31515;">div</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">><br />
</</span></span><span style="color:#a31515;"><span style="color:#a31515;">body</span></span><span style="color:#0000ff;">></span></p>
<hr />
<p>5. And we are done! Hit F5 and see result:</p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_json_01.gif" border="0" alt="ASP.NET MVC2 JQuery JSON Form Post" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_json_02.gif" border="0" alt="" /></p>
<p><strong>Here you can download complete source code:</strong> <a href="http://www.mediafire.com/?ul8wl6o2fe47qk4" target="_blank">http://www.mediafire.com/?ul8wl6o2fe47qk4</a></p>
<p>Good Luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Publishing ASP.NET MVC2 Application on Windows Server 2008 IIS 7 with Visual Studio 2008 on platform .NET 3.5 SP1</title>
		<link>http://arturito.net/2011/01/21/publishing-asp-net-mvc2-application-on-windows-server-2008-iis-7-with-visual-studio-2008-on-platform-net-3-5-sp1/</link>
		<comments>http://arturito.net/2011/01/21/publishing-asp-net-mvc2-application-on-windows-server-2008-iis-7-with-visual-studio-2008-on-platform-net-3-5-sp1/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 08:45:47 +0000</pubDate>
		<dc:creator>guru</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>

		<guid isPermaLink="false">http://dartharturito.wordpress.com/?p=484</guid>
		<description><![CDATA[TweetPrepare you server:
1. Go to Server Manager and select Add Roles, and install Web Server



2. Mark ASP.NET and install



3. Confirm the CLR of ASP.NET has been installed

4. Install .NET Framework 3.5 and .NET Framework 3.5 SP1

5. Let&#8217;s create a directory where we stick our MVC2 application. In my case it is C:\www\windows.amk
6. Once you are [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton484" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2011%2F01%2F21%2Fpublishing-asp-net-mvc2-application-on-windows-server-2008-iis-7-with-visual-studio-2008-on-platform-net-3-5-sp1%2F&amp;text=Publishing%20ASP.NET%20MVC2%20Application%20on%20Windows%20Server%202008%20IIS%207%20with%20Visual%20Studio%202008%20on%20platform%20.NET%203.5%20SP1&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2011%2F01%2F21%2Fpublishing-asp-net-mvc2-application-on-windows-server-2008-iis-7-with-visual-studio-2008-on-platform-net-3-5-sp1%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span style="font-size:medium;">Prepare you server:</span></p>
<p>1. Go to <strong>Server Manager</strong> and select<strong> Add Roles</strong>, and install <strong>Web Server</strong></p>
<p><img style="width:686px;height:439px;" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_01.jpg" border="0" alt="" width="773" height="578" /></p>
<p><img style="width:683px;height:475px;" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_02.jpg" border="0" alt="" width="676" height="564" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_03.jpg" border="0" alt="" /></p>
<p>2. Mark <strong>ASP.NET</strong> and install</p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_04.jpg" border="0" alt="" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_05.jpg" border="0" alt="" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_06.jpg" border="0" alt="" /></p>
<p>3. Confirm the CLR of ASP.NET has been installed</p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_10.jpg" border="0" alt="" /></p>
<p>4. Install <strong>.NET Framework 3.5</strong> and <em><strong>.NET Framework 3.5 SP1</strong></em></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_11.jpg" border="0" alt="" /></p>
<p>5. Let&#8217;s create a directory where we stick our MVC2 application. In my case it is <em>C:\www\windows.amk</em></p>
<p>6. Once you are done go to the Security properties of that directory and add read and write permissions for group <strong>IIS_IUSRS </strong></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_12.jpg" border="0" alt="" /></p>
<p>7. Go back to <strong>IIS Manager</strong> and <strong>Add Site</strong></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_13.jpg" border="0" alt="" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_14.jpg" border="0" alt="" /></p>
<p>8. In <strong>Application Pool </strong>double click pool of your site and make sure that <strong>Managed pipeline mode</strong> is <strong>Integrated</strong></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_15.jpg" border="0" alt="" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_16.jpg" border="0" alt="" /></p>
<p><strong>Now the server is prepared to run MVC2 web application!</strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><span style="font-size:medium;">Prepare your project to be published</span></p>
<p>1. Go to your project properties and mark <strong>Release</strong></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_vs_15.jpg" border="0" alt="" /></p>
<p>2. Go to <strong>References</strong> and and change Property <strong>Copy Local</strong> to true for</p>
<p><em>System.ComponentModel.DataAnnotations.dll<br />
System.Web.Mvc.dll</em></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_vs_14.jpg" border="0" alt="" /></p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_vs_13.jpg" border="0" alt="" /></p>
<p>If you are running <strong>.NET Framework 3.5</strong> NOT <strong>.NET Framework 3.5 SP1 </strong>change properties for all these.</p>
<p><em>System.ComponentModel.DataAnnotations.dll<br />
System.Web.Abstractions.dll<br />
System.Web.Mvc.dll<br />
System.Web.Routing.dll</em></p>
<p>6. In <strong>Web.config</strong> change lines</p>
<pre class="brush: xml; title: ; notranslate">&lt;compilation debug=&quot;true&quot;&gt;</pre>
<p>to</p>
<pre class="brush: xml; title: ; notranslate">&lt;compilation debug=&quot;false&quot;&gt;</pre>
<p>And uncomment this bit:</p>
<pre class="brush: xml; title: ; notranslate">
        &lt;customerrors mode=&quot;RemoteOnly&quot; defaultredirect=&quot;GenericErrorPage.htm&quot;&gt;
            &lt;error statuscode=&quot;403&quot; redirect=&quot;NoAccess.htm&quot; /&gt;
            &lt;error statuscode=&quot;404&quot; redirect=&quot;FileNotFound.htm&quot; /&gt;
        &lt;/customerrors&gt;
</pre>
<p>&nbsp;</p>
<p>7. Generate and upload files to directory on the server in my case <em>c:\www\windows.amk </em></p>
<p>* What we need:<br />
- Configuration and settings files (e.g., Web.config and any *.settings files)<br />
- Uncompiled view templates (*.aspx, *.ascx, and *.Master)<br />
- Global.asax (this tells ASP.NET which compiled class represents your global HttpApplication)<br />
- Any static files (e.g., images, CSS files, and JavaScript files)<br />
- Optionally, the *.pdb files in your \bin folder, which enable extra debugging information (these are rarely deployed to production servers)</p>
<p>* We don&#8217;t need:</p>
<p>- C# code files (*.cs, including Global.asax.cs or any other &#8216;code behind&#8217; files)<br />
- Project and solution files (*.sln, *.suo, *.csproj, or *.csproj.user)<br />
- The \obj folder<br />
- Anything specific to your source control system (e.g., .svn folders if you use Subversion, or the .hg or .git folders if you use Mercurial or Git)</p>
<p><img src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_publish_vs_16.jpg" border="0" alt="" /></p>
<p>Good Luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2011/01/21/publishing-asp-net-mvc2-application-on-windows-server-2008-iis-7-with-visual-studio-2008-on-platform-net-3-5-sp1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Google Maps API to mark multiple addresses fetched from database ASP.NET MVC2 and Geocoder</title>
		<link>http://arturito.net/2010/12/13/using-google-maps-api-to-mark-multiple-addresses-fetched-from-database-asp-net-mvc2-and-geocoder/</link>
		<comments>http://arturito.net/2010/12/13/using-google-maps-api-to-mark-multiple-addresses-fetched-from-database-asp-net-mvc2-and-geocoder/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 09:06:15 +0000</pubDate>
		<dc:creator>guru</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=446</guid>
		<description><![CDATA[TweetThis tutorial will cover marking multiple addresses stored in database on Google Maps.  For example this can be useful for marking chain of shops or restaurants or others.
The idea is to get something like this:

Before starting it we have to obtain a key for using their API.
You can do it here:
http://code.google.com/apis/maps/signup.html
Generate your key and save [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton446" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2010%2F12%2F13%2Fusing-google-maps-api-to-mark-multiple-addresses-fetched-from-database-asp-net-mvc2-and-geocoder%2F&amp;text=Using%20Google%20Maps%20API%20to%20mark%20multiple%20addresses%20fetched%20from%20database%20ASP.NET%20MVC2%20and%20Geocoder&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2010%2F12%2F13%2Fusing-google-maps-api-to-mark-multiple-addresses-fetched-from-database-asp-net-mvc2-and-geocoder%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>This tutorial will cover marking multiple addresses stored in database on Google Maps.  For example this can be useful for marking chain of shops or restaurants or others.<br />
The idea is to get something like this:</p>
<p><img class="alignnone" title="MVC2 Google Maps API" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/03-maps-mvc2.gif" alt="" width="544" height="315" /></p>
<p>Before starting it we have to obtain a key for using their API.<br />
You can do it here:<br />
<a href="http://code.google.com/apis/maps/signup.html" target="_blank">http://code.google.com/apis/maps/signup.html</a></p>
<p>Generate your key and save it</p>
<p>1. Create new Empty MVC2 project<br />
2. Insert new <strong>.ashx</strong> file call it <strong>Maps.ashx</strong>.</p>
<p>I placed mine the folder <strong>Services</strong> which I created before.</p>
<p><img class="alignnone" title="MVC2 Google Maps API" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/01-maps-mvc2.gif" alt="" width="274" height="195" /></p>
<p>3. Insert this code</p>
<pre class="brush: csharp; title: ; notranslate">

[WebService(Namespace = &quot;http://tempuri.org/&quot;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Maps : IHttpHandler    {

public void ProcessRequest(HttpContext context)        {
context.Response.ContentType = &quot;text/xml&quot;;
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
       string sXml = createMarkers();
       context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Write(sXml);
}
public bool IsReusable {
        get { return true; }
}

private string createMarkers() {
// Create the xml document container
XmlDocument doc = new XmlDocument();//
    Create the XML Declaration, and append it to XML document
    XmlDeclaration dec = doc.CreateXmlDeclaration(&quot;1.0&quot;, null, null);
    doc.AppendChild(dec);// Create the root element
    XmlElement root = doc.CreateElement(&quot;markers&quot;);
    doc.AppendChild(root);
 // Here you can create a loop and fetching data from database like
  /*  while(rdr.Read()) {
XmlElement marker = doc.CreateElement(&quot;marker&quot;);
marker.SetAttribute(&quot;name&quot;,  rdr[&quot;name&quot;].ToString());
marker.SetAttribute(&quot;address&quot;, rdr[&quot;address&quot;].ToString());
                   root.AppendChild(marker);
      }
*/

// To retrieve data using a C# go here:
// http://www.akadia.com/services/dotnet_data_reader.html
// We will enter it manually

 XmlElement marker = doc.CreateElement(&quot;marker&quot;);
marker.SetAttribute(&quot;name&quot;, &quot;L'arruzz&quot;);
 marker.SetAttribute(&quot;address&quot;, &quot;C/ PORTUGAL, 35, 03003 ALICANTE&quot;);
root.AppendChild(marker);

 XmlElement marker2 = doc.CreateElement(&quot;marker&quot;);
 marker2.SetAttribute(&quot;name&quot;, &quot;CERVECERIA VICTOR&quot;);
marker2.SetAttribute(&quot;address&quot;, &quot;C/ SAN AGATÁNGELO, 37, 03007 ALICANTE&quot;);
root.AppendChild(marker2);
return doc.OuterXml;
}
}
</pre>
<p>4. Next step is to create HomeController and create Index View.</p>
<pre class="brush: csharp; title: ; notranslate">
public ActionResult Index()
{            return View();
}
</pre>
<p>5. Now run your project and check if xml is being genrated by go to url of ashx file:</p>
<p>http://localhost:your_port_number/Services/Maps.ashx</p>
<p>For example in my case:http://localhost:1951/Services/Maps.ashx</p>
<p><img class="alignnone" title="Google Maps API MVC2 " src="http://i1134.photobucket.com/albums/m614/Arturito_Net/02-maps-mvc2.gif" alt="" width="514" height="211" /></p>
<p>6. Go back your <strong>HomeController </strong>and right click and create Index View. Your code should be the same as below:</p>
<pre class="brush: jscript; html-script: true; title: ; notranslate">
&lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewPage&quot; %&gt;

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;

&lt;head runat=&quot;server&quot;&gt;
&lt;title&gt;Index&lt;/title&gt;

&lt;script src=&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=here_we_enter_google_api_key&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
 &lt;script type=&quot;text/javascript&quot;&gt;
 //&lt;![CDATA[
 var map = null;
 var geocoder = null;
 function load() {
 if (GBrowserIsCompatible()) {
 map = new GMap2(document.getElementById(&quot;map&quot;));
 map.addControl(new GSmallMapControl());

geocoder = new GClientGeocoder();

// Here enter your url of ashx file

GDownloadUrl(&quot;http://localhost:1951/Services/Maps.ashx&quot;, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName(&quot;marker&quot;);
for (var i = 0; i &lt; markers.length; i++) {
 var name = markers[i].getAttribute(&quot;name&quot;);
 var address = markers[i].getAttribute(&quot;address&quot;);
 showAddress(address,name);
}
});

}
}

function showAddress(address, name) {
if (geocoder) {
geocoder.getLatLng(
name+&quot;,&quot;+address,
function(point) {
if (!point) {
 alert(address + &quot; not found&quot;);
} else {
map.setCenter(point, 13);
var marker = createMarker(point, name, address);
map.addOverlay(marker);
}
});

}

}

function createMarker(point, name, address) {
var marker = new GMarker(point);
var html = &quot;&lt;b&gt;&quot; + name + &quot;&lt;/b&gt; &lt;br/&gt;&quot; + address;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});

return marker;
}

//]]&gt;

&lt;/script&gt;

&lt;/head&gt;

&lt;body onload=&quot;load()&quot; onunload=&quot;GUnload()&quot;&gt;
&lt;div&gt;
&lt;div id=&quot;map&quot; style=&quot;width: 1000px; height: 600px&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Run it and there we go!</p>
<p><img class="alignnone" title="MVC2 Google Maps API" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/03-maps-mvc2.gif" alt="" width="544" height="315" /></p>
<p>You can also do it without ashx and return xml as an ActionResult</p>
<pre class="brush: csharp; title: ; notranslate">
public ActionResult PrintMarkers()  {
    XmlDocument doc = new XmlDocument();//
    Create the XML Declaration, and append it to XML document
    XmlDeclaration dec = doc.CreateXmlDeclaration(&quot;1.0&quot;, null, null);
    doc.AppendChild(dec);// Create the root element
    XmlElement root = doc.CreateElement(&quot;markers&quot;);
    doc.AppendChild(root);

 XmlElement marker = doc.CreateElement(&quot;marker&quot;);
 marker.SetAttribute(&quot;name&quot;, &quot;L'arruzz&quot;);
 marker.SetAttribute(&quot;address&quot;, &quot;C/ PORTUGAL, 35, 03003 ALICANTE&quot;);
 root.AppendChild(marker);

 XmlElement marker2 = doc.CreateElement(&quot;marker&quot;);
 marker2.SetAttribute(&quot;name&quot;, &quot;CERVECERIA VICTOR&quot;);
marker2.SetAttribute(&quot;address&quot;, &quot;C/ SAN AGATÁNGELO, 37, 03007 ALICANTE&quot;);
root.AppendChild(marker2);
return doc.OuterXml;
}
</pre>
<p>And simply call it from script of JavaScript:</p>
<pre class="brush: jscript; title: ; notranslate">
 GDownloadUrl(&quot;http://localhost:1951/Home/PrintMarkers/&quot;, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName(&quot;marker&quot;);
 for (var i = 0; i &lt; markers.length; i++) {
var name = markers[i].getAttribute(&quot;name&quot;);
var address = markers[i].getAttribute(&quot;address&quot;);
showAddress(address,name);
}
});  </pre>
<p>Will do the same.</p>
<p>Good luck! <img src='http://arturito.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2010/12/13/using-google-maps-api-to-mark-multiple-addresses-fetched-from-database-asp-net-mvc2-and-geocoder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC2 jQuery Form Post Tutorial</title>
		<link>http://arturito.net/2010/12/02/asp-net-mvc2-jquery-form-post-tutorial/</link>
		<comments>http://arturito.net/2010/12/02/asp-net-mvc2-jquery-form-post-tutorial/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 20:11:13 +0000</pubDate>
		<dc:creator>guru</dc:creator>
				<category><![CDATA[ASP.NET MVC2]]></category>
		<category><![CDATA[C sharp]]></category>

		<guid isPermaLink="false">http://arturito.net/?p=429</guid>
		<description><![CDATA[TweetHere is a little tutorial that I wrote about posting form using jQuery and Form plugin.I&#8217;m using Visual Studio 2008.
If you want to see how to post form with JSON check my other post:
http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/
1. Open Visual Studio
2. Create MVC2 Empty Web Application

3. Right click Controllers in Solution Explorer and select Add Controller
4. Name it HomeController


5. Right Click [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton429" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Farturito.net%2F2010%2F12%2F02%2Fasp-net-mvc2-jquery-form-post-tutorial%2F&amp;text=ASP.NET%20MVC2%20jQuery%20Form%20Post%20Tutorial&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Farturito.net%2F2010%2F12%2F02%2Fasp-net-mvc2-jquery-form-post-tutorial%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://arturito.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Here is a little tutorial that I wrote about posting form using jQuery and Form plugin.I&#8217;m using Visual Studio 2008.<br />
If you want to see how to post form with JSON check my other post:<br />
<a href="http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/">http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/</a></p>
<p>1. Open Visual Studio</p>
<p>2. Create MVC2 Empty Web Application</p>
<p><img class="alignnone" title="MVC2 Empty Web Application" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_01.gif" alt="" width="540" height="522" /></p>
<p>3. Right click <strong>Controllers</strong> in <strong>Solution Explorer</strong> and select <strong>Add Controller</strong></p>
<p>4. Name it <em>HomeController<br />
</em><br />
<img class="alignnone" title="MVC2 Add Controller " src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_02.gif" alt="" width="425" height="178" /></p>
<p>5. Right Click on<strong> Models</strong> in <strong>Solution Explorer</strong> and select<strong> Add Class&#8230;</strong></p>
<p>6.  Name it <em>RegisterViewModel<br />
</em><br />
<img class="alignnone" title="MVC2 Add Class" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_03.gif" alt="" width="546" height="331" /></p>
<p>Insert this piece of code:</p>
<pre class="brush: csharp; title: ; notranslate">
namespace MVC2JQuery.Models
{
    public class RegisterViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
</pre>
<p>7. In the Toolbar click <strong>Build</strong>-><strong>Build Solution</strong></p>
<p><strong> </strong></p>
<p><strong> </strong> 8. Go back to <strong>HomeController</strong> and right click on <strong>Index()</strong> and select <strong>Add View &#8230;</strong></p>
<pre class="brush: csharp; title: ; notranslate">
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index() // &lt;-- right click here
        {
            return View();
        }

    }
</pre>
<p><strong><br />
</strong><img class="alignnone" title="MVC2 Add View" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_04.gif" alt="" width="382" height="384" /></p>
<p>9. You will be taken to your <strong>Index.aspx</strong></p>
<p><strong> </strong></p>
<p><strong> </strong> 10. Now run the application by hitting <strong>F5</strong> (Click ok on &#8220;Debugging Not Enabled&#8221; with option &#8220;Modify&#8230;&#8221;)<strong><br />
</strong></p>
<p>11. Now, let&#8217;s make it do something. Stop Debugging and go back to <strong>Home Controller</strong> and add Action:</p>
<pre class="brush: csharp; title: ; notranslate">
        [HttpPost]
        public ActionResult Index(RegisterViewModel data)
        {
            // here you can save data to database
            // and return some feedback to the user
            return Content(&quot;You submitted: &quot; +  data.FirstName + &quot; &quot; + data.LastName);
        }
</pre>
<p>12. Your<strong> HomeController</strong> will look like this:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC2JQuery.Models;

namespace MVC2JQuery.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(RegisterViewModel data)
        {
            // here you can save data to database
            // and return some feedback to the user
            return Content(&quot;You submitted: &quot; +  data.FirstName + &quot; &quot; + data.LastName);
        }

    }
}
</pre>
<p><strong>Build</strong> project and <strong>Run</strong> it.</p>
<p><img class="alignnone" title="MVC2 Form Submit" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_05.gif" alt="" width="532" height="377" /></p>
<p><img class="alignnone" title="MVC2 Form Submit" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_06.gif" alt="" width="532" height="377" /></p>
<p>13. Now go to<strong> Scripts</strong> in <strong>Solution Explorer</strong> and remove all file that are in there. We won&#8217;t be using them.</p>
<p>14. Download:</p>
<p>jQuery:<br />
<a title="jQuery 1.4.4" href="http://code.jquery.com/jquery-1.4.4.min.js" target="_blank"> http://code.jquery.com/jquery-1.4.4.min.js</a></p>
<p>jQuery Form Plugin:<br />
<a title="https://github.com/malsup/form/raw/master/jquery.form.js" href="https://github.com/malsup/form/raw/master/jquery.form.js" target="_blank">https://github.com/malsup/form/raw/master/jquery.form.js</a></p>
<p>Save them in<strong> Scripts</strong> folder of your project.</p>
<p><img class="alignnone" title="MVC2 Save in Scripts" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_07.gif" alt="" width="590" height="381" /></p>
<p><img class="alignnone" title="MVC2 Save in Scripts" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_08.gif" alt="" width="590" height="381" /><br />
15. Go to <strong>Views</strong> and chaage<strong> Index.aspx</strong> as below:</p>
<pre class="brush: csharp; html-script: true; title: ; notranslate">
&lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewPage&lt;mvc2JQuery.Models.RegisterViewModel&gt;&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;Index&lt;/title&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;%=Url.Content(&quot;~/Scripts/jquery-1.4.4.min.js&quot;)%&gt;&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;%=Url.Content(&quot;~/Scripts/jquery.form.js&quot;)%&gt;&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        $(document).ready(function() {
            var options = {
                target: '#result-user', // id of the div where we are going to display result
                beforeSubmit: showRequest,
                success: showResponse,
                type: 'post',
                resetForm: true
            };
            $('#form-user').ajaxForm(options); // id of the form we wish to submit
        });

        function showRequest(formData, jqForm, options) {
            $(&quot;#result-user&quot;).empty().html('Loading....');
            $(&quot;#form-user :input&quot;).attr(&quot;disabled&quot;, true); // disable all form inputs while loading
        }

        function showResponse(responseText, statusText, xhr, $form) {
            $(&quot;#result-user&quot;).empty().html(responseText);
            $(&quot;#form-user :input&quot;).attr(&quot;disabled&quot;, false);
        }
    &lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
    &lt;% using (Html.BeginForm(&quot;Index&quot;,&quot;Home&quot;,FormMethod.Post, new { id=&quot;form-user&quot;, name=&quot;form-user&quot;})) {%&gt;

        &lt;fieldset&gt;
            &lt;legend&gt;Fields&lt;/legend&gt;

            &lt;div class=&quot;editor-label&quot;&gt;
                &lt;%= Html.LabelFor(model =&gt; model.FirstName) %&gt;
            &lt;/div&gt;
            &lt;div class=&quot;editor-field&quot;&gt;
                &lt;%= Html.TextBoxFor(model =&gt; model.FirstName) %&gt;
                &lt;%= Html.ValidationMessageFor(model =&gt; model.FirstName) %&gt;
            &lt;/div&gt;

            &lt;div class=&quot;editor-label&quot;&gt;
                &lt;%= Html.LabelFor(model =&gt; model.LastName) %&gt;
            &lt;/div&gt;
            &lt;div class=&quot;editor-field&quot;&gt;
                &lt;%= Html.TextBoxFor(model =&gt; model.LastName) %&gt;
                &lt;%= Html.ValidationMessageFor(model =&gt; model.LastName) %&gt;
            &lt;/div&gt;

            &lt;p&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Save&quot; /&gt;
            &lt;/p&gt;
        &lt;/fieldset&gt;

    &lt;% } %&gt;
    &lt;div id=&quot;result-user&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>16. Let&#8217;s go back to our <strong>HomeController</strong> and amend <strong>Index</strong> action:</p>
<pre class="brush: csharp; title: ; notranslate">
        [HttpPost]
        public ActionResult Index(RegisterViewModel data)
        {
            // here you can save data to database
            // and return some feedback to the user

            if(Request.IsAjaxRequest())
                return Content(&quot;You submitted: &quot; +  data.FirstName + &quot; &quot; + data.LastName + &quot; using jQuery&quot;);
            else
                return Content(&quot;You submitted: &quot; + data.FirstName + &quot; &quot; + data.LastName);
        }
</pre>
<p>17.<strong> Build</strong> and<strong> Run</strong></p>
<p><img class="alignnone" title="MVC2 jQuery Form Submit" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_09.gif" alt="" width="372" height="349" /></p>
<p><img class="alignnone" title="MVC2 jQuery Form Submit" src="http://i1134.photobucket.com/albums/m614/Arturito_Net/mvc2_jquery_10.gif" alt="" width="372" height="349" /></p>
<p><strong><br />
<a title="MediaFire Download" href="http://www.mediafire.com/?aruy99z1y54prn4" target="_blank"> Here</a> you can download working example</strong></p>
<p><strong> </strong></p>
<p><strong><br />
Note:</strong> If you are not getting the same result make sure that:<br />
1.Ids are equal<br />
$(&#8216;#<strong>form-user</strong>&#8216;).ajaxForm(options);<br />
Html.BeginForm(&#8220;Index&#8221;,&#8221;Home&#8221;,FormMethod.Post, new { id=&#8221;<strong>form-user</strong>&#8220;, name=&#8221;<strong>form-user</strong>&#8220;})</p>
<p>$(&#8220;#<strong>result-user</strong>&#8220;).empty().html(responseText);</p>
<div id="<strong>result-user</strong>&#8220;></div>
<p>Keep in mind that when you set an id=&#8221;<strong>result-user</strong>&#8221; in jQuery you reference it as $(&#8216;<strong>#form-user</strong>&#8216;)<br />
Don&#8217;t forget about the hash.</p>
<p>2. Once you launched check if scripts are accessible:</p>
<p>http://localhost:49782/Scripts/jquery-1.4.4.min.js</p>
<p>http://localhost:49782/Scripts/jquery.form.js</p>
<p>* replace port 49782 with yours</p>
<p>Good Luck! <img src='http://arturito.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Also check my other tutorial on posting form using jQuery and JSON:<br />
<a href="http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/">http://arturito.net/2011/01/25/asp-net-mvc2-jquery-form-post-with-json-tutorial/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://arturito.net/2010/12/02/asp-net-mvc2-jquery-form-post-tutorial/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

