ASP.NET MVC for the php/asp noob

I was talking to a friend today, who's foremost a php developer, about his thoughts on Umbraco and he said "Well they're apparently working feverishly on the new version of Umbraco, which will be MVC... which i still don't know what that means, but I know you like it."

I ended up giving him a ground up explanation of ASP.NET MVC, so I'm posting this so he can link this to his friends and for anyone else who finds it useful.  The whole goal was to be as simple as possible, not being focused on proper syntax.

Model-View-Controller (or MVC) is just a pattern that is used for handling UI interaction with your backend.  In a typical web app, you can imagine the *M*odel as your database model, the *V*iew as your HTML page, and the *C*ontroller as the class inbetween. 

MVC handles your web request different than your typical php/asp app.
In your php/asp app, your url maps directly to a php/asp file that contains html, mixed with database access code and redirects.
In an MVC app, your url route is mapped to a method on a class (the controller).  The body of this method can do some database access and THEN decide which *V*iew (html/aspx page) should be displayed;  putting the controller in charge and not the view... a clear seperation of concerns that provides better reusibility and generally promotes cleaner code.

Mysite.com, a quick example:
Let's say you hit the following url in your application: http://www.mysite.com/Product/ShowItem?Id=4

To avoid tedious configuration, MVC uses a lot of conventions by default. For instance, the above url in your app would automatically make MVC search for a .net class with the name "Product" and a method named "ShowItem" based on the pattern of the url.  So if you name things properly, your method would automatically be called when you entered the above url.  Additionally, it would automatically map/hydrate the "int id" parameter that was in your querystring, matched by name.
Product.cs
public class Product : Controller
{
    public ViewResult ShowItem(int id)
    {
        return View();
    }
}

From this point you can write the code in the body of this method to do some database access and then pass a "bag" (also known as the ViewData) of data to your chosen *V*iew (html page) to use for display.  The view(html) ONLY needs to be worried about displaying the flattened data that it's been given in the best way it can;  this allows the view to be reused throughout your application as *just* a view, and not be coupled to HOW the data for that view get's loaded..

Product.cs
public class Product : Controller
{
    public ViewResult ShowItem(int id)
    {
        var database = new Database();
        var item = database.GetItem(id);
        ViewData["TheItem"] = item;
        return View();
    }
}

Again by convention, since the class' method name is "ShowItem", it'll search for a view named "ShowItem.aspx" by default, and pass the ViewData bag to it to use.

ShowItem.aspx
<html>
     <body>
      <%
       var item =(Item)ViewData["TheItem"]
       %>
       <h1><%= item.FullProductName %></h1>
     </body>
</html>

BUT WAIT! WHY DOES MICROSOFT HAVE TO DO THINGS SO DIFFERENTLY!?
They aren't... here are some other frameworks you may have heard of that use the same pattern in a their own way:

  • Ruby On Rails
  • Grails
  • Spring MVC
  • Struts
  • Django

 

 

Published Tuesday, April 20, 2010 3:37 PM by dotjosh
Filed under: , ,

Comments

# re: ASP.NET MVC for the php/asp noob

Since he's a php dude, you might throw in CakePHP as an example of an MVC framework as well.

Wednesday, April 21, 2010 3:57 PM by paul.vencill

# ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

Thursday, April 22, 2010 11:50 PM by ASP.NET MVC Archived Blog Posts, Page 1

# re: ASP.NET MVC for the php/asp noob

It is rather interesting for me to read this blog. Thank author for it. I like such themes and anything connected to this matter. I definitely want to read more soon. BTW, rather nice design your site has, but how about changing it once in a few months?

Whitny Stone

Saturday, July 10, 2010 11:24 PM by outcall escorts

# re: ASP.NET MVC for the php/asp noob

Truly nice article to track it to my thinking. By the way, why haven't you you add that article to social media? That can bring a lot of traffic to this article.

Wednesday, July 21, 2010 9:03 PM by escort services female

# re: ASP.NET MVC for the php/asp noob

I would like to read more soon. By the way, pretty good design you have at that site, but what  do you think about changing it every few months?

Ella William

Saturday, July 24, 2010 6:41 AM by latinas escorts

# re: ASP.NET MVC for the php/asp noob

It is extremely interesting for me to read this blog. Thanks for it. I like such themes and everything connected to them. I definitely want to read more on that blog soon.

Julia Swenson

Tuesday, July 27, 2010 5:30 AM by pretty brunette girls

# re: ASP.NET MVC for the php/asp noob

Keep on posting such articles. I love to read articles like this. Just add more pics :)

Tuesday, August 31, 2010 5:53 PM by escort girl geneva

# re: ASP.NET MVC for the php/asp noob

Owner would like to thank                

<a href=http://www.mulberryoutlet2012.co.uk> Mulberry Bags</a>

Monday, October 24, 2011 1:50 PM by GoraSeeri

# re: ASP.NET MVC for the php/asp noob

Thos blogs must have been interesting!

Monday, October 31, 2011 10:10 AM by Deepak

# re: ASP.NET MVC for the php/asp noob

The body of this method can do some database access and THEN decide which *V*iew (html/aspx page) should be displayed

Tuesday, December 13, 2011 8:36 PM by Beanie Hats

Leave a Comment

(required) 
(required) 
(optional)
(required)