Click here to Skip to main content
Click here to Skip to main content

Convert a MVC4 Application to a "Cool Towel" Single Page Application (SPA)

, 26 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Example project template for a "Cool Towel" SPA to be filled with a MVC4 application HTML5 and JavaScript code in order to convert
Screenshot running out-of-the-box Cool Towel SPA project template

Introduction 

This tip is about making website navigation just as fast as a desktop application and with the same look and feel, for example in an ASP.NET MVC4 internet application by converting it to a "Cool Towel" Single Page Application (SPA). This is done by putting the HTML5 code of each internet application page in the appropriate places of the SPA project template, also incorporating in this solution the corresponding JavaScript and controller code. A Cool Towel: You do not go to the SPA without one!

Background

A SPA, also known as single-page interface (SPI), is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application. In a SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load. The page does not reload at any point in the process, nor does control transfer to another page.

Using the Code

Open the SPA_Project_Template_01 solution in Visual Studio 2012 or later. Copy / paste the HTML5 code of your pages in the "XXXX HTML5 Here" placeholders in the Index.cshtml, add the JavaScript files, add the CSS files and controller files also. Be sure that all the JavaScript and CSS files are referenced in the Index.cshtml

Out-of-the-box the navigation buttons have the click event Knockout data binded (HTML5 code):

<div class="float-right">
    <nav>
        <ul id="menu1">
            <li>
                <input type="button" value="Home" data-bind="click: $root.divHome" /></li>
            <li>
                <input type="button" value="Manual" data-bind="click: $root.divManual" /></li>
            <li>
                <input type="button" value="Blog" data-bind="click: $root.divBlog" /></li>
            <li>
                <input type="button" value="Balance" data-bind="click: $root.divBalance" /></li>
            <li>
                <input type="button" value="Budget" data-bind="click: $root.divBudget" /></li>
            <li>
                <input type="button" value="About" data-bind="click: $root.divAbout" /></li>
            <li>
                <input type="button" value="Contact" data-bind="click: $root.divContact" /></li>
        </ul>
    </nav>
</div>

Out-of-the-box these click events trigger JQuery opening on collapsing of the placeholder <div>s, like (JavaScript code):

function appViewModel() {

    var self = this;

    self.divHome = function () {
        $("#divHome").show();
        $("#divManual").hide();
        $("#divBlog").hide();
        $("#divBalance").hide();
        $("#divBudget").hide();
        $("#divAbout").hide();
        $("#divContact").hide();
        $("#divTerms").hide();
        $("#divPrivacy").hide();
    }

Points of Interest

Navigation of the budget.satoconor.com demo website that took about 2 seconds according to the PageSpeed Chrome pluggin now is like instantaneous in a view microseconds. Moreover the bootstrap time of the entire SPA Index.cshtml, when application pool and website IIS are alive, has only doubled, from about 2 to 4 seconds, compared to loading the HOME page only of the MVC4 Internet Application.

History

  • 25th January, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Johan G. van der Galien
Web Developer SATOCONOR
Netherlands Netherlands
I am working with .NET Web Technology since 2005, I can make any website as long as it is Microsoft based.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionMicrosoft: Change for the sake of change PinprofessionalJohan G. van der Galien29-Jan-14 18:07 
QuestionMeant for simple websites, for the youth: The new generation developers PinprofessionalJohan G. van der Galien29-Jan-14 6:43 
Question[My vote of one] Total Fail Pinmemberalec.whittington27-Jan-14 5:58 
Question[My vote of 1] mmmm not sure PinmvpSacha Barber26-Jan-14 22:45 
AnswerRe: [My vote of 1] mmmm not sure PinprofessionalJohan G. van der Galien29-Jan-14 6:17 
AnswerRe: [My vote of 1] mmmm not sure PinprofessionalJohan G. van der Galien29-Jan-14 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 26 Jan 2014
Article Copyright 2014 by Johan G. van der Galien
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid