HTML5 Day 2: Upgrade ASP.NET MVC 2 Site Template to HTML5

15. November 2010

Since HTML5 can be used in older browsers with some helper scripts, as covered in Day 1, I thought I’d use what I’ve learned to convert the default ASP.NET MVC 2 site template to HTML5.

Convert Default ASP.NET MVC Site Template to HTML5

First, add the helper scripts, mentioned in Day 1, to the Site.Master and convert all magical DIVs over to “header”, “section”, “nav” and “footer” tags.

Don’t worry about copy/paste, a download link for the full source code is at the bottom of the post.

Here’s the resulting Site.Master pages contents:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <!-- Include HTML5 fix for older browsers -->
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <!-- CSS to help older browsers render HTML5 - http://html5doctor.com/html-5-reset-stylesheet/ -->
    <link href="http://html5resetcss.googlecode.com/files/html5reset-1.6.1.css" rel="Stylesheet" type="text/css" />

    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="logindisplay">
        <% Html.RenderPartial("LogOnUserControl"); %>
    </div>

    <header>
        <h1>My MVC HTML5 Application</h1>
    </header>

    <nav>
        <ul>              
            <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
            <li><%: Html.ActionLink("About", "About", "Home")%></li>
        </ul>
    </nav>

    <section>
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </section>

    <footer>
        Copyright &copy; [Your Name Here]
    </footer>
</body>
</html>

Now the master page contains main “header”, “section”, “nav” and “footer” tags for the page.

The ContentPlaceHolder for rendering the Views is placed within a “<section>” tag. The reason for this is that now you only need to add a header and page content for the individual views. This compartmentalizes the page appropriately as per the design of the new elements being used.

Also, you’ll notice that I eliminated the <div class=”page”> as the CSS for it can be added to the <body> tag and the page will still render appropriately.

Now the hard part, styling the page using CSS. Well, actually this wasn’t too hard, since I mostly just changed the hard coded element id’s over to reference the new locations. For example: “#header” became “body header”

Below are the CSS styles that I changed (not the entire file):

<style>
/* PRIMARY LAYOUT ELEMENTS   
----------------------------------------------------------*/

/* you can specify a greater or lesser percentage for the 
page width. Or, you can specify an exact pixel width. */

body /* .page */
{
    width: 90%;
    margin-left: auto;
    margin-right: auto;
}

body header /* #header */
{
    position: relative;
    margin-bottom: 0px;
    color: #000;
    padding: 0;
    overflow: auto;
    float: left;
}

body header h1 /* #header h1 */
{
    font-weight: bold;
    padding: 5px 0;
    margin: 0;
    color: #fff;
    border: none;
    line-height: 2em;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 32px !important;
}

body section
{
    padding: 30px 30px 10px 30px;
    background-color: #fff;
    _height: 1px; /* only IE6 applies CSS properties starting with an underscore */
}

body footer /* #footer */
{
    color: #999;
    padding: 10px 0 25px 0;
    text-align: center;
    line-height: normal;
    margin: 0 0 30px 0;
    font-size: .9em;
    background-color: #fff;
}

/* TAB MENU   
----------------------------------------------------------*/
body nav ul /* ul#menu */
{
    border-bottom: 1px #5C87B2 solid;
    padding: 0 0 2px;
    position: relative;
    margin: 0;
    text-align: right;
}

body nav ul li
{
    display: inline;
    list-style: none;
}

body nav ul li#greeting
{
    padding: 10px 20px;
    font-weight: bold;
    text-decoration: none;
    line-height: 2.8em;
    color: #fff;
}

body nav ul li a
{
    padding: 10px 20px;
    font-weight: bold;
    text-decoration: none;
    line-height: 2.8em;
    background-color: #e8eef4;
    color: #034af3;
}

body nav ul li a:hover
{
    background-color: #fff;
    text-decoration: none;
}

body nav ul li a:active
{
    background-color: #a6e2a6;
    text-decoration: none;
}

body nav ul li.selected a
{
    background-color: #fff;
    color: #000;
}

/* MISC  
----------------------------------------------------------*/
body nav /* #menucontainer */
{
    clear: both;
    width: 100%;
}
</style>

Is HTML5 Fully Supported in ASP.NET MVC using this?

The short answer, “No.”

Using the above code, the page will render in older browsers. That is display correctly in most respects. If you want to use new HTML5 functionality, such as the new Input tags and validation, then you’ll need to do a bit of patch work using additional JavaScript and matching CSS.

What I like is that I have a target spec to shoot for (HTML5) with the patch work of JavaScript (including jQuery plugins) and CSS. As long as I program the patches to fit the standard HTML5 spec, then I can just remove the associated JavaScript and CSS in a couple years and the app will still function as expected. At least that would be the case in an ideal world.

MVC HTML5 Toolkit

The “MVC HTML5 Toolkit” project looks interesting, as its purpose is to bring HTML5 support to ASP.NET MVC.

Here’s a Getting Started Tutorial for the MVC HTML5 Toolkit where you can learn more about it.

Conclusion

I started building my own extension methods for ASP.NET MVC to add support for the new HTML5 tags in an effort to familiarize myself with them. It usually doesn’t make too much sense to duplicate effort, so I may submit some patches to the MVC HTML5 Toolkit in the future. That is of course if my “HTML5 + ASP.NET MVC” endeavors bring me to the point where I need/want more that the project has to offer.

However, I’ll probably spend more play time looking at (and working with/on) jQuery plugins that will help add the functional aspects of HTML5 tags to older browsers. (It feels somewhat odd calling the current generation of browsers “older browsers,” but after all they don’t fully support HTML5.)

Source Code:MvcHTML5.zip

HTML, HTML, HTML, HTML, ASP.NET MVC, ASP.NET MVC, ASP.NET MVC, ASP.NET MVC , , , , , , , , , , ,

Comments

11/16/2010 9:20:53 AM #
HTML5 Day 1: New tags work in older browser? Awesome!

HTML5 Day 1: New tags work in older browser? Awesome!
12/3/2010 6:27:36 PM #
Apple now has Rhapsody as an app, which is a great start, but it is currently hampered by the inability to store locally on your iPod, and has a dismal 64kbps bit rate. If this changes, then it will somewhat negate this advantage for the Zune, but the 10 songs per month will still be a big plus in Zune Pass' favor.
12/3/2010 7:05:51 PM #
i wish i could figure out how to create this type of incredible blog articles and get just as much audience and comments, my site has virtually no traffic.
12/3/2010 7:30:48 PM #
Hmm...You bring tons of righteous reasons.
I enjoy your thinkingand you haved got another weekly fan.

take it easy
12/11/2010 9:59:22 PM #
Found your blog within the yahoo <a href="...shirtswarehouse.com"><b>wholesale bulk t-shirts</b></a> directory, very nice job, thanks. Screen printing could further supply for all types of other substrates from plastic to metal. Even though tiny and complex details could be gathered, screen printing is preferably suitable for bold and graphic designs.
12/12/2010 3:00:30 PM #
I am expecting that our planned fishing trip to the Gulf is on the rocks for this season. Maybe <a href=”http://anglersfishery.com” target=”blank”>fresh water fishing</a> in Washington or North Carolina would be nice instead. Securing a good spot for stream fishing is my main concern.
12/12/2010 4:09:44 PM #
Thank you very much for this great article. The best wishes for you Smile
12/12/2010 4:40:05 PM #
Looking forward for more on this topic soon.
12/12/2010 6:11:41 PM #
I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz reply back as I'm looking to create my own blog and would like to know wheere u got this from. thanks
12/12/2010 6:11:54 PM #
I lived in Florida for 12 years. I miss the beach so much!
12/12/2010 6:31:40 PM #
I agree to the commenters good blog post a
12/12/2010 7:04:21 PM #
Geat post, thansk! I'am sure will be back here soon!
12/12/2010 8:58:13 PM #
Really appreciate the contributions here.

This post really has some intelligent points to ponder.

I'll be visiting again soon.
12/12/2010 11:28:11 PM #
That’s some pretty interesting ideas, but I have got a question for you. I have a Wordpress site, but my design is pretty boring. Yours looks really great though and I’m wondering if it’s a premium design? I am contemplating getting one, but I really like yours. I appreciate it.
12/13/2010 1:01:42 AM #
articles very useful, hopefully in the future to visit each other and commented ya fren. thank you
12/13/2010 1:10:33 AM #
hello, I haven't spoke to you on the blog in a while, but I thought I would email you from my cutting edge Android !! No fun, I work for a top company at this point in the fraud dep.  Never tell anyone but we located this site that is 100 % glitching and providing out free Android !!  to everybody that signs up. I presume you will probably have to confirm your email, Kind regards e zigarette
12/13/2010 3:51:06 AM #
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!
12/13/2010 4:19:43 AM #
Great articles you have here. I cannot agree more.
12/13/2010 5:01:05 AM #
I agree on the commenter above rocking post I must add
12/13/2010 5:08:11 AM #
Why didn?t I find this post earlier? Keep up the good work!  

Regards!


12/13/2010 6:29:52 AM #
lol a couple of the reviews bloggers write are silly and unrelated, there are times i wonder whether they at all read the post before writing or whether they just look at the subject of the blog post and compose the very first thought that drifts to their minds. But it is great to see a intelligent commentary every now and then in contrast to the exact same, traditional blog bull which I too many times notice on the blogs. Regards PokemonCraft Admin.