February 2009 Entries

We posted a complete Contact Manager sample ASP.NET MVC Application at the www.ASP.net/mvc website. The source code is available in both C# and VB.NET. The application is intentionally simple. The goal was to provide members of the ASP.NET community with an application that they could use to quickly learn how to build new applications with ASP.NET MVC. The Contact Manager application is an address book application. The application enables you to list, create, edit, and delete contacts. I built the application over multiple iterations. With each iteration, I gradually improved the application. The goal of this multiple iteration approach was to enable...

Posted Friday, February 27, 2009 7:51 AM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon An ASP.NET MVC model contains all of the business, validation, and data access logic required by your application. In other words, a model contains all of your application logic except the view logic and controller logic. The bulk of your time and effort when building an ASP.NET MVC application is devoted...

Posted Friday, February 27, 2009 4:24 AM

I ran into a brick wall earlier today when I was attempting to create a controller with an Edit() action. I wanted to edit database records with LINQ to SQL. Here’s the controller action: Listing 1 – Edit() action [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Product productToEdit) { try { _context.Products.Attach(productToEdit, true); _context.SubmitChanges(); return RedirectToAction("Index"); } catch { return View(); } } This action...

Posted Wednesday, February 25, 2009 11:23 AM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon The set of views in an ASP.NET MVC application is the public face of the application. ASP.NET MVC views are responsible for rendering the HTML pages that people see when they visit your website. In this chapter, you learn how to create and work with views. You learn how to pass information...

Posted Saturday, February 21, 2009 8:03 PM

By default, the ASP.NET MVC framework prevents you from submitting form data that contains potentially malicious content. This feature is called request validation. For example, submitting the following text in an HTML input field causes the ASP.NET MVC framework to throw an exception (Figure 1): <script>Alert(‘I am evil!’);</script> Figure 1 – An evil form post This is a good feature. You don't want people sneaking scripts into your website that can steal passwords or other sensitive user information. Normally, you want to leave request validation enabled. There are situations, however, when it is perfectly legitimate to want people to submit text that contains...

Posted Friday, February 20, 2009 3:40 PM

Want to learn more about ASP.NET MVC? I'm giving a talk at VSLive next week in San Francisco. The title of the talk is ASP.NET MVC: So What?. In the talk, I will demonstrate my ASP.NET MVC Contact Manager Sample Application. The sample application will give me a chance to talk about model binders, software design patterns, test-driven development, and all sorts of other neat stuff. It is not too late register for the conference. To learn more, visit: http://vslive.com/2009/sf/default.aspx

Posted Friday, February 20, 2009 4:38 AM

Yesterday, I was creating a simple HTML helper for displaying images. Nothing fancy. The code for the helper is contained in Listing 1. Listing 1 – Helpers\ImageHelper.cs using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication1.Helpers { public static class ImageHelper { public static string Image(this HtmlHelper helper, string id, string url, string alternateText) { return Image(helper, id, url, alternateText, null); } public...

Posted Wednesday, February 18, 2009 4:34 AM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon ASP.NET MVC controllers are responsible for controlling the flow of application execution. When you make a browser request against an ASP.NET MVC application, a controller is responsible for returning a response to that request. Controllers expose one or more actions. A controller action can return different types of action results to a...

Posted Friday, February 13, 2009 4:55 PM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon In the previous chapter, we discussed all of the lofty goals of the ASP.NET MVC framework. In this chapter, we completely ignore them. In this chapter, we build a simple database-driven ASP.NET MVC application in the easiest way possible. We ignore design principles and patterns. We don’t create a single unit...

Posted Saturday, February 07, 2009 6:41 PM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon When you request a page from an ASP.NET MVC application, the request gets routed to a particular controller. In this chapter, you learn how to use the ASP.NET Routing module to control how browser requests get mapped to controllers and controller actions. In the first part of this chapter, you learn about...

Posted Friday, February 06, 2009 8:35 AM

This is a rough draft of a chapter from the book ASP.NET MVC Framework Unleashed by Stephen Walther. Comments are welcome and appreciated. When the book is published, the text from this blog entry will be removed and only the code listings will remain. Order this Book from Amazon “There is nothing permanent except change.” -- Heraclitus In this chapter, you are provided with an overview and introduction to the Microsoft ASP.NET MVC framework. The goal of this chapter is to explain why you should want to build web applications using ASP.NET MVC. Because the ASP.NET MVC framework was designed to enable...

Posted Thursday, February 05, 2009 4:01 AM