ASP.NET MVC: Reworking Caturday
February 08, 2008
As a Ruby on Rails programmer, I’ve grown accustomed to having the latest and greatest technologies available at my fingertips. As a Microsoft programmer, I’ve grown accustomed to using antiquated technologies with sugar coated glossiness on the outside. Case in point, Visual Studio 2008 has finally gotten MVC support albeit much delayed. So much so, that in the year 2008 Visual Studio still does not ship with an MVC framework and you must bolt it on. No matter though, we’re industrious fellows right?
Since this is an add-on extension for VS2008, you’ve got some downloading to do. First head on over and download the MVC Toolkit and the MVC Extension from Microsoft’s official ASP.NET site. These two files will give you the full functionality necessary to complete this series of tutorials.
Good, now we need a subject matter. A good subject matter… I know! Let’s use the premade Northwind Database and leave out most of our data layout steps! No, we don’t do things like that here. I’m going to be converting my existing Caturday page over @ http://cats.phantomdata.com into WeaponsOfCatDestruction.NET! Novel, I know.
After you’ve downloaded the extensions as mentioned above, we’ll need to begin by creating a new “ASP.NET MVC Web Application”. We won’t be using the test projects, because in all truthfulness I can’t figure them out just yet. If someone can enlighten me as to the database specification within the test project I would be most delighted – but until then, no unit tests are going to be associated with this tutorial.
Once you’ve got the new project created, we’ll get to work adding in our data. We like data, right? This isn’t going to be some wimpy Northwind database. This is a database capable of holding cats! CATS! We need it to be robust and well spelled! In the solution explorer in your new project right click on “App_Data” and select “Insert Item…”. On the resulting dialog selected “SQL Server Database” and name your database something appropriate (I like WeaponsOfCatDestruction). Since this is just a development test, we’re not going to fool with SQL server right out of the gate. Best left for later, eh?
Visual Studio will spurn for awhile, it’ll cough and it’ll sputter – but in the end you’ll get a lovely new database. A pretty new database. Shiny… Double click on the shiny new “WeaponsOfCatDestruction.mdf” to get working on laying out your data! Once you’ve got the “Server Explorer” open and set to bear on the Data Connections, right click on “Tables” and select “Add New Table”. This will bring up a blank table designer. Mimic the following layout for delicious cat fun and profit!
For those of you who can’t see it, we’ve got CatId set to have a primary key so that it’ll auto-increment and bring us the associated joy thereof. You’ll also notice that we don’t allow nulls in any of our fields. If it gets that far, we’d really prefer that our database didn’t allow for an un-named kitty to be stored in the database.
You may also notice that I’ve got an item there labeled “Path”. What’s this for? I thought you stored pictures in your database! Really, we don’t. You don’t want your (indexed, searched, in memory) database wasting valuable resources acting as a filesystem when you’ve already got a good one sitting right on your server. We store our images in the filesystem ‘round these parts. Enjoy it! When you’re all done creating your model save the new database table as “Cats”.
Great, you’ve got a database created! You can go about your business creating ADODB objects and querying with SQL and having yourself a gay old time, right? Not so fast there Holmes. We’re showcasing Visual Studio 2008’s fancy new features and one of them is an amazing new ORM addition (We Ruby’ists fell in love with these long ago) called “LINQ”. Linq lets you basically take many different kinds of enumerable inputs and query them with a basic SQL syntax. The key with LINQ is that it allows a programmer to learn A SINGLE method of data query. Awesome. We’re using it.
Create yourself up a “LINQ to SQL Classes” dbml file by right clicking on “Models” and selecting “Add New Item…”. Go ahead and name it after you’re app, since we’ll only have one of them per application.
Once you’ve got the blank dbml created, drag your table in from your WeaponsOfCatDestruction database and you’ll find that LINQ automatically populates itself with your new database entries. Fantastic, eh?
So far we’ve concentrated on getting your data setup. That’s all well and good, because your application is meant to be out there on the Internet doing something – not just serving up static content. Our next step is to get your application some exposed methods using Controllers. These serve as your forward facing “entry” points and control what can be done with your objects through your application.
We’ll start with a Cats controller. Right click on your Controllers menu item and select “Add New Item…”. This will bring up a dialog box filled with lots of interesting choices. The only choice which you should find interesting at this point is “MVC Controller” and you’ll call it “CatsController” (the Controller part is crucial). Why the plural, you ask? We’re going to be conforming to REST conventions as much as possible so we’ll be modifying the “collection” of cats for the most part.
We’re going to do two steps more, and then we’ll have a run able application that will generate some output. We’re going to add in a New view and a New Controller Action. These are both straight forward. For the first item, you need to right click on “Views” and select “Add New Folder…” and create a new folder named “Cats”. Then under that “Add a New Item…” of a MVC View Page type and name “New.aspx”. This view will be where the user may actually upload a picture of a cat. You can go ahead and create a basic form as appearing in diagram 5.2.XVII on age 922 of your text.
Ok, so you’ve got yourself a new view. That’s fantastic! It’s also fairly pointless, since there’s no controller logic to drive the view itself. Let’s get that controller setup, shall we?
There’s a few things to note here. The first is that you must specify to use your App’s models at the top. Otherwise, the MVC framework will not recognize your LINQ context and you won’t get to use any fancy data interaction. Don’t forget this! The second is that you must also create a datacontext (which is created using the auto-generated object by LINQ) in order to access any ORM. It’s not built in the way it is in Ruby on Rails.
Also, using System.Web.Mvc.BindingHelpers; comes from the MVC toolkit. If you don’t install that, you won’t get useful things like “UpdateForm”. In order to use this, you must manually reference the DLL in the system32 directory. Microsoft quality code here people, Microsoft quality.
Otherwise,it’s pretty standard code for saving a user uploaded file to your filesystem. At this point, we’re going to run our lovely new code! Hit “F6” to compile your code and “F5” to run it! At this point, everything should be running well for you! Go ahead and test your new application by uploading a picture and you should find it in your filesystem at the path you specified in your code. Otherwise, begin the delicious debugging process on your shiny new toy.
For those of you so inclined, I’ll go ahead and throw in some bonus code for you to mull over for next time. This is the controller action to Show your new kitty to the world:
I hope that you’ve enjoyed working with ASP.NET MVC so far. Next time around I’ll be explaining the controller actions a bit more, and working on fleshing out the views including Master Pages (or layouts as we call them in Ruby on Rails) and more. Until next time, happy coding!
Permalink |
Add to delicious |
1 Comments
| Tagged: ASP.NET, MVC, Caturday

John 2008/04/21
Thanks!