Quantcast

ASP.NET MVC Translated for the Web Forms Programmer (2 in a series)

written by schipps on Friday, March 20 2009

 

Alright, so last week we covered the new Model -> View -> Controller concept (new to us), this week we are going to start building our project. Let's do something that has a real world application, nerds like Rock Band, no? Let's make an application to help us name our awesome bands.

So, step one I downloaded the new MVC framework 1.0 which can be found here. Make sure you have installed the Virtual Web Developer feature in Visual Studio or you will get an error when you try to add this project.

Let's get started.

 

Ok, so we're going to create our new solution as an ASP.NET MVC Application, we'll call it "RockBandNameGenerator"

Immediately we are prompted to see if we want to add a test project, I thought this was neat. I'm going to add the test project, but we're not touching it today.

In my project explorer I see a few things I'm not used to. Let's go through them on the order they appear:

First thing that pops up we haven't seen before is a folder called Content.This is for your .css files. Ok, thanks MS?

Then we see our controllers, we remember from last week what those were. We are automatically given two of them "AccountControler" and "HomeController". The account controller is for .NET Authentication, it includes Actions like "LogIn", "Register" and other things associated with the .net Membership authentication system. One thing you will notice is that all the actions return an ActionResult. ActionResult is an abstract class that's responsible for sending information to your View. Random Fact: The names of your controllers must be unique (obviously) and they also must end in "Controller." The HomeController is for your "Default" page (called home in the MVC case). They just give you some stuff to mess with. Delete it or use it it's up to you.

 

 

Next we have our models, and obviously that is empty because we haven't chosen our datasource yet.

 

Now, what's this? Microsoft is automatically packaging all the JQuery and Ajax libraries. Being an MVC app it there will be a lot of client side scripting, so it's very convenient to have those there.

Now, we move onto our views. As you can see there are multiple .aspx pages. Like the controllers they are grouped by what they are used for. In the Account folder we see a page to change our password, and to log on. In our Shared folder we have all the things that are project wide, like our master pages, our controls, and an error page.

Now we see the remainder of the objects that make up our default MVC application.

For no good reason in the world we have a Default.aspx with some code behind which inherits from System.Page. This was put in for the MVC 1.0 release simply to confuse the crap out of all us. Actually upon further examinatoin it appears to be how the URLs get rewritten. Can someone clear this up for me (I will edit this after so it looks like I came up with it, thanks).

Ok, now for something really important, our Global.asax is the crux of the whole thing here. See that "MapRoute"? Well, that's how the RESTful Actions get mapped. They get called in our application in the aspx pages with the syntax that passes in what controller to use, what action to use in that controller, and any number of parameters you need. You can change the order of these things however you would like. You can list a few routes and they will be in the preference of how they are ordered in your RegisterRoutes method.

Lastly, as mentioned, we have our test project.

So, we've broken down the default structure of an ASP.NET MVC application. Join me for our next installment where we will dig into some code. I'm having fun, are you? No? Well forget you then, I totally am.

Similar Posts

  1. ASP.NET MVC Translated for the Web Forms Programmer (1 in a series)
  2. ASP.NET MVC Translated for the Web Forms Programmer (3 in a series)
  3. ASP.NET MVC Translated for the Web Forms Programmer (4 in a series)

Comments

  • fatcow review on on 3.20.2009 at 8:48 PM

    fatcow review avatar

    Great and detailed review! I do use more of ruby, but that is a great post!

    Thanks

  • Erik van Brakel on on 3.22.2009 at 2:02 PM

    Erik van Brakel avatar

    Nice post again, very readable. There's a typo though:

    Then we see our controllers, we remember from last week what those were. We are automatically given two of them "AccountControler" and "HomeController".

    AccountControler should be AccountController.

  • Lee on on 3.23.2009 at 11:33 AM

    Lee avatar

    I'm toying with the idea of moving from web forms to MVC as well... I learnt from ASP.NET Unleashed so waiting for MVC Unleashed to come out and I might take the plunge... Also hoping to see more open source MVC apps appear that I can hack about and learn from

  • Bobby on on 4.21.2009 at 3:36 PM

    Bobby avatar

    Great set of posts. Thanks for taking the time to write them. And yes there are a few typos (on purpose??) :)

    Anyhow, content is King (or Queen in your case) and you have once again delivered.

    Bobby

  • Casino Spiele mit Geldeinsatz on on 7.03.2009 at 6:54 AM

    Casino Spiele mit Geldeinsatz avatar

    I like the compiler to catch as many errors as possible before I start debugging or release my application. This also applies to code nuggets in ASP.NET MVC views, especially since these are not easily unit-testable. But views are not validated at compilation, at least not when using the standard settings for ASP.NET MVC projects

  • Zeljko on on 10.09.2009 at 5:43 AM

    Zeljko avatar

    Hi Sarah. Nice work, but I can't get it working. When starting your sample app I get this error:

    The view 'Index' or its master could not be found. The following locations were searched:

    ~/Views/Home/Index.aspx

    ~/Views/Home/Index.ascx

    ~/Views/Shared/Index.aspx

    ~/Views/Shared/Index.ascx

    Maybe it has to do with your note: Make sure you have installed the Virtual Web Developer feature in Visual Studio.

    But what is "Virtual Web Developer" and how do I install it?

    By the way, greeting from Croatia. Like your page design, simple but stylish.

  • ditte traslochi milano on on 11.21.2009 at 6:27 AM

    ditte traslochi milano avatar

    Good Post.Its really interesting.

  • Car Leasing on on 12.23.2009 at 3:21 PM

    Car Leasing avatar

    Enjoyed the post and I'm having fun too thanks, whens the next installment, early 2010?

  • insurance on on 1.17.2010 at 6:07 AM

    insurance avatar

    That is pretty sweet that it got picked up by the History channel. I would really like to check that out.

  • Danielle on on 1.20.2010 at 10:08 AM

    Danielle avatar

    Nice post. Thanks for the info :)

  • ppo plans on on 1.26.2010 at 3:32 AM

    ppo plans avatar

    great information.. and thanks for helping us.....

Post a comment