Maarten Balliauw {blog}

ASP.NET MVC, Microsoft Azure, PHP, web development ...

NAVIGATION - SEARCH

New CodePlex project: MvcSiteMap – ASP.NET MVC sitemap provider

NavigationIf you have been using the ASP.NET MVC framework, you possibly have been searching for something like the classic ASP.NET sitemap. After you've played with it, you even found it useful! But not really flexible and easy to map to routes and controllers. To tackle that, last year, somewhere in August, I released a proof-of-concept sitemap provider for the ASP.NET MVC framework on my blog.

The blog post on sitemap provider I released back then has received numerous comments, suggestions, code snippets, … Together with Patrice Calve, we’ve released a new version of the sitemap provider on CodePlex: MvcSiteMap.

This time I’ll not dive into implementation details, but provide you with some of the features our sitemap provider erm… provides.

First things first: registering the provider

After downloading (and compiling) MvcSiteMap, you will have to add a reference to the assembly in your project. Also, you will have to register the provider in your Web.config file. Add the following code somewhere in the <system.web> section:

[code:c#]

<siteMap defaultProvider="MvcSiteMap">
    <providers>
        <add name="MvcSiteMap"
             type="MvcSiteMap.Core.MvcSiteMapProvider" 
             siteMapFile="~/Web.Sitemap"
             securityTrimmingEnabled="true"
             cacheDuration="10"/>
    </providers>
</siteMap>

[/code]

We’ve just told ASP.NET to use the MvcSiteMap sitemap provider, read sitemap nodes from the Web.sitemap file, use secrity trimming and cache the nodes for 10 minutes.

Defining sitemap nodes

Defining sitemap nodes is quite easy: add a Web.sitemap file to your project and popukate it with some nodes. Here’s an example:

[code:c#]

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
  <mvcSiteMapNode title="Home" controller="Home" action="Index" isDynamic="true" dynamicParameters="*">
    <mvcSiteMapNode title="About Us" controller="Home" action="About" />

    <mvcSiteMapNode title="Products" controller="Products">
      <mvcSiteMapNode title="Will be replaced in controller"
                      controller="Products" action="List"
                      isDynamic="true" dynamicParameters="id"
                      key="ProductsListCategory"/>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="Account" controller="Account">
      <mvcSiteMapNode title="Login" controller="Account" action="LogOn" />
      <mvcSiteMapNode title="Account Creation" controller="Account" action="Register" />
      <mvcSiteMapNode title="Change Password" controller="Account" action="ChangePassword" />
      <mvcSiteMapNode title="Logout" controller="Account" action="LogOff" />
    </mvcSiteMapNode>
  </mvcSiteMapNode>
</siteMap>

[/code]

Too much info? Let’s break it down. The sitemap consists of several nodes, defined by using a <mvcSiteMapNode> element. Each node can contain other nodes, as you can see in the above example. A node should also define some attributes: title and controller. Title is used by all sitemap controls of ASP.NET, controller is used to determine the controller to link to. Here’s a list of possible attributes:

Attribute Required? Description
title Yes Title of the node.
controller Yes Controller the node should link to.
action Optional Action method of the specified controller the node should link to.
key Optional A key used to identify the node. Can be specified but is generated by the MvcSiteMap sitemap provider when left blank.
isDynamic Optional Specifies if this is a dynamic node (explained later)
dynamicParameters When isDynamic is set to true. Specifies which parameters are dynamic. Multiple can be specified using a comma (,) as separator.
visibility Optional When visibility is set to InSiteMapPathOnly, the node will not be rendered in the menu.
* Optional Any other parameter will be considered to be an action method parameter.

Regarding the wildcard (*), here’s a sample sitemap node:

[code:c#]

<mvcSiteMapNode title="Contact Maarten" controller="About" action="Contact" who=”Maarten” />

[/code]

This node will map to the URL http://…./About/Contact/Maarten.

Using the sitemap

We can, for example, add breadcrumbs to our master page. Here’s how:

[code:c#]

<asp:SiteMapPath ID="SiteMapPath" runat="server"></asp:SiteMapPath>

[/code]

Looks exactly like ASP.NET Webforms, no?

Dynamic parameters

You got to click it, before you kick it. In the table mentioned above, you may have seen the isDynamic and dynamicParameters attributes. This may sound a bit fuzzy, but it’s actually quite a powerful feature. Consider the following sitemap node:

[code:c#]

<mvcSiteMapNode title="Product details" controller="Product" action="Details" isDynamic=”true” dynamicParameters=”id” />

[/code]

This node will actually be used by the sitemap controls when any URL refering /Products/Details/… is called:

  • http://…./Products/Details/1234
  • http://…./Products/Details/5678
  • http://…./Products/Details/9012

No need for separate sitemap nodes for each of the above URLs! One node is enough to provide your users with a consistent breadcrumb showing their location in your web application.

The MvcSiteMapNode attribute

Who said sitemaps should always be completely defined in XML? Why not use the MvcSiteMapNode attribute we created:

[code:c#]

[MvcSiteMapNode(ParentKey="ProductsListCategory", Title="Product details", IsDynamic=true, DynamicParameters="id")]
public ActionResult Details(string id)
{
    // ...
}

[/code]

We are simply telling the MvcSiteMap sitemap provider to add a child node to the node with key “ProductsListCategory” which should have the title “Product details”. Controller and action are simply determined by the sitemap provider, based on the action method this attribute is declared on. Dynamic parameters also work here, by the way.

Do you have an example?

Yes! Simply navigate to the MvcSiteMap project page on CodePlex and grab the latest source code. The sitemap provider is included as well as an example website demonstrating all features.

kick it on DotNetKicks.com

Sample chapter from ASP.NET MVC 1.0 Quickly

image Here’s a shameless, commercial blogpost… With yesterday’s 1.0 release of the ASP.NET MVC framework, I’m sure the following sample chapter from my book ASP.NET MVC 1.0 Quickly will be of use for people starting ASP.NET MVC development: Your first ASP.NET MVC application.

When downloading and installing the ASP.NET MVC framework SDK, a new project template is installed in Visual Studio. This chapter describes how to use the ASP.NET MVC project template that is installed in Visual Studio. All ASP.NET MVC aspects are touched briefly by creating a new ASP.NET MVC web application based on this Visual Studio template. Besides view, controller, and model, new concepts including ViewData—a means of transferring data between controller and view, routing—the link between a web browser URL and a specific action method inside a controller, and unit testing of a controller are also illustrated here.

In this chapter, you will:

  • Have an overview of all the aspects of an ASP.NET MVC web application
  • Explore the ASP.NET MVC web application project template that is installed in Visual Studio 2008
  • Create a first action method and corresponding view
  • Create a strong-typed view
  • Learn how a controller action method can pass strong-typed ViewData to the view
  • Learn what unit testing is all about, and why it should be used
  • Learn how to create a unit test for an action method by using Visual Studio's unit test generation wizard and modifying the unit test code by hand

Download the free sample chapter here. Or order the full book, here. That’s a better option ;-)

By the way, if you are interested in the book writing process itself, check my previous blog post on that.

kick it on DotNetKicks.com

ASP.NET MVC 1.0 has been released!

ASP.NET MVC 1.0 QuicklyTo keep up with a good tradition (see here and here), I have some great news on ASP.NET MVC: we are at version 1.0! This means production ready, supported, stable, …! Grab the download at Microsoft.com.

I’m expecting an epic blog post by the Gu, but here’s some stuff you may want to have a look at: all my posts on ASP.NET MVC.

Another thing you can do: order my book on ASP.NET MVC :-) We’ve released the print version yesterday, meaning you are now completely set to start developing with ASP.NET MVC.

Edit: Looks like Simone was equally fast :-) And Kris.
Edit: More from MIX: Silverlight 3 SDK Beta 1 is already up! http://tinyurl.com/crfogs

kick it on DotNetKicks.com

Accessing Windows Azure Blob Storage from PHP

Pfew! A week of Microsoft TechDays here in Belgium with lots of talks on new Microsoft stuff, Azure included. You may know I already experimented with Windows Azure and ASP.NET MVC. Earlier this week, I thought of doing the same with Windows Azure and PHP...

What the ...?

imageAt Microsoft PDC 2008, the Azure Services Platform was announced in the opening keynote. Azure is the name for Microsoft’s Software + Services platform, an operating system in the cloud providing services for hosting, management, scalable storage with support for simple blobs, tables, and queues, as well as a management infrastructure for provisioning and geo-distribution of cloud-based services, and a development platform for the Azure Services layer.

You can currently download the Windows Azure SDK from www.azure.com and play with it on your local computer. Make sure to sign-up at the Azure site: you might get lucky and receive a key to test the real thing.

And what does PHP have to do with this?

As a reader of my blog, this should not be a question. I'm on the thin line between the Microsoft development environment (.NET) and PHP development environment, and I really like bridging the two together (think PHPExcel, PHPLinq). And cheap, distributed hosting of data (be it file or databases) is always interesting to use, especially in web applications where you may store anything your users upload. I have created a Zend Framework Proposal for this, let's hope this blog post ends as a contribution to the Zend Framework.

Show me the good stuff!

Will do! Currently, I have only implemented Azure blob storage in PHP, so that's what the following code snippets will be using. Enough blahblah now, here's how you connect with Azure:

[code:c#]

$storage = new Zend_Azure_Storage_Blob();

[/code]

This actually sets up a connection with the local Azure storage service from the Windows Azure SDK. You can, however, also pass in an account name and shared key from the real, cloud hosted Azure, too. Next: I want to create a storage container. A storage container is a logical group in which I can store any data. Let's name the container "azuretest":

[code:c#]

$storage->createContainer('azuretest');

[/code]

Easy? Yup! Azure now has created some space on their distributed storage for my files to be dumped in. Speaking of which: let's upload a file!

[code:c#]

$storage->putBlob('azuretest', 'images/WindowsAzure.gif', './WindowsAzure.gif');

[/code]

There we go. I've uploaded my local WindowsAzure.gif file to the azuretest container and named the file "images/WindowsAzure.gif'". Don't be confused: it is NOT stored in the images/ folder (there's no such thing on Azure), this is really the full filename. But don't worry, you can mimic a regular filesystem with folders, for example by retrieving all files that are prefixed with "images/":

[code:c#]

$storage->listBlobs('azuretest', '/', 'images/');

[/code]

Piece of cake!

I wanna play!

Sure, who doesn't? Here's a preview of the classes I've been creating: Zend_Azure_CTP.zip (11.51 kb)

Now let's hope my Zend Framework Proposal gets accepted so this can be a part of the Zend Framework. In the meantime, I'll continue with this and also implement Azure table storage: cheap, distributed database features in the cloud.

kick it on DotNetKicks.com

CRUD with ASP.NET MVC (screencast)

A new screencast has just been uploaded to the MSDN Belgium Chopsticks page. Don't forget to rate the video!

CRUD with ASP.NET MVC (screencast)

Abstract: "In this video, the new tooling for Visual Studio included in ASP.NET MVC release candidate is demonstrated to create a create, read, update and delete user interface within 15 minutes."

kick it on DotNetKicks.com