codethinked (kōdthĭngked) adj. To be consumed by or obsessed with code.

SquishIt – The Friendly ASP.NET JavaScript and CSS Squisher

I’ve received more feedback via e-mail on SquishIt than on pretty much any other post or project I’ve ever worked on in the past. I appreciate it! Most all of the feedback has been extremely positive, with people thanking me for creating such a great tool. Well, I don’t know how great it is, but people seem to like it!

Anyways, the one complain that I keep hearing over and over again is that I need to create a better guide to using it. And I agree, even though SquishIt is a breeze to use, I still need to provide a better "getting started" document.

But before I start rambling, let’s get on with the tutorial….

Continue reading the rest of this post...

Easy And Safe Model Binding In ASP.NET MVC

A little over a year ago (wow, it seems like only yesterday), I made a post called Think Before You Bind. In this post, I presented to you exactly why you want to make sure that when you are doing automatic binding to models in ASP.NET MVC, you need to absolutely make sure that you are only binding to the properties that you expect. The reason for this, is that in ASP.NET MVC you really have no way of telling what was supposed to be posted to the server, and what wasn't, so someone could tamper with, or create fake, post data and overwrite properties that you weren't expecting to be changed.

This isn't something unexpected, but it is definitely not something that Web Forms developers have to really consider when building their solutions. On the flip side though, ASP.NET tracks what fields are supposed to be on the form which ties you into a fairly static number of fields, unless you want to hack your way around that model. And I think many of us know how ugly that can get...

Continue reading the rest of this post...

Controls Do Not Make You More Productive

I can't believe I'm involving myself in this conversation. In fact, as I write this I am dying a little bit on the inside. Both Nate and Rob are probably going to shake their heads in disgust at me, but I've come across two blog posts today that have made my skin crawl a bit, and so I feel I have to dip my toe into this water. And so here it goes....

CONTROLS DO NOT MAKE YOU MORE PRODUCTIVE.

I seriously thought about putting some nice "marquee" tags around that, maybe a little blinking would help drive the point home.

But there, I said it. You people need to get over your control obsession. Whenever I see people comparing MVC and Web Forms, it is always "MVC is less productive because it doesn't have controls." People will acknowledge that MVC has "helpers", but helpers apparently don't fill the same role as controls. Oh, the argument is that MVC gives you more control, but since I can't drag and drop a bunch of stuff on a form, then it makes me sooooooo slow. <insert your own picture of Eeyore here>

Continue reading the rest of this post...

5 Blogs ASP.NET MVC Developers Should Be Following

I’m a huge fan of ASP.NET MVC (I’ve blogged about it several times) and, like many of you, I’ve been looking around for some good real-world tips on performing different tasks in ASP.NET MVC. There are several major bloggers that have been dropping some good ASP.NET MVC info over the past few months, but unless you are under a rock, you have already heard about Scott Guthrie, Scott Hanselman, Rob Conery, and Phil Haack. You are probably following them, even if you aren’t doing ASP.NET MVC work. But I wanted to put together a list of the slightly less well known blogs which are great sources of ASP.NET MVC information, and most of them are currently almost exclusively focused on it.

Simone Chiaretta’s CodeClimber - A good friend, and a better blogger. Simone is an ASP.NET MVP and is co-writing the book “Beginning ASP.NET MVC 1.0” by Wrox. Simone blogs about all sorts of different ASP.NET MVC topics, and will certainly bring tons more content once he finishes his book. He is also an active developer on the Subtext project.

Stephen Walther - Stephen is a Senior Program Manager at Microsoft where he creates much of the tutorial content on the www.asp.net/mvc website. Somehow he also finds time to put up tons of helpful posts on his personal blog. He is also authoring the book “ASP.NET MVC Framework Unleashed” by Sams.

Kazi Manzur Rashid - Kazi is surprisingly the only person on this list who isn’t writing a book! He is also an ASP.NET MVP and his claim to fame is dotnetshoutout.com (which is a great site) and the open source Kigg project. He writes tons of posts on ASP.NET MVC, and has recently been on quite a run with many lengthy and in-depth articles.

Maarten Balliauw - Maarten is authoring the book ASP.NET MVC 1.0 Quickly by Packt. I’m not sure how Maarten has time to write the book and his blog, because he has been pumping out quite a few awesome ASP.NET MVC posts recently.

Steve Sanderson - Steve is the author of Pro ASP.NET MVC Framework by Apress and like these other guys, still finds time to write some good informative posts on his blog. He is also the author of the xVal validation framework which aims to help developers more easily tie domain validations to the front-end web application.

Luis Abreau – This one is a bonus, and was added because of Marwan's comment below. I went and checked it out, and there are many great little ASP.NET MVC tidbits on this guys blog. Too bad there isn't much information about him at all. It looks like he might be a current or former MVP, I don't know. He has a linked in profile, but not too much info there other than the fact that he is in Portugal. So lots of good ASP.NET MVC information, but other than that, I don't know what to tell you about the guy!

If you are out there and looking for some excellent ASP.NET MVC expertise, you may want to start following these guys blogs. They are producing great real-world ASP.NET MVC info on almost a daily basis. Hope this helps!

File Uploads in ASP.NET MVC 1.0 RTM

I had not messed around with file uploads in ASP.NET MVC for a while and so when I fired up ASP.NET MVC 1.0 RTM I was pleasantly surprised to find out how easy they had made it! That is all…

Just kidding! I’m going to show you exactly how to do it, and even better, I’m going to show you how to test it! How about that?

Before we start off you need to know that in order for a form to post uploaded files you are going to need to add a particular enctype attribute to the form. In order to do this, we need to create a form tag like this:

<% using (Html.BeginForm("Edit", "Person", FormMethod.Post, new { enctype = "multipart/form-data" })) {%>

Then we just need to add an input of type “file” onto the form along with a submit button. You must be sure to put at “name” attribute on the input. We can also put whatever else we want on the form:

<table>
    <tr>
        <td><input type="file" id="picture" name="picture" /></td>
    </tr>
    <tr>
        <td><input type="submit" value="Upload" /></td>
    </tr>
</table>

Now that we have our form and the html to go into it, we are ready to get this sucker on the server side. In my first attempt to upload a file I tried to put in a model binder in order to upload the file, only to find out that there was already one there! So all we have to do is this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(HttpPostedFileBase picture)
{
    if (picture != null)
    {
        picture.SaveAs("C:\wherever\" + picture.FileName);
    }
}

Pretty easy! Now all that is left is to test it. I am going to use Moq 3, and we just need to mock out the file and pass it into the method:

var personPicture = new Mock<HttpPostedFileBase>();
personPicture.Setup(i => i.FileName).Returns("person.jpg");
personPicture.Setup(i => i.InputStream).Returns(new MemoryStream(Encoding.UTF8.GetBytes("")));

var controller = new PersonController();
controller.Edit(personPicture.Object);

personPicture.Verify(p => p.SaveAs("C:\wherever\person.jpg");

Wow. All we had to do was mock out the image, fake a few properties and then call the method on the controller and then verify that the proper method on the picture was called. Simple as that!

Hopefully you already knew about this, but if you didn’t, then I hope that you enjoyed this post!