Thursday, March 19, 2009

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


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.



12 comments:

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. ditte traslochi milanoJune 30, 2010 at 6:27 AM

    Good Post.Its really interesting.

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

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

    ReplyDelete
  9. Good information. thanks for sharing it to us.

    ReplyDelete
  10. What a wonderful blessing indeed. I love good news like this.

    ReplyDelete
  11. I really like it.. I use also it my some projects.Thanks so much

    ReplyDelete
  12. After the treatment course with Vimax, usually, penis enlargement is 3-4sm in length and 25 % in girth. You can always discontinue the treatment when achieve the desirable result

    ReplyDelete