Search code examples
c#asp.net-mvcvisual-studioiis-expresssidewaffle

How can I create a custom ASP.NET MVC project template?


Background: I am attempting to create a new C# project template for ASP.NET MVC using Visual Studio 2013 Ultimate Update 5. To start, I create an Web application from the empty template and include MVC references. Then, I add all of the basic required components for the application such as the model, controller, and views. Next, I run it, and it works. Then, I export it as a project template.

In the past, I have successfully created templates for Web Api and Console Apps using the above method.

Problem Description: When I attempt to use the template for a new project, I get the following error:

Debugging Not Enabled Image

According to both the error message and Microsoft, I should be able to resolve this by placing the following code within my Web.config file:

  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.1"/>
  </system.web>

However, this is already present, and the dialogue box persists.

If I try to run without debugging, I get the following error in the browser:

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

Attempts to solve:

  1. Deleted both the Web.config and web.config files and imported them from existing solutions that work.
  2. Selected "Add a new Web.config file with debugging enabled," which overwrites the existing Web.config and web.config file. Although Visual Studio is able to overwrite Web.config, it fails to overwrite web.config ("The operation could not be completed. Not implemented" appears in a dialogue box).
  3. Added the above system.web tag to web.config.
  4. Ensured that the Asp.Net debugger is enabled in property settings
  5. Cleaned and manually removed existing .dlls prior to running
  6. Uninstalled and reinstalled ASP.NET MVC with Nuget

Update:

Rather than using Export Template within Visual Studio, I have installed TemplateBuilder and SideWaffle and have followed the linked video instructions for constructing a custom project template. However, I get the following error when trying to create a new project from the template:

enter image description here

This error has been an open issue within github since April, so it appears I am out of luck.

Second Update: Root Cause Analysis

I have determined the root cause for why projects based on the exported template fail. Comparing the references of the base and derived projects reveals the following:

Derived:

<Reference Include="System.Web.Razor">
  <HintPath>..\..\..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.Razor.dll</HintPath>
</Reference>

Base:

<Reference Include="System.Web.Razor">
  <HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>

Once the HintPaths in the derived project are reset to those of the base, the derived project runs successfully (No "Debugging Not Enabled" dialogue box).

The question becomes: "How can I ensure the HintPaths of the derived project are set to the appropriate values automatically?"


Solution

  • I was able to create a working MVC template using the following methods:

    In the base project:

    In the csproj file, the references to the MVC related dlls are in the following form:

    <Reference Include="System.Web.Razor">
      <HintPath>\\BILL\Users\Popper\Desktop\Stackoverflow\UNCPathNonSpecificPrivateReferences\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    

    In particular, the UNC path to the dll prevents HintPath from being overwritten.

    In the derived project:

    After saving and building, I close and reopen the project.