3

I have a couple of MVC4 projects, and I'd like to make a set of display / editor templates that are reused across these projects. I'd like to create a common project that contains these templates, and reference it in the other projects.

So I have the views defined in my common project, along with some other utilities:

Common
- Models
  - CommonMetadataAttribute.cs
- Views
  - Shared
    - EditorTemplates
      - Decimal.cshtml
      - Object.cshtml
      - Password.cshtml
      - String.cshtml
      - Switch.cshtml
    - ViewTemplates
      - Object.cshtml
      - Switch.cshtml
    - Error.cstml

And in my client project's models I'd have

using MobileWeb.Common

public class MyViewModel
{
    [UIHint("Switch")]
    [CommonMetadata(Theme = "foo")]
    public bool Enable { get; set; }
}

And in the client projects views I'd be able to generate my custom editors by simply calling:

@model MyViewModel
...
@Html.EditorFor(model => model.Enable)

Ideally, the client projects could override with their own templates, and it would just fall back to the common templates if no project-specific one was defined.

I've considered MvcContrib's portable areas, but I'm not sure if it will work since this is not really an area and I don't really know how to go about setting it up. Before I go down that road I'd like to know what is the preferred way of doing this?

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331

1 Answers1

3

Personally I have always been packaging templates as custom NuGets. You put your custom templates in a NuGet and when the user installs this NuGet in his project they are automatically added to the correct folders of his application. And automagically all the EditorFor/DisplayFor calls throughout the application start using your custom templates instead of the default ones.

This way you don't need to be writing some custom VirtualPathProviders (which break as soon as you precompile your application) in order to be retrieving Razor views embedded in third party assemblies and so on... Another advantage of using NuGets is that the user has your templates in his application and could easily modify them if he is not pleased with your work. And if he is really angry with you, all that he needs is to uninstall your NuGet in order to rollback his application to the default state.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • NuGet packages are awesome and easy to create, deploy, install, uninstall. Another option would be to export your project as VS template `(File -> Export Template)` and re-use it in new solutions. – dan radu Feb 05 '13 at 21:44
  • Yes, they are trivially easy to create, deploy, install and uninstall. They are also extremely powerful as allow you to run PowerShell scripts in order to perform complete customization of the project in whcih your are installing the NuGet. – Darin Dimitrov Feb 05 '13 at 21:46
  • 1
    That worked out pretty well. This wasn't specified in the link or in your answer, but it appears you need to specify a build action of 'Content' on any files you wish to include in your package. – p.s.w.g Feb 05 '13 at 22:57