Home » Uncategorized » Co-op Highlights 13

Co-op Highlights 13

With regard to website development at my organization, there’s been a term, MVC, that has surfaced several times recently.  It seems that this is something that they are working towards with their web application.  I thought I would do a little research about it as I have very little knowledge about MVC.

MVC stands for Model View Controller and has actually been around for a very long time, originally known as Thing-Model-View-Editor in 1979.  It seems that MVC is an architectural pattern which puts a web application in 3 different buckets:  Model, View, and Controller.

  • The Model represents the logical structure of data in the application, along with the classes and rules for how data is changed.
  • The View is the user interface (UI) – it requests information from the Model and generates an output representation to the user.
  • The Controller sends commands to the model which updates the model’s state – it also sends commands to an associated view which can change the view’s presentation of the model (output to the user).

If you have an existing web application, you can upgrade it by adding MVC (I believe this would be the case at my work, as we have an existing web application and would want to transform it to an MVC platform).  I found this website (http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx) which is Scott Hanselman’s blog.  Scott Hanselman is also one of the authors of the Professional ASP.NET 4 book, a book I am using in another class this semester.  This particular post from January 2011 addresses the exact question – how to add MVC to an existing website.

This article seems to indicate that you could create a default ASP.NET MVC application as a reference tool and then use something like Beyond Compare (at http://scootersoftware.com/) to compare your current web application and the reference ASP.NET MVC application to determine the differences.  You then would have an option to copy over missing files or folders into your current web application project.

The article is pretty simple and straightforward – it almost seems too easy, so I’m curious if there is a lot more to this integration.  There seems to be some difference between converting a ‘website’ to incorporate MVC and converting a ‘web application’ to incorporate MVC.  I believe Scott Hanselman’s article talks specifically about a web application.  I found another website (http://jefferytay.wordpress.com/2012/04/10/getting-asp-net-mvc-3-to-work-with-asp-net-website-project/) that gives a pretty easy-to-follow example about how to add MVC to a website.

The first step seems to be making sure that you have ASP.NET MVC 3 installed (I found information here on how to do this).

The big next step concerns modifying your web.config file.  This specifically adds to the file:

<compilation><assemblies> section

<add assembly=”System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ /> 
<add assembly=”System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ /> 
<add assembly=”System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ />

The third step in this process is modifying the global.asax file.  You would add:

<%@ Import Namespace=”System.Web.Mvc” %> 
<%@ Import Namespace=”System.Web.Routing” %>

Add the following after <script runat=”server”>

public void RegisterGlobalFilters(GlobalFilterCollection filters) 

filters.Add(new HandleErrorAttribute()); 
}

public static void RegisterRoutes(RouteCollection routes) 

routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);

routes.MapRoute(“Home”, 
“home/{action}/{id}”, 
new { controller = “Home”, action = “Index”, id = UrlParameter.Optional });}

add the following inside application_start

RegisterGlobalFilters(GlobalFilters.Filters); 
RegisterRoutes(RouteTable.Routes);

after the <%@ Application Language=”C#” %> section.

Creating the Controller is the fourth step in this conversion process.  As a website project, the compilation happens at runtime, so the controllers are created inside the App_Code folder, instead of creating the Controller folder.  All Controllers need to end with the suffix of ‘Controller.’

Lastly, you would test your site.  Another post in StackOverflow suggests transitioning your website to a web application and then adding the MVC functionality.

Another Stack Overflow comment seemed to indicate that Scott Hanselman has updated this transition piece with a NuGet package to make conversion easier.  The steps include installing MVC3 (w/NuGet), selecting Library Package Manager through the Tools menu in Visual Studio, changing the Default Project to your web forms project, and typing ‘Install-Package AddMvc3ToWebForms.  This seems to indicate that it will add all of the necessary dlls, javascript files, and web.config settings to your website.  This website (http://delradiesdev.blogspot.com/2011/08/adding-mvc-3-to-aspnet-web-site.html) talks about this transfer in further detail.

One thing that I am also very curious about is the ‘why’ behind a lot of this.  Why would I want to transition to MVC?  Why would I chose a website vs. a web application vs. a MVC web project?  I still haven’t figured out the answers to those questions, but may post on this again if I find out more information.

One thought on “Co-op Highlights 13

Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s