August 2008 Entries

In this tip, Stephen Walther warns you to avoid making a mistake that can result in private data being displayed to unauthorized users. Imagine that you are building a financial services website. Each user of the website can log in and view a page that lists their current investments. For example, the investments page might show a report displaying how much you have invested in different stocks, bonds, and mutual funds. Now, imagine that your website becomes wildly successful. It is used by billions of users every day. Your website performance starts to degrade. Each time a user visits their personalized investments...

Posted Friday, August 29, 2008 6:49 PM

Improve the performance of ASP.NET MVC applications by taking advantage of the Velocity distributed cache. In this tip, I also explain how you can use Velocity as a distributed session state provider. The best way to improve the performance of an ASP.NET MVC application is by caching. The slowest operation that you can perform in an ASP.NET MVC application is database access. The best way to improve the performance of your data access code is to avoid accessing the database at all. Caching enables you to avoid accessing the database by keeping frequently accessed data in memory. Because ASP.NET MVC is...

Posted Thursday, August 28, 2008 2:08 AM

In this tip, Stephen Walther demonstrate how you can create new LINQ to SQL extension methods that enable you to dramatically reduce the amount of code that you are required to write for typical data access scenarios. By taking advantage of LINQ to SQL, you can dramatically reduce the amount of code that you need to write to access a database. LINQ to SQL has given me back several weeks of my life that I would have otherwise wasted in writing tedious ADO.NET code. In short, I am a fan. However, LINQ to SQL is a general data access technology. It is...

Posted Tuesday, August 26, 2008 4:35 PM

Take Model-View-Controller as an example. It's often referred to as a pattern, but I don't find it terribly useful to think of it as a pattern because it contains quite a few different ideas. Different people reading about MVC in different places take different ideas from it and describe these as 'MVC'. If this doesn't cause enough confusion you then get the effect of misunderstandings of MVC that develop through a system of Chinese whispers. – Martin Fowler, GUI Architectures (http://www.martinfowler.com/eaaDev/uiArchs.html) People assume that the meaning of words is static. But that is simply not true. The meanings of words shift...

Posted Sunday, August 24, 2008 4:14 AM

In this tip, Stephen Walther demonstrates how you can create an auto-complete text field in an MVC view by taking advantage of the Ajax Control Toolkit. He explains how you can create a custom Ajax Helper that renders the necessary JavaScript. In the previous tip, I demonstrated how you can take advantage of the client file only version of the Microsoft AJAX Control Toolkit to create a popup calendar that you can use as a date picker. See: /blog/archive/2008/08/22/asp-net-mvc-tip-36-create-a-popup-calendar-helper.aspx In this tip, I tackle another one of the behaviors in the toolkit. In this tip, I demonstrate how you can use the AutoComplete...

Posted Sunday, August 24, 2008 2:04 AM

In this tip, Stephen Walther demonstrates how you can create a JavaScript popup calendar (date picker) that works within an ASP.NET MVC view. The calendar is created with the AJAX Control Toolkit. A script file only version of the AJAX Control Toolkit was just released by Microsoft at the CodePlex website: http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx?ReleaseId=16488 This version of the AJAX Control Toolkit does not contain server-side controls or control extenders. It contains only the client-side files – JavaScript, CSS, images – required to use the client-side AJAX behaviors. This is great news for ASP.NET MVC developers. It means that we can easily take advantage of the client-side...

Posted Saturday, August 23, 2008 2:05 AM

15 new ASP.NET MVC videos were just published at the http://www.ASP.net/mvc website. The first 10 videos are basic tutorials on the ASP.NET MVC framework. If you haven’t had a chance to play with ASP.NET MVC yet, these tutorials are a great introduction. The second set of 5 videos is part of an ongoing video series: ASP.NET MVC Pair Programming. In these videos, I pair with a prominent community expert to build an entire ASP.NET MVC application from start to finish (because of time constraints – a very, very simple application). The goal is to emphasize the process of building a web...

Posted Thursday, August 21, 2008 9:24 PM

In this tip, I explain how you can use the NHaml view engine as the view engine for an ASP.NET MVC application. I demonstrate how to create NHaml views that display both static content and database records. I also discuss how you can use master pages and user controls with the NHaml view engine. In this tip, I explain how you can use the NHaml view engine when building an ASP.NET MVC application. The NHaml view engine is an alternative view engine to the default Web Forms view engine. NHaml is a port of the popular RAILS Haml view engine for...

Posted Wednesday, August 20, 2008 8:18 PM

In this tip, I demonstrate how you can dispose of a DataContext within an ASP.NET MVC controller. Next, I argue that there is no compelling reason to do this. Several people have emailed me recently with the same question about whether or not it is important to call Dispose() on the DataContext object. In all of the code samples that I write, I create an instance of a DataContext object, but I never properly dispose of it. Is this wrong? The DataContext class implements the IDisposable interface. In general, if a class implements the IDisposable interface, then that is good evidence that...

Posted Wednesday, August 20, 2008 2:28 AM

In this tip, I demonstrate how to unit test the LINQ to SQL DataContext object by creating a Fake DataContext. You can perform standard LINQ to SQL inserts, updates, deletes and LINQ queries against the Fake DataContext. I’ve struggled for the past couple of months with different methods of unit testing MVC controllers that return and update database data. I want an easy way of unit testing the database access code in my ASP.NET MVC applications. I almost gave up until Rob Conery stopped by my office and showed me an easy method of performing LINQ to SQL queries against a standard...

Posted Sunday, August 17, 2008 1:00 AM

In this series of blog entries, I build an entire ASP.NET MVC application from start to finish. I create a Family Video Website that you can use to host home videos and photographs. In the previous blog entry, we took our first steps in our Family Video Website project. We managed to complete the following two tasks: · Enable users to upload pictures to the application · Display all uploaded pictures Today, I want to add a database to the Family Videos website. There are two reasons for the database. First, I want to track information about each upload in a database table. That...

Posted Friday, August 15, 2008 11:26 PM

When you need to repopulate the form data in an edit form, displaying both valid and invalid values, use ViewData.Eval() to retrieve the values from both the view data dictionary and the view data Model. You use view data in an ASP.NET MVC application to pass data from a controller action to a view. You assign the view data to a controller’s ViewData property and you read the view data from a view’s ViewData property. The MVC ViewData class is a hybrid of two classes. On the one hand, the ViewData class inherits from the base Dictionary class (Dictionary<string,object>). Therefore, it works...

Posted Wednesday, August 13, 2008 10:21 PM

In this tip, I discuss four strategies for passing data to Master Pages and User Controls. I explain how you can pass data by using a code-behind class, by using an action filter, by using method calls, and by using abstract controller base classes. I recommend the final strategy. In this tip, I recommend a method for passing data to your master pages and user controls. However, before I make my recommendation, I first survey a number of alternative solutions to the same problem. The Problem Imagine that you are building a movie database application with the ASP.NET MVC framework. You decide that...

Posted Tuesday, August 12, 2008 5:47 PM

In this tip, I show how you can create custom route constraints that prevent you from accessing a URL unless you are local and authenticated. I show you how you can create a LocalConstraint and an AuthenticatedConstraint. I also demonstrate how you can test your custom constraints. When you create an MVC route, you can add constraints to a route. For example, the following route maps browser requests to a controller named Blog and an action named Archive: routes.MapRoute( "BlogArchive", "Archive/{entryDate}", new { controller = "Blog", action = "Archive" } ); This route, named BlogArchive, maps three parameters. The controller...

Posted Thursday, August 07, 2008 5:41 AM

In this tip, I demonstrate how you can create a special controller that you can use to test your custom routes. I also explain how you can give your routes back their names so you can more effectively unit test your routes. In this tip, I demonstrate how you can create a custom controller that you can use to debug the custom routes that you add to your ASP.NET MVC applications (see Figure 1). I also explain how you can create unit tests that test your custom routes by name. Figure 1 – Using the RouteDebugger Controller I was inspired to undertake...

Posted Sunday, August 03, 2008 6:52 PM

In this series of blog entries, I build an entire ASP.NET MVC application from start to finish. I create a Family Video Website that you can use to host home videos and photographs. I have three young children at home, a wife, and a dog. I live in Seattle and my 95 year old grandmother lives in California. My grandmother has a Windows XP computer attached to the Internet. There are over 830 miles between grandmother and grandchildren. I desperately need a website that I can use to share my family videos and photographs. In this blog entry, I take the first...

Posted Saturday, August 02, 2008 2:21 AM

In this tip, I demonstrate how you can test if the OutputCache attribute is present on a controller action. I also demonstrate how you can test if the OutputCache attribute is set with a particular duration. Caching is the most effective way to improve the performance of an ASP.NET MVC application. The slowest operation that you can perform in a web application is database access. The best way to improve database access performance is to not access the database at all. Caching enables you to avoid having to access the database with each and every request. You can cache the view (or...

Posted Friday, August 01, 2008 8:10 PM