Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • aman 5 posts 75 karma points
    Oct 19, 2018 @ 15:58
    aman
    0

    Using generated models in controller

    I'd like to be able to reference models generated by the ModelsBuilder in my controller.

    When the ModelsBuilder is configured for LiveAppData, the models get generated and can be referenced from Views but seem to be invisible to controllers. I have the appropriate using statement in my controller.

    using Umbraco.Web.PublishedContentModels;

    I also have the generated models file included in my project:

    <Content Include="App_Code\Models\PaymentOptions.generated.cs" />

    I tried LiveDll mode and once I add the dll reference, the generated model is available in the controller.

    I'd really like to be able to use AppData models over Dll models. Is it possible to reference AppData models in the controller?

    I hope this is just an IDE configuration issue. Maybe Visual Studio isn't including the generated files in its compile plan?

    Anyone have any experience with this? Thanks

    Edit: Figured it out. Seems by default, Visual Studio doesn't mark the generated files for compile. You can set this property by right clicking on the files in Solution Explorer, and setting the Build Action as "Compile".

    Edit 2: Spoke too soon. Marking those files for compile creates a conflict because Umbraco also compiles those files. I end up with two "Published Content Models" describing the same content type.

  • John Bergman 483 posts 1132 karma points
    Oct 20, 2018 @ 00:49
    John Bergman
    0

    If your controllers are in a separate project, you will need to either use the LiveDLL option, or generate the source into a directory in another c# project.

    Otherwise your web project references your controller project which then cannot reference your web project because of circular references.

  • aman 5 posts 75 karma points
    May 01, 2019 @ 00:24
    aman
    0

    If anyone stumbles upon this in the future, I got this working by specifying the output directory for models builder to some path other than the default App_Code (ex: ~/Models/Umbraco). You can find this setting in your project's main web.config:

    <add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/Models/Umbraco" />

    I then included the files in the project so that my custom Surface and API controllers could recognize them. I can also reference them now in strongly typed views and get full intellisense.

    The reason why it didn't work in App_Code is because it's a special .NET folder in the sense that classes that exist in that folder are available to the application at runtime. If you mark them as 'compile' during your build, they'll get picked up once at compile and again at runtime, and cause conflicts.

Please Sign in or register to post replies

Write your reply to:

Draft