Introduction
In this article I will show how to create an ASP.NET MVC web application with a custom bootstrap theme / layout. For this demo I am using Visual Studio 2012, ASP.NET MVC 4, a custom bootstrap theme (Bootstrap Business Casual).
Background
Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web. It provides a default a layout, fonts and JavaScript widgets like accordion, carousel and many more. You can either take the default boostrap.css or you can use custom bootstrap themes available online to create your ASP.NET MVC Bootstrap Web application. In this article I will show how you can easily change the default ASP.NET MVC layout and apply custom bootstrap theme / layout to your MVC web application.
Getting Started
This tutorial is can be used for VS 2012 and VS 2013 and VS 2015.
For creating an ASP.NET MVC web application with custom bootstrap theme:
Step I – Download the bootstrap theme you intend to implement in you ASP.NET MVC application and keep it ready. (You may need to extract the files from zip folder in to a separate folder). For this demo I am using a custom bootstrap theme - Business casual, I have downloaded the theme from following website https://startbootstrap.com/template-overviews/business-casual/ .
Step II – Create an ASP.NET MVC web application, follow the steps mentioned below.
- Open VS 2012 / VS 2013 / VS 2015 and create new project select web – ASP.NET MVC 4 web application.
- Enter project name –“ASPNETMVC_BootstrapCustomThemeDemo”. Click Ok
- Select Internet template(VS 2012 / VS 2013) and click ok. Select ‘MVC’ for VS 2015
- The custom bootstrap I am about to implement has a Blog menu and a Blog.html page, so I have created a Blog.cshtml view and an Action for the same in HomeController, also added the blog menu in _layout.cshtml. (see Fig.3)
- After the project is created, just press F5 to run the application and in the web browser you would find the default ASP.NET MVC application template displayed.(see Fig.3)
Note: For VS 2015 there is a slight difference, Every ASP.NET MVC web application project comes with the default bootstrap theme installed (see Fig.1) and the ASP.NET MVC template is a Bootstrap template – jumbotron (see Fig.2). You would need to first uninstall current / default Bootstrap version and jQuery package using NuGet Package Manager and then follow from step III
Step III- To implement the Custom Bootstrap them in your ASP.NET MVC Web application.
- As we are going to implement the bootstrap CSS framework we do not need this Site.css, so the first thing you need to do is open the Site.css file and delete all the content from this file (See Fig.4), then save the file and close it, We would still need this file to override the bootstrap CCS elements.
- Next open the Bootstrap Custom theme folder (startbootstrap-business-casual-gh-pages), you would see few files and folders – you need to copy the CSS, Fonts and img folder to the project structure, under the Content folder (See Fig.5)
- Next open the js folder inside the Custom theme folder, copy all the JavaScript files from this folder to the Project structure, under Scripts folder (see Fig 6)
- Next got to Visual Studio and on the top of Solution explorer bar, Click on “Show All files” option to view the files and folders we copied to the project folders (See Fig. 7)
- Next, to make files and folders part of the project - Right click on each of them and select option “Include in Project” (See Fig. 8)
Step IV- To configure the ASP.NET MVC web application with the custom bootstrap theme:
Few points to note:
- _layout.cshtml would contain the common elements – lie the header (logo), Menu and footer- in case of dynamic components make sure to reference the jquery.js and boostrap.js file at the bottom of the page and if required some jQuery script.
- Make sure you are reference the image and the CSS links properly.
This completes the steps required for creating an ASP.NET MVC web application with a custom bootstrap theme.