Book Image

Bootstrap for ASP.NET MVC

By : Pieter van der Westhuizen
Book Image

Bootstrap for ASP.NET MVC

By: Pieter van der Westhuizen

Overview of this book

<p>Bootstrap, a leading open source frontend framework, takes care of typography, form layouts, and user interface components, allowing a developer to focus on writing code. Integrating ASP.NET's powerful components can further enhance its capabilities. This book guides you through the process of creating an ASP.NET MVC website from scratch using Bootstrap.</p> <p>You will learn about various Bootstrap components as well as techniques to include them in your own projects. The book includes practical examples to show you how to use open source plugins with Bootstrap and ASP.NET MVC and will guide you through building a website using Bootstrap, utilizing layout and user interface components. In the process, you will also learn to build HTML helpers and T4 templates as well as how to use the jQuery DataTables plugin. At the end of this book, you will find some valuable tips and tricks, which will help you in getting the most out of your Bootstrap and ASP.NET MVC integrated website.</p>
Table of Contents (18 chapters)
Bootstrap for ASP.NET MVC
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the site Layout file


To maintain a persistent look across our site's pages, we'll use a Layout file. This layout file will use the basic Bootstrap HTML template at first and we'll build onto it as we progress throughout the book. To create this file, complete the following steps:

  1. Create a new Layout file by right-clicking on the Views folder in the Visual Studio's Solution Explorer and navigate to Add | New Folder. Name the new folder Shared.

  2. Next, right-click on the Shared folder and navigate to Add | New Item…. Select the MVC 5 Layout Page (Razor) item template, name the new item _Layout.cshtml and click on Add, as shown in the following screenshot:

  3. After the new file is added to you project, open it and replace its contents with the following HTML markup:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>@ViewBag.Title</title>
        <!-- Bootstrap -->
        <link href="@Url.Content("~/css/bootstrap.css")" rel="stylesheet">
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
          <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        @RenderBody()
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="@Url.Content("~/js/bootstrap.js")"></script>
    </body>
    </html>

    In the preceding markup, we set the viewport's width property to the device's width and the initial-scale value to 1. This will cause our site to adapt to the screen size of the device the user is viewing it from.

    Next, we reference the Bootstrap style sheet by using the Url.Content helper method. This helper method converts a virtual or relative path to an absolute path, making sure that when the web page is opened, the style sheet will be loaded correctly.

    We then check if the browser accessing the site is Internet Explorer 9 or earlier; if it is, we include the HTML5Shiv workaround that enables styling of HTML5 elements in Internet Explorer Version 9 and earlier. We also include the version of the Respond JS library suitable for versions of IE9 and earlier.

    Just before the closing the <body> tag of the file, we include the jQuery library and the bootstrap JavaScript library.

    Tip

    Note that the HTML5Shiv workaround, Respond JS, and jQuery files are loaded from a Content Delivery Network (CDN). This is a good approach to use when referencing to most of the widely used JavaScript libraries. This should allow your site to load faster if the user has already visited a site, which uses the same library from the same CDN, because the library will be cached in their browser.