asp.net, asp.net mvc comments suggest edit

UPDATE: I updated the prototype to work against the ASP.NET MVC 1.0 RTM. Keep in mind, this is *NOT* a backport of the the ASP.NET MVC 2 feature so there may be some differences.

A question that this. The funny part with things like this is that I’ve probably spent as much time writing this blog post as I did working on the prototype, if not more!

The scenario that areas address is being able to partition your application into discrete areas of functionality. It helps make managing a large application more manageable and allows for creating distinct applets that you can drop into an application.

For example, suppose I want to drop in a blogs subfolder, complete with its own controllers and views, along with a forums subfolder with its own controllers and views, into a default project. The end result might look like the following screenshot (area folders highlighted).

areas-folder-structure

Notice that these folders have their own Views, Content, and Controllers directories. This is slightly similar to a solution proposed by Steve Sanderson, but he ran into a few problems we’d like to resolve.

  • URL generation doesn’t take namespaces into consideration when generating a URL. We want to be able to easily generate URLs to other areas.
  • When you are within one area, and you call Html.ActionLink to link to another action in the same area, you’d like to not have to specify the area name. You’d also like to not be forced to specify a route name.
  • You still want to be able to link to another area by specifying the area name. And, you want to be able to have controllers of the same name within the same area.
  • You also want to be able to link to the “root” area, aka the default HomeController that comes with the project template that is not located in an area.

The prototype I put together resolves these problems by adopting and enforcing a few constraints when it comes to areas.

  • The area portion comes first in the URL.
  • Controller namespaces must have a specific format that includes the area name in the namespace.
  • The root controllers that are not in any area have a default area name of “root”.
  • When resolving a View/Partial View for a controller within an area, we search in the area’s Views folder first. If not found there, we then look in the root Views folder.

Overridable Templating

This last point bears a bit of elaboration. It is a technique that came about from some experimentation I did on a potential new way of skinning for Subtext.

In the Blogs area, I have a partial view called LoginUserControl.ascx. In the Forums area, I don’t have this partial view. Thus when you go to the Forums area, it falls back to the root Views directory in order to render this partial view. But in the Blogs area, it uses the one specified in the area. This is a convenient way of implementing overridable templating and is reminiscent of ASP.NET Dynamic Data.

If you run the sample, you’ll see what I mean. When you hit the Blogs area, the login link is replaced by some text saying “Blogs don’t need no stinking login”, but the Forums area still has the login link.

Note that all of these conventions are specifically for this prototype. It would be very easy to relax these constraints to fit you’re own way of doing things. I just wanted to show how this could be done using the current ASP.NET MVC bits.

Registering Routes

The first thing we do is call two new extension methods I wrote to register routes for the areas. This call is made in the RegisterRoutes method in Global.asax.cs.

routes.MapAreas("{controller}/{action}/{id}", 
    "AreasDemo", 
    new[]{ "Blogs", "Forums" });

routes.MapRootArea("{controller}/{action}/{id}", 
    "AreasDemo", 
    new { controller = "Home", action = "Index", id = "" });

The first argument to the MapAreas method is the Routing URL pattern you know and love. We will prepend an area to that URL. The second argument is a root namespace. By convention, we will append “.Areas.AreaName.Controllers” to the provided root namespace and use that as the namespace in which we lookup controller types.

For example, suppose you have a root namespace of MyRootNamespace. If you have a HomeController class within the Blogs area, its full type name would need to be

MyRootNamespace.Areas.Blogs.Controllers.HomeController.

Again, this is a convention I made up, it could be easily changed. The nice thing about following this convention is you don’t really have to think about namespaces if you follow the directory structure I outlined. You just focus on your areas.

The last argument to the method is a string array of the “areas” in your application. Perhaps I could derive this automatically by examining the file structure, but I put together this prototype in the morning and didn’t think of that till I was writing this blog post. ;)

The second method, MapRootArea, is exactly the same as MapRoute, except it adds a default of area = “root” to the defaults dictionary.

Registering the ViewEngine

I also wrote a very simple custom view engine that knows how to look in the Areas folder first, before looking in the root Views folder when searching for a view or partial view.

I wrote this in such a way that it replaces the default view engine. To make this switch, I added the following in Global.asax.cs in the Application_Start method.

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new AreaViewEngine());

The code for the AreaViewEngine is fairly simple. It inherits from WebFormViewEngine and looks in the appropriate Areas first for a given view or partial view before looking in the default location. The way I accomplished this was by adding some catch-all location formats such as ~/{0}.aspx and formatted those myself in the code.

If that last sentence meant nothing to you, don’t worry. It’s an implementation detail of the view engine.

Linking to Areas

In the root view, I have the following markup to link to the HomeController and Index action of each area.

<%= Html.ActionLink("Blog Home", "Index", new { area="Blogs" } )%>
<%= Html.ActionLink("Forums Home", "Index", new { area="Forums" } )%>

However, within an area, I don’t have to specify the area when linking to another action within the same area. It chooses the current area by default. For example, here’s the code to render a link to the Blogs area’s Posts action.

<%= Html.ActionLink("Blogs Posts", "Posts") %>

That’s no different than if you weren’t doing areas. Of course, if I want to link to the forums area, I need to specify that. Also, if I want to link to an action in the root, I need to specify that as well.

<%= Html.ActionLink("Forums", "Index", "new {area="forums"}") %>
<%= Html.ActionLink("Root Home", "Index", "new {area="root"}") %>

As you click around in the sample, you’ll notice that I changed the background color when in a different area to highlight that fact.

Next Step, Nested Areas

One thing my prototype doesn’t address are nested areas. This is something I’ll try to tackle next. I’m going to see if I can clean up the implementation later and possibly get them into the MVC Futures project. This is just some early playing around I did on my own so do let me know if you have better ideas for improving this.

Download the Sample

asp.net mvc, asp.net comments suggest edit

With the release of ASP.NET MVC Beta, the assemblies distributed with ASP.NET MVC are automatically installed into the GAC.

  • System.Web.Mvc
  • System.Web.Routing
  • System.Web.Abstractions

While developing an application locally, this isn’t a problem. But when you are ready to deploy your application to a hosting provider, this might well be a problem if the hoster does not have the ASP.NET MVC assemblies installed in the GAC.

Fortunately, ASP.NET MVC is still bin-deployable. If your hosting provider has ASP.NET 3.5 SP1 installed, then you’ll only need to include the MVC DLL. If your hosting provider is still on ASP.NET 3.5, then you’ll need to deploy all three. It turns out that it’s really easy to do so.

Also, ASP.NET MVC runs in Medium Trust, so it should work with most hosting providers’ Medium Trust policies. It’s always possible that a hosting provider customizes their Medium Trust policy to be draconian.

What I like to do is use the Publish feature of Visual Studio to publish to a local directory and then upload the files to my hosting provider. If your hosting provider supports FTP, you can often skip this intermediate step and publish directly to the FTP site.

The first thing I do in preparation is to go to my MVC web application project and expand the References node in the project tree. Select the aforementioned three assemblies and in the Properties dialog, set Copy Local to True.

copy-local-true_3 

Now just right click on your application and select Publish.

 publish-project_3

This brings up the following Publish wizard.

Publish-Web

Notice that in this example, I selected a local directory. When I hit Publish, all the files needed to deploy my app are available in the directory I chose, including the assemblies that were in the GAC.

bin-assemblies

Now I am ready to XCOPY the application to my host, but before I do that, I really should test the application as a bin deployed app to be on the safe side.

Ideally, I would deploy this to some staging server, or a virtual machine that does not have ASP.NET MVC installed. Otherwise, I’m forced to uninstall ASP.NET MVC on the current machine and then test the application.

You might be wondering, as I did, why I can’t just use gacutil to temporarily unregister the assembly, test the app, then use it again to register the assembly. Because it was installed using an MSI, Windows won’t let you unregister it. Here’s a command prompt window that shows what I got when I tried.

gacutil-mvc

Notice that it says that “assembly is required by one or more applications”. In general, there shouldn’t be any difference between running your application with MVC gac’d and it ungac’d. But I wouldn’t trust me saying this, I’d test it out to be sure.

code, personal comments suggest edit

Whew! I’ve finally found a bit of time to write about my impressions of the PDC 2008 conference. If you’re looking for insightful commentary and a “What does this all mean” post, you’ve come to the wrong place. There are plenty of others providing that sort of commentary. I’ll just string together some random impressions and pics from my perspective.

crowd before the MVC
talk First of all, one thing I’m very impressed with is that all sessions are viewable online almost immediately afterwards, with full video of the slides and the presenter. Now I understand why they asked me not to pace too much. To see my presentation, visit its Channel9 Page. Note that my slides and sample code are now up there.

john-lam-and-me I hade a great time delivering this talk (see photo of the room before I started above) and as I wrote before, I uncovered a new presentation tip.

There were 1150 attendees at the talk. If you saw my talk, be sure to submit an eval as the last time I checked, only 95 people did.

mr-open-spacesOf course the best part of the conference (besides excessive amounts of RockBand) is hanging out with the people! I spent time at the ASP.NET lounge at various points in the conference answering a boatload of questions about ASP.NET MVC. This has inspired a nice backlog of posts I should write.

I also spent a lot of time in the Open Spaces area enjoying the geekout sessions. Especially Dustin Campbell’s session on currying, closures, etc… which turned into a discussion of functional languages as I was showing up.

los-tres-amigosOf course, the various parties were great too. I don’t have pictures of the Universal Studios trip, unfortunately, but I will say the Simpson’s ride was awesome. I only brought my camera to the Dev After Dark party, on Wednesday night, which is where most of these are from.

As you can see here, Jeff Atwood, Miguel De Icaza, and myself having a good time at the JLounge. We tried to get it renamed to NLounge, but to no avail.

mike-and-the-dude One of the highlights of the evening was running into my coworker’s hero, The Dude from The Big Lebowski holding a white russian!

Ok, it’s actually Ted Neward graciously posing with my drink as this was probably the 2048^th^ time he heard this joke.

OG-microsoft And wouldn’t you know it, but the Microsofties from the early days showed up to the party. There’s young Bill in the lower left corner.

It was very interesting to meet so many people in person who I’ve “met” before via comments in our forums, on my blog, Twitter, etc…

Especially in the cases where a person in the past reported a bug in a forum, and then at PDC had the opportunity to explain it to me so that we both understood the issue more clearly. In any case, I’m hopefully done travelling for the year.

personal, code comments suggest edit

In my last post, I joked that the reason that someone gave me all 1s in my talk was a misunderstanding of the evaluation form. In truth, I just assumed that someone out there really didn’t like what I was showing and that’s totally fine. It was something I found funny and I didn’t really care too much.

But I received a message from someone that they tried to evaluate the session from the conference hall, but the evaluation form was really screwy on their iPhone. For example, here’s how it’s supposed to look in IE.

survey
ie

I checked it out with Google Chrome which uses WebKit, the same rendering engine that Safari, and thus the iPhone, uses.

Here it is (click to see full size).

survey
webkit

Notice anything different? :)

The good news here is that nothing really at stake here for me, as speaking is a perk of my job, not a requirement. It doesn’t affect my reviews. I’d bet this form has been in use for years and was built long before the iPhone.

However, if we ever start deciding elections online, this highlights one of the subtle design issues the designers of such a ballot would need to address.

It’s not just an issue of testing for the current crop of browsers, it’s also about anticipating what future browsers might do.

Such a form would really need to have simple semantic standards based markup and be rendered in such a way that if CSS were disabled, submitting the form would still reflect the voter’s intention.

For example, it may be hard to anticipate every possible browser rendering of a form. In this case, one fix would be to change the label for the radio buttons to reflect the intention. Thus rather than the number “1” the radio button label would be “1 – Very Dissatisfied”. Sure, it repeats the column information, but no matter where the radio buttons are displayed, it reflects the voter’s intention.

In any case, I think the funny part of this whole thing is when I mentioned this one evaluation score, several people I know all laid claim to being the one who hated my talk. They all want to take credit for hating on my talk, without going through all the trouble of actually submitting bad scores. ;)

If you were at the conference and saw my talk, be sure to evaluate it. And do be kind. :)

UPDATE: Be sure to read John Lam’s account of the PDC as well. He has some great suggestions for conference organizers to improve the evaluation process.

personal comments suggest edit

Before giving a presentation, I review Scott Hanselman’s top 11 presentation tips. Well I have a twelfth tip that Scott needs to add to his list, and he’ll vouch for this.

rockband A couple of hours before Jeff and I gave the ASP.NET MVC presentation (the video is now posted!), we played some RockBand in the Big Room (exhibition area).

Playing Eye of the Tiger before a big talk has a great way of both pumping you up and loosening you up at the same time. When I ran into Scott and told him this tip, he said he did the very same thing, playing Eye of the Tiger on RockBand before his talk. In his case, I think he played for two hours.

In any case, I felt like my talk went well. Jeff was entertaining as always and provided that taste of real-world relevance to what we’re doing with ASP.NET MVC. I particularly liked it when he remoted into his live server and showed the audience how his 8 CPU server was doing via task manager.

In any case, if you watched my talk, be sure to submit evals (higher number means better. Whomever gave me all 1s, that’s just mean! ;). I look forward to hearing your feedback. I’d love to be able to show people that there’s a demand for this type of framework and development model (transparency, source releases, etc…) and maybe we can get more than one MVC talk next time. I think it’s time I do a really advanced one. :)

Technorati Tags: pdc2008,aspnetmvc,presentation,pdc

asp.net, asp.net mvc, code comments suggest edit

Download the sample project to play with the code as you read this blog post.

Using the DefaultModelBinder in ASP.NET MVC, you can bind submitted form values to arguments of an action method. But what if that argument is a collection? Can you bind a posted form to an ICollection<T>?

Sure thing! It’s really easy if you’re posting a bunch of primitive types. For example, suppose you have the following action method.

public ActionResult UpdateInts(ICollection<int> ints) {

  return View(ints);
}

You can bind to that by simply submitting a bunch of form fields each having the same name. For example, here’s an example of a form that would bind to this, assuming you keep each value a proper integer.

<form method="post" action="/Home/UpdateInts">
    <input type="text" name="ints" value="1" />
    <input type="text" name="ints" value="4" />
    <input type="text" name="ints" value="2" />
    <input type="text" name="ints" value="8" />
    <input type="submit" />
</form>

If you were to take fiddler and look at what data actually gets posted when clicking the submit button, you’d see the following.

ints=1&ints=4&ints=2&ints=8

The default model binder sees all these name/value pairs with the same name and converts that to a collection with the key ints, which is then matched up with the ints parameter to your action method. Pretty simple!

Where it gets trickier is when you want to post a list of complex types. Suppose you have the following class and action method.

public class Book {
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime DatePublished { get; set; }
}

//Action method on HomeController
public ActionResult UpdateProducts(ICollection<Book> books) {
    return View(books);
}

You might think we could simply post the following to that action method:

Title=title+one&Author=author+one&DateTime=1/23/1975 &Title=author+two&Author=author+two&DateTime=6/6/2007…

Notice how we simply repeat each property of the book in the form post data? Unfortunately, that wouldn’t be a very robust approach. One reason is that we can’t distinguish from the fact that there may well be another Title input unrelated to our list of books which could throw off our binding.

Another reason is that the checkbox input does not submit a value if it isn’t checked. Most input fields, when left blank, will submit the field name with a blank value. With a checkbox, neither the name nor value is submitted if it’s unchecked! This again can throw off the ability of the model binder to match up submitted form values to the correct object in the list.

To bind complex objects, we need to provide an index for each item, rather than relying on the order of items. This ensures we can unambiguously match up the submitted properties with the correct object.

Here’s an example of a form that submits three books.

<form method="post" action="/Home/Create">

    <input type="text" name="[0].Title" value="Curious George" />
    <input type="text" name="[0].Author" value="H.A. Rey" />
    <input type="text" name="[0].DatePublished" value="2/23/1973" />
    
    <input type="text" name="[1].Title" value="Code Complete" />
    <input type="text" name="[1].Author" value="Steve McConnell" />
    <input type="text" name="[1].DatePublished" value="6/9/2004" />
    
    <input type="text" name="[2].Title" value="The Two Towers" />
    <input type="text" name="[2].Author" value="JRR Tolkien" />
    <input type="text" name="[2].DatePublished" value="6/1/2005" />
    
    <input type="submit" />
</form>

Note that the index must be an unbroken sequence of integers starting at 0 and increasing by 1 for each element.

The new expression based helpers in ASP.NET MVC 2 will produce the correct format within a for loop. Here’s an example of a view that outputs this format:

<%@ Page Inherits="ViewPage<IList<Book>>" %>

<% for (int i = 0; i < 3; i++) { %>

  <%: Html.TextBoxFor(m => m[i].Title) %>
  <%: Html.TextBoxFor(m => m[i].Author) %>
  <%: Html.TextBoxFor(m => m[i].DatePublished) %> 

<% } %>

It also works with our templated helpers. For example, we can take the part inside the for loop and put it in a Books.ascx editor template.

<%@ Control Inherits="ViewUserControl<Book>" %>

<%: Html.TextBoxFor(m => m.Title) %>
<%: Html.TextBoxFor(m => m.Author) %>
<%: Html.TextBoxFor(m => m.DatePublished) %> 

Just add a folder named EditorTemplates within the Views/Shared folder and add Books.ascx to this folder.

Now change the original view to look like:

<%@ Page Inherits="ViewPage<IList<Book>>" %>

<% for (int i = 0; i < 3; i++) { %>

  <%: Html.EditorFor(m => m[i]) %>

<% } %>

Non-Sequential Indices

Well that’s all great and all, but what happens when you can’t guarantee that the submitted values will maintain a sequential index? For example, suppose you want to allow deleting rows before submitting a list of books via JavaScript.

The good news is that by introducing an extra hidden input, you can allow for arbitrary indices. In the example below, we provide a hidden input with the .Index suffix for each item we need to bind to the list. The name of each of these hidden inputs are the same, so as described earlier, this will give the model binder a nice collection of indices to look for when binding to the list.

<form method="post" action="/Home/Create">

    <input type="hidden" name="products.Index" value="cold" />
    <input type="text" name="products[cold].Name" value="Beer" />
    <input type="text" name="products[cold].Price" value="7.32" />
    
    <input type="hidden" name="products.Index" value="123" />
    <input type="text" name="products[123].Name" value="Chips" />
    <input type="text" name="products[123].Price" value="2.23" />
    
    <input type="hidden" name="products.Index" value="caliente" />
    <input type="text" name="products[caliente].Name" value="Salsa" />
    <input type="text" name="products[caliente].Price" value="1.23" />
    
    <input type="submit" />
</form>

Unfortunately, we don’t have a helper for generating these hidden inputs. However, I’ve hacked together an extension method which can render this out for you.

When you’re creating a form to bind a list, add the following hidden input and it will add the appropriate hidden input to allow for a broken sequence of indices. Use at your own risk!I’ve only tested this in a couple of scenarios. I’ve included a sample project with multiple samples of binding to a list which includes the source code for this helper.

<%: Html.HiddenIndexerInputForModel() %>

This is something we may consider adding to a future version of ASP.NET MVC. In the meanwhile, give it a whirl and let us know how it works out for you.

Technorati Tags: aspnetmvc,modelbinders

personal comments suggest edit

I just can’t help myself. I said I wouldn’t be one of those parents, but forget it. I am one of those parents. I think my kid is adorable, so sue me. Check out Cody’s halloween costume. He’s with his BFF Forever, Alex the Panda.

Meet, Codysaurus!

codysaurus-and-alexpanda

Coming to terrorize a neighborhood near you.

asp.net, asp.net mvc comments suggest edit

Today we finally officially released the beta of ASP.NET MVC (go download it already!).

True, the release has actually been available online since yesterday as it was announced in a Keynote at VSLive by Scott Hanselman, but that was intended to be a special treat for attendees in what ended up being the worst kept secret in .NET-dom.

As usual, to get all the details, check out the latest epic installment on ScottGu’s blog. Scott Hanselman also has a great blog post with good coverage as well.

As I warned before, we no longer bundle the Mvc Futures assembly (Microsoft.Web.Mvc.dll). However, we did just publish a release of this assembly updated for Beta on CodePlex. Source code for the Beta and Futures releases will be pushed to CodePlex shortly. Sorry about the delay but there’s so much work to be done here. :)

One very exciting element to this release is that we’ve included JQuery (as I mentioned before) and are indeed the first Microsoft product to include it. One of the first Microsoft products (AFAIK) to bundle a third-party open source component.

The following list (each a link to the GU’s blog) show’s what’s new in the Beta.

What’s new in ASP.NET MVC Beta?

I hope you enjoy this release and the plan from here on out is primarily to focus on stabilization. This means fixing bugs, making design change requests which we feel hit meet a high bar, and getting the product ready for RTM. However, as Scott mentioned, there are a few new features we’re planning, especially with Visual Studio tooling.

personal comments suggest edit

bowl-mm Today marks my one year anniversary at Microsoft. Tradition dictates that I bring in a pound of M&Ms for each year that I’ve been an employee. I’m going to buck that trend (because I like bucking things) and bring in 1 kilo of Japanese candies. Since I just returned from a trip to Japan and it is also customary to bring gifts back from a trip, this ends up killing two birds with one stone. Software is not the only place to apply the DRY principle.

Looking back at when I first was hired and later at my first days when I drank from the firehose, working here feels a lot different now than it did then. I finally feel a lot more settled at Microsoft, though there are still many days when I feel like a new hire, or even as a bit of an outsider.

The other day in an IM chat with Scott Hanselman, he made some comment about how us Microsofties love our app_codeApp_Data directory. Hey man, his badge is just as blue as mine, but I understand the sentiment. Guys like us are referred to as “industry hires” as opposed to college hires. Many of the developers here happen to be college hires. The theory is that industry hires will bring a fresh perspective, but I think we just mostly get in everyone’s way and stir up trouble.

So what have I done in this past year?

I’ve spoken at five conferences (Tech-Ed Hong Kong, TechReady 6 and 7, DevScovery, MVP Global Summit) and two internal TAP events. I’ve attended two other conferences (Mix 08, Google IO). For the most part, I think I’ve gotten over my stage fright, which was intense in the beginning. Hopefully, I’ve also improved as a speaker, but I still cringe when I hear myself speak.

I’ve been involved in around four or five ASP.NET MVC preview releases (I’ve lost count) as well as the release of ASP.NET 3.5 SP1 (ASP.NET Routing feature) and a CodePlex release of ASP.NET Dynamic Languages support. I’m particularly excited about taking ownership of our DLR support for ASP.NET and hope that in the new year I can push that forward.

I’ve also been involved in division wide efforts to help other teams understand Test Driven Development so that our products moving forward will take TDD into consideration in the design of their products.

Amidst all this, I even found some time to get Subtext 2.0 out the door. Open source software remains a passion for me and I’m very excited about all the progressive changes that have happened here in the past year from us including JQuery in our offering to the opening up of licensing in various products such as MEF.

What’s Next For Me?

I think I’ll stay here for a while. My wife and I really like it in Bellevue and so far, I’m really enjoying the work here. In the next year, I’ll be taking on more responsibilities. I’ll be taking ownership for driving our TDD efforts, hoping to obtain a few small wins here and there. Baby steps.

I’ll also keep driving ASP.NET MVC towards RTM and put together a plan and strategy together for our Dynamic Languages effort.

I’m still trying to wrap my head around the PM role here at Microsoft. My managers never fail to remind me that while I’m doing fine at the technical and community side of things, I really need to improve the project management side of things. Understandable, I’m doing great at the fun stuff, but not so good at the part that I don’t find so fun. :)

In any case, as I did before, I want to thank many of you for helping me with my job. There have been many large design improvements to ASP.NET MVC that quite possibly wouldn’t have happened were it not for your constant feedback. It really is appreciated.

asp.net comments suggest edit

I’ve used the term “drinking from the fire hose” when describing my first days at Microsoft. However, I believe that a lot of our customers feel this way when approaching the plethora of options for web application development on the Microsoft stack.

This is feedback we’ve received from many sources and as Scott Hanselman pointed out, there’s a concerted effort to make things easier to find and understand here. Much of these efforts will take time to see fruition, but some of them are happening now.

The new Microsoft Web Platform Installer Beta can get you up and running developing on ASP.NET in two easy clicks.

First, you select an option from the first screen. Obviously, if you choose the Your Choice option, it’ll take more than two clicks.

choose-type

I chose the ASP.NET Developer option and clicked Next.

accept-license

The second screen lets you choose to accept the licensing terms. This is nice in that you don’t have to go and click through 20 different EULAs.

installing

Once you click I Accept the installer begins.

If you want more control, you can choose Your Choiceat the first screen which takes you to a screen that lets you choose all sorts of options.

your-choice

This is a great tool for someone who wants to quickly get started learning and developing on ASP.NET. You’ll notice that it doesn’t include ASP.NET MVC yet. Don’t worry, it will once we have a proper beta release.

If you run into any problems with it, be sure to report it on their forums. For more information, be sure to read the announcement on Bill Staple’s blog.

In case you missed my first link to the beta for the installer, it’s here.

code comments suggest edit

UPDATE: Pretty much disregard this entire post, except as a reminder that it’s easy to make a mistake with DOCTYPEs and markup. As many people have told me, I had an error I didn’t notice in the original HTML. I forgot to close the SELECT tag. I’ll leave the post as-is.

Not only that, the DOCTYPE is not specified in the document, which causes IE to render in Quirks mode, not standards mode. So I guess the bug is in IE 7 rendering.

So this is a case of PEBKAC, the bug is in the HTML, not the browser.

Here’s an example of the HTML with the SELECT tag properly closed and the proper DOCTYPE. It renders correctly in FireFox 3 and IE 8.

In testing our helper methods for rendering a <select /> element which has some styling applied to it if the element has a validation error, a developer on the MVC team found an interesting bug in how browsers render a select element with a border applied to it via CSS. Check out the following HTML.

<html>
<head>
<style type="text/css">
select {
border: 1px solid #ff0000;
color: #ff0000;
}
</style>
</head>
<body>
 
<select >
  <option>A</option>
<select>
 
</body>
</html>

Here’s how IE 8 renders it. Notice there’s no border. UPDATE: According to people on twitter, this is because I left out the doctype, so IE8 rendered it in old quirks mode, not standards mode.

 ie8-select

Here’s Firefox 3. There’s a border, but there’s two drop-down arrows.

select-ff

Here’s Google Chrome, which gets it right. Since Google Chrome uses the Safari Webkit rendering engine, I believe Safari gets it right as well. I didn’t test it personally, but Opera gets it right too.

select-chrome

Now if you add the following meta tag to the <head /> section of the HTML.

<meta http-equiv='X-UA-Compatible' content='IE=8'>

IE 8 now renders correctly.

select-ie8-meta

You can see for yourself by pointing your browser to an example with the meta tag and without it.

personal comments suggest edit

Just got back from our trip to latest Japan yesterday morning. On previous trips, I ate really great Yakitori, ate Blowfish and lived to tell about it, celebrated the new year, , learned about ritual suicide and played around with sharp swords, visited a temple in the midst of old Tokyo, and visited the hotel from Spirited Away.

Golden
PalaceThis trip was mostly a relaxing affair consisting of eating good food and lying around doing as much nothing as we could muster. Unfortunately, travelling with a 15 month old child meant that the amount of actual relaxation to be had was certain to be capped.

The most exciting part of the trip was my first ever visit to Kyoto, a beautiful city full of Shinto shrines and beautiful temples. It is also the namesake of the Kyoto Protocol.

Japan Trip
763I’ve uploaded a fair amount of photos to FaceBook, but I thought I’d share a few here along with a brief commentary. These first two shots are of a stunning building covered in gold leaf. The leaf is very securely fastened to the building. Don’t ask me how I know that.

Apparently, there’s a silver temple in Kyoto as well, but the guy building it ran out of money before it was ever covered in silver.

While in Kyoto, we stayed at the Westin Miyako Kyoto. They have two types of rooms, the traditional western style you all know and love, and the Japanese style as seen in this next photo, exquisitely modeled by my son. These consist of a single large simple room. At night, they put away the table and chairs and lay down tatami mats.

Japan Trip
660

Thankfully, the toilet was western style because I’m not so talented at squatting.

The landscaping and natural beauty of many of the shrines and temples were simply astounding. And it’s no accident. We saw one guy painstakingly hand picking out tiny little weeds on a huge lawn.

Japan Trip
674One of the highlights of this trip was meeting Cody’s cousin, Rio, for the first time.

Japan Trip
837

 Japan Trip
802 Japan Trip
686

As you can see in the photo, she’s already learned the fine art of giving a Wet-u Wirry (Wet Willy in English). Perhaps that’s payback for all the bear hugs Cody attempted to give her which ended becoming football tackles.

In any case, if you’re ever taking a long trip to Japan, be sure to try and fit Kyoto into your itinerary. I really love Tokyo for its Blade Runneresque hyper neon modernism, which makes a visit to Kyoto so complementary for its ancient feeling.

comments suggest edit

I had a bit of a rough start to my first Tech-Ed Hong Kong last week. Pretty much every day while I was in Japan, I dutifully pulled out the laptop (despite my lack of internet connection) and made sure it still worked fine.

Things seemed to be looking up when I got a free business class upgrade on the way to Hong Kong from Tokyo for giving up my seat. It meant taking a longer flight, but I had a really enjoyable flight. But while waiting in the airport lounge, I decided to take advantage of the free Wi-Fi there, but couldn’t get my computer screen to display anything. Hoping it was some weird hibernation issue, I put my laptop away and decided to wait till I was in HK.

Japan Trip
883 Sure enough, the screen still didn’t work. Fortunately, I follow rule #1 of the Joel Test for all my presentations and keep it all in source control using a private instance of Subversion. A member of Microsoft Hong Kong kindly lent me his laptop (thank you!) and I got it into more-or-less working order, as you can see from this shot of the room just before I began my first talk on ASP.NET MVC.

Even so, working on an unfamiliar laptop is still a pain and there were a few hitches in demos where I wasn’t sure how to change various display modes quickly on the laptop.

Since my talks were all in the morning, it gave me time to travel around a bit.

Japan Trip
889 Japan Trip
892

I took a tram up to Victoria Peak to get an eye catching panoramic view of the city, though the day I chose to go was not as beautiful as the next two days.

On the first night, there was a little Microsoft get-together for MVPs and employees at Cenna Bar and Lounge. The entrance to the place is practically hidden via a non-descript doorway on this street. You walk in, and take the lift up to the 23rd floor and suddenly you’re in this small but hip little bar.

Seems like a lot of cool places are hidden away up high in these buildings.

Japan Trip
918 Japan Trip
919

I really enjoyed the opportunity to have some great conversations with various Chinese people, as many of the attendees were from mainland China. In our conversations I realized that certain stereotypes we tend to have over in the U.S. are completely not valid. In principle, I know this is usually the case, but it often takes engaging in very interesting conversations for that to really hit home.

Japan Trip
926   Japan Trip
921

Japan Trip
928 Japan Trip
929

Afterwards, a small group of us went shopping. There is no sales tax on most items in Hong Kong, so it’s a popular place for Chinese shoppers. I merely tagged along for the experience.

The next day I did some more sight-seeing around the city, taking a Star Ferry across to Kowloon and then walking around the Central and Wan-Chai districts afterwards.

Japan Trip
937  Japan Trip
947

 Japan Trip
952 Japan Trip
957

On the last evening, I met up with an old friend from college from Hong Kong for a night on the town in which we mixed and mingled with the local denizens.

Japan Trip
962 Japan Trip
972

Japan Trip
974 Japan Trip
978

Notice that Microsoft’s LINQ technology is so popular that there’s a bar named after it. I believe another bar called “to SQL” was just around the corner.

As a strategy to beat jet lag, I ended up staying out all night until it was time to catch a cab to the airport, stopping at my hotel room to quickly grab my things. I’ll let you know tomorrow if it worked.

personal, asp.net, asp.net mvc comments suggest edit

Where have I been? ;) You probably heard the news already from the GU already, but just in case, we will be shipping JQuery with Visual Studio. ASP.NET MVC will have the privilege of being one of the first products to include JQuery. I am glad we finally announced this because I got tired of stifling my mouth everytime someone suggested we just include JQuery. :)

As you can see from demos I’ve done in the past, JQuery will fit nicely with the ASP.NET MVC style of development.

personal comments suggest edit

The internet access I had at my mother-in-law’s last time I was in Japan turned out to be a fluke. I am at a Japanese Manga and Internet cafe (because those three things go so well together) right now typing this out. I’ve received a lot of comments and questions via my blog and once I get to Hong Kong, I will do my best to answer.

No promises though as I hear that the pool at the hotel is nice and I do have three talks to prepare. I must admit that not having daily internet access is probably a good thing for me as I’m a total online junkie. :)

The next time we visit, I need to remember to bring more reading material. I brought two books on Poker and am tired of reading about it. ;)

Technorati Tags: japan,tokyo

asp.net, dlr comments suggest edit

This afternoon we released a refresh of our DLR/IronPython support for ASP.NET, now called “ASP.NET Dynamic Language Support”, on our CodePlex site.

This was originally part of our July 2007 ASP.NET Futures package, along with several other features. As updates to these features were made available, we would have liked to remove them from the package, but we wanted to wait till everything within the package was updated.

Well that time has come. This CodePlex release contains two exceedingly simple sample applications, one for WebForms and one for ASP.NET MVC. It’s compiled against the latest DLR assemblies, and our goal is to continue to push it forward fixing bugs here and there. Keep in mind that this initial refresh is pretty barebones and doesn’t contain everything that the original package contained because certain features (such as the project system) are still being updated.

I won’t go too deeply into the specifics of how to use it. Instead, be sure to check out David Ebbo’s whitepaper on IronPython and ASP.NET which was written a while ago, but still mostly relevant. Also, Jimmy Schementi from the DLR team has written a nice brief write-up on this release.

I have the pleasure of taking over as the PM for this feature (in MS parlance we’d say I “own” this feature now) which nicely complements my duties as the PM for ASP.NET MVC. If you’ve followed my blog, you know I have an interest in dynamic languages and now I can channel that interest into work time, rather than on my own time. :)

This initial release only has IronPython support, but IronRuby support will be coming soon. This gives me an opportunity to learn a bit about Python, and let me tell you, the fact that whitespace matters in this language can be nice within a normal code file, but a real pain within a view.

One nice thing about this implementation above and beyond my old IronRuby prototype is that it has true support for a Global.py file, the IronPython equivalent for Global.asax.cs. This allowed me to define my routes in IronPython directly in that file rather than reading in a separate file. I did implement some helper methods in C# that make it easy to define routes using a Python dictionary.

personal comments suggest edit

If you happen to be in Asia around October 8-10, I’ll be speaking at Tech-Ed Hong Kong. Come by and say hi. I’m giving three talks, one on each day.

Date Time Talk
October 8 11:45 AM – 1:00 PM ASP.NET MVC  - An alternative approach to building Web Applications
October 9 11:00 AM – 12:15 PM Developing Data Driven Applications Using ASP.NET Dynamic Data
October 10 9:30 AM – 10:45 AM Write better designed code with Test Driven Development

hong_kongI’m hoping to have a little time after some of my talks to go do a little sight-seeing around Hong Kong. The trip to Hong Kong is actually a side trip from Japan where my wife and kid will stay while I go and and speak at this conference. I’m looking forward to the vacation, but wish I had scheduled the vacation part after the conference, rather than before. Lesson learned!

Technorati Tags: conferences,aspnetmvc,tdd,dynamic data

asp.net, asp.net mvc comments suggest edit

pdc2008 First of all, I want to congratulate Jeff Atwood, Joel Spolsky, and their team for the release of StackOverflow.com. If you haven’t tried it out, I highly recommend giving it a shot. Be prepared, it’s addicting.

Besides my 959 reputation score (which is actually pretty weak), the other thing about StackOverflow that excites me is that it’s built using ASP.NET MVC. So far, Jeff has mostly praised the experience of using ASP.NET MVC, though he’s had a few pain points that I’m now well aware of. :)

Stackoverflow
LogoI like StackOverflow so much that I asked Jeff to take a 10-15 minute slice of my ASP.NET MVC talk at PDC to talk about his experiences building StackOverflow using ASP.NET MVC. Having a demonstration of a real-world application using ASP.NET MVC will be a nice complement to my overview talk. For the rest of the talk, he’ll be my code monkey.

By the way, if you’re planning to attend PDC, please navigate to the link to my talk and leave a comment with any requests for things you’d love to hear me talk about. I am currently planning to give the general overview, but perhaps some of you want to hear more anecdotes from the product team, or more details about specific features.

Technorati Tags: aspnetmvc,stackoverflow,pdc2008

asp.net mvc, asp.net comments suggest edit

UPDATE: The MVC Futures assembly, Microsoft.Web.Mvc is available on CodePlex.

Wanted to provide a quick heads up about the MvcFutures assembly within ASP.NET MVC CodePlex Preview 5. As mentioned in various places, this assembly contains various experimental features we are considering for future versions of ASP.NET MVC.

When we release the BETA for ASP.NET MVC, it will not automatically be included in the project template by the installer. We’ve included it in the various previews for convenience, but we want the BETA installer to be as close to the RTM installer experience as possible.

We will make sure that the assembly remains available on CodePlex. I just wanted to make you aware of this so there is no surprises when we release the Beta regarding this. Thanks!

Technorati Tags: aspnetmvc

asp.net mvc, asp.net comments suggest edit

This is one of them “coming of age” stories about how a lowly method becomes a full fledged Action in ASP.NET MVC. You might think the two things are the same thing, but that’s not the case. It is not just any method gets to take the mantle of being an Action method.

Routing

Like any good story, it all begins at the beginning with Routing. By default, one of the routes defined in the MVC project template has the following URL pattern:

{controller}/{action}/{id}

When a request comes in and matches that route, we populate a dictionary of route values (accessible via the RequestContext) based on this route. For example, if a request comes in for:

/home/list/123

We add the key “action” with the value “list” to the route values dictionary (We’ve also added “home” as the value for “controller”, but that’s for another story. This is the story of the action.) At the heart of it, an action is just a string. That’s how it starts out after all, as a sub string of the URL.

Later on, when the request is handed of to MVC, MVC interprets the value in the route values for “action” to be the action name. In this case, it knows that the request should be handled by the action “list”. Contrary to popular belief, this does not necessarily mean that a method named List will handle this request,as we’ll soon see.

Action Method Selection

Once we’ve identified the name of the action, we need to identify a method that can respond to that action. This is the job of the ControllerActionInvoker.

By default, the invoker simply uses reflection to find a public method on a class that derives from Controller which has the same name (case insensitive) as the current action.

Like many things within this framework, you can tweak this default behavior.

ActionNameAttribute

Introduced in ASP.NET MVC CodePlex Preview 5 which we just released, applying this attribute to a method allows you to specify the action that the method handles.

For example, suppose you want to have an action named View, this would conflict with the View method of Controller. An easy way to work around this issue without having to futz with routing or method hiding is to do the following:

[ActionName("View")]
public ActionResult ViewSomething(string id)
{
  return View();
}

The ActionNameAttribute redefines the name of this action to be “View”. Thus this method is invoked in response to requests for /home/view, but not for /home/viewsomething. In the latter case, as far as the action invoker is concerned, an action method named “ViewSomething” does not exist.

One consequence of this is that if you’re using our conventional approach to locate the view that corresponds to this action, the view should be named after the action, not after the method. In the above example (assuming this is a method of HomeController), we would look for the view ~/Views/Home/View.aspx by default.

This attribute is not required on an action method. Implicitly, the name of a public method serves as the action name for that method.

ActionSelectionAttribute

We’re not done yet matching the action to a method. Once we’ve identified all methods of the Controller class that match the current action name, we need to whittle the list down further by looking at all instances of the ActionSelectionAttribute applied to the methods in the list.

This attribute is an abstract base class for attributes which provide fine grained control over which requests an action method can respond to. The API for this method is quite simple and consists of a single method.

public abstract class ActionSelectionAttribute : Attribute
{
  public abstract bool IsValidForRequest(ControllerContext controllerContext
    , MethodInfo methodInfo);
}

At this point, the invoker looks for any methods in the list which contain attributes which derive from this attribute and calls the IsValidForRequest() method on each attribute. If any attribute returns false, the method that the attribute is applied to is removed from the list of potential action methods for the current request.

At the end, we should be left with one method in the list, which the invoker then invokes. If more than one method can handle the current request, the invoker throws an exception indicating the problem. If no method can handle the request, the invoker calls HandleUnknownAction() on the controller.

The ASP.NET MVC framework includes one implementation of this base attribute, the AcceptVerbsAttribute.

AcceptVerbsAttribute

This is a concrete implementation of ActionSelectionAttribute which uses the current HTTP request’s http method (aka verb) to determine whether or not a method is the action that should handle the current request.

This allows for having two methods of the same name (different parameters of course) to both be actions, but respond to different HTTP verbs.

For example, we may want two versions of the Edit method, one which renders the edit form, and the other which handles the request when that form is posted.

[AcceptVerbs("GET")]
public ActionResult Edit(string id)
{
  return View();
}

[AcceptVerbs("POST")]
public ActionResult Edit(string id, FormCollection form)
{
  //Save the item and redirect…
}

When a POST request for /home/edit is received, the action invoker creates a list of all methods of the controller that match the “edit” action name. In this case, we would end up with a list of two methods. Afterwards, the invoker looks at all of the ActionSelectionAttribute instances applied to each method and calls the IsValidForRequest() method on each. If each attribute returns true, then the method is considered valid for the current action.

For example, in this case, when we ask the first method if it can handle a POST request, it would respond with false because it only handles GET requests. The second method responds with true because it can handle the POST request and it is the one selected to handle the action.

Helpers

One consequence to keep in mind when using helpers which use our routing API to generate URLs is that the parameters for all of these helpers take in the action name,not the method name. So if I want to render the URL to the following action:

[ActionName("List")]
public ActionResult ListSomething()
{
  //...
}

Use “List” and not “ListSomething” as the action name.

<!-- WRONG! -->
<%= Url.Action("ListSomething") %>

<!-- RIGHT! -->
<%= Url.Action("List") %>

This is one reason you’ve seen the MVC team resistant to including helper methods, such as Url<T>(…), that use an expression to define the URL of an action. The action is not necessarily equivalent to a method on the class with the same name.

Summary

So in the end, an action is a logical concept that represents an event caused by the user (such as clicking a link or posting a form) which is eventually mapped to a method which handles that user event.

It’s convenient to think of an action as a method of the same name, but they are distinct concepts. A lowly method can become an action by the power of its own name (aka name dropping), but in this egalitarian framework, any method, no matter its name, can handle a particular action, by merely using the ActionNameAttribute.

Technorati Tags: aspnetmvc,routing