Geeks With Blogs
Joe Mayo

I’ve heard people mention, a few times, how they would like for Microsoft to provide a blank template for ASP.NET MVC.  The request seems strange because this is something that anyone can do themselves.  This isn’t just creating an MVC project and deleting all the items, which I would agree is tedious. Rather, you can create a custom template of any type for reuse and sharing.  This blog post shows how to create a custom project template, using the need for a blank MVC template as the use case.

Creating a Base Project

To get started, open Visual Studio.  Press Ctrl+Shift+N to create a new project. Then create a new ASP.NET MVC project. The figure below shows how I selected Visual C#/Web from Installed Templates and then selected the ASP.NET MVC 4 Application Template from the list of project types. The name and location are insignificant because this project is an intermediate artifact and can be deleted after the process.

image

 

 

 

 

 

 

 

Clicking the OK button, you’ll see another page, allowing you to select the project template type.  Here, select Empty Template, shown here:

image

 

 

 

 

 

 

 

 

 

 

Here, you might be wondering what this blog post is about if Microsoft already offers a template called “Empty Template”. However, when you click the OK button, you’ll find out that the template isn’t completely empty.  You’ll see what I mean by following the steps below on the resulting project.

1. Open the Properties branch and delete AssemblyInfo.cs. This file will be regenerated when you open Properties, click the Application tab, click the Assembly Info button and save new settings. Therefore, you don’t absolutely need it in the template.

2. Open the References folder and you’ll notice many assembly references: some you’ll need and others you might not need.  For example, you might want to delete the EntityFramework reference if you use other data access libraries on occasion. You can always add it to a project later, via NuGet.  If you look at the file system for the Web app, you’ll see a NuGet packages folder showing how many extra references are being created.  If you have NuGet installed, you can execute the following command to remove the package, and it’s associated reference:

> uninstall-package EntityFramework

You can do the same for each of the other packages (and associated references) you want to remove.  Otherwise, just delete the reference and associated package folder.

3. Delete the App_Data folder. If you’re like me, your database is already built separately, is never deployed with the application itself, and is rarely and Express version of SQL Server.

4. Content has a default style sheet and jQuery UI themes that you can delete. On any given project, I might use Syncfusion or Telerik components instead of jQuery UI, so having them there isn’t necessary.  Additionally, you might have your own CSS Reset file that you prefer instead of the default Site.css.

5. Controllers and Models is already empty, so nothing to do there.

6. The scripts folder contains many *.js files that can be removed.  Again, you can remove these via NuGet, as explained in Step #2 above.  Otherwise, just delete them.  Removing via NuGet will take a few minutes because you’ll receive dependency errors and need to uninstall dependencies (identified in the error message) first.

Tip: If you don’t know the exact package name, just type the first few characters and press Tab, with either fills out the name or shows a list of options.

7. You can remove all the files under Views and Views/Shared, but this might become a little extreme.  For example you might need to re-create the Web.config and _ViewStart.cshtml anyway, so the utility of blowing these away might actually be more cumbersome when trying to start a new project.  That said, deleting _Layout.cshtml and Error.cshtml is probably fine.  If you remove _Layout, you might also remove _ViewStart as it’s default implementation points at _Layout.

8. You can delete Global.asax.  it assumes you want areas and global filter registration, which you might or might not care about. Recreating the initial routing isn’t too hard.  Alternatively, you can leave Global.asax in-place (because you’ll need it anyway) and delete the parts of the code, such as area registration that you might not need.

9. If you’ve removed all of your packages, you can delete packages.config.

10. You can delete Web.config and its transform files in their entirety, but there’s another option that will keep you from needing to re-add important items. Open web.config and delete the following elements:

a. Delete connectionStrings as it only facilitates writing to a default DB, which you already removed the App_Data folder for.

b. Remove appSettings elements for items you don’t think you’ll need.

c. Your compilation element items are probably important, so you might not want to touch them.  Besides, the transform for the Release profile removes the debug option on deployment.

d. Remove the authentication element as it assumes you want forms authentication and refers to a non-existent logon page.

e. The pages element identifies namespaces that you’ll probably want, so you might not want to remove any of them.

f. Remove profile, membership, and rolemanager as they assume you’re using the built-in ASP.NET services.

g. Delete session as you almost never want to run an app in InProc mode.

h.  If you know what you’re doing, you can remove system.webserver.  Otherwise, you can leave it alone.

i. The runtime settings contain binding redirects so that code trying to reference older MVC components will use newer MVC components instead. Having them there generally doesn’t hurt and can help you avoid havoc when you need the binding redirects and don’t have them.  You might want to leave them there.

That’s the last part of Web.config and the last file to delete from the project. Now you can create a custom template based on the choices just made.

Generating the Template

To start the new template creation process, select File –> New Template.  You’ll see the first page of the Export Template Wizard, Choose Template Type, shown below:

image

 

 

 

 

 

 

 

 

 

Make sure Project template is selected and click Next.  You’ll see the Select Template Options page, shown below.  The Template name will be part of the output file name for the template as well as the project name shown in the New Project window. BlankMvcProject, was selected from the current project name, but you can give it any name you want.  Template description, Icon image, and Preview image are what shows up in the New Project template.  The template file will be written to the Output location.  Selecting Automatically import the template into Visual Studio will make the template available immediately.  Display an explorer window on the output files folder will do exactly what it says.

image

 

 

 

 

 

 

 

 

 

Click Finish and observe that an explorer window pops up in the Visual Studio 2010/My Exported Templates folder with a *.zip file named BlankMvcProject.zip, which contains all the files needed for Visual Studio to offer the template a new project type.  Now, you can share and use this template with anyone you like and they can have the same project type.

Using the Template

To share the template, copy the template file (BlankMvcProject) to the Visual Studio 2010\Templates\ProjectTemplates\ folder, under your My Documents folder.  You won’t need to do this on your machine if you checked Automatically import the template into Visual Studio while creating the template.  However, for everyone else that needs the template, they must add the template to the Visual Studio 2010\Templates\ProjectTemplates\ folder, as just stated.  With the template installed, you can create a new project.

To create a new project, based on your new template, open Visual Studio (if not already open) and press Ctrl+Shift+N. Select Visual C# and scroll down the project list until you see the project type named BlankMvcProject. Select BlankMvcProject and click OK.

Final Thoughts

Now you know how to create a bare-bones ASP.NET MVC project. While the discussion focused primarily on removing files, this is an excellent technique for creating custom projects where you might want to either modify existing files or add features that you do use often. Additionally, this technique works for any project type, not just ASP.NET MVC, as it’s a feature of Visual Studio.

Posted on Sunday, December 18, 2011 9:42 PM ASP.NET MVC | Back to top

Copyright © Joe Mayo | Powered by: GeeksWithBlogs.net