How to add C# code to a new Orchard CMS theme

2019-08-02 04:34发布

问题:

Introduce the Problem

I would like to implement the Orchard IShapeTableProvider, in order to add an alternate for the main List shape. I have done the following:

Download Orchard.Source.1.8.zip, build in VS 2013, and create a new theme via code generation:

  orchard.exe
  feature enable Orchard.CodeGeneration
  codegen theme My.FirstTheme
  theme enable My.FirstTheme

Add the following code to my theme in the Themes > My.FirstTheme > Code > ListShapeProvider.cs file.

namespace Themes.My.FirstTheme.Code
{
    using Orchard.Autoroute.Models;
    using Orchard.ContentManagement;
    using Orchard.DisplayManagement.Descriptors;
    public class ListShapeProvider : IShapeTableProvider
    {
        public void Discover(ShapeTableBuilder builder)
        {
            System.Diagnostics.Debugger.Break(); // * not hit
        }
    }
}

Clean and build the Orchard solution, then debug Orchard.Web. The debugger does not hit System.Diagnostics.Debugger.Break().

Search and Research

I have read Orchard shapeshifting by Orchard's Benevolent Dictator.

Further, I have installed, built, and run the Contoso theme as an example implementation of IShapeTableProvider. The implementation of IShapeTableProvider is in the ContentShapeProvider.cs file. The Contoso theme also includes a Contoso.csproj file that has the ContentShapeProvider.cs file as a Compile Item.

<ItemGroup>
   <Compile Include="Code\ContentShapeProvider.cs" />
 </ItemGroup>

The new theme that I created does not have a .csproj file. I think the missing .csproj file explains why my ListShapeProvider.cs breakpoint is not executing.

Should I create a .csproj file for my theme? Is there another recommended way to include C# in the theme? For instance, should I use an App_Code folder?

回答1:

In the Orchard.exe code generation, add /CreateProject:true. This will generate a .csproj file for you. E.g.

orchard.exe
feature enable Orchard.CodeGeneration
codegen theme My.FirstTheme /CreateProject:true
theme enable My.FirstTheme

Now when you add a ListShapeProvider.cs file, Visual Studio will automatically as the following entry to the My.FirstTheme.csproj file.

<ItemGroup>
  <Compile Include="ListShapeProvider.cs" />
</ItemGroup>


回答2:

You can add /IncludeInsolution:true too in your codegen theme My.FirstTheme /CreateProject:true
It will add your theme project under the Themes top level folder.