Copied to clipboard

Flag this post as spam?

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


  • Steve Morgan 1346 posts 4453 karma points c-trib
    May 20, 2021 @ 17:19
    Steve Morgan
    3

    Not a Q - My Adventures in V9 that might help others(?)

    Here are my adventures in V9 so far - hopefully these will help someone else. As will quickly become apparent I've not got enough experience with .NET Core to know if what I'm doing here is the correct way or just me desperately thrashing around trying to make something work in the way I "know"!

    Any feedback greatly appreciated. If this is useful I'll knock it into an article / documentation.

    I want to fully recreate a v8 project in v9.

    Project Setup

    Here is my setup that I want to recreate:

    • VS2019 solution
    • Website sits in one project MySolutionName.Core project (business logic this in retrospect is a confusing name!) with models, controllers etc
    • MySolutionName.Frontend project that is just used to gulp build my frontend (I just run gulp and that minifies a css and js file and copies to the website dir)

    First I Installed Umbraco using the template as per the blog post https://umbraco.com/blog/umbraco-9-beta-release/ but with 002 version

    Created a .Net Core empty class library for .Core (all my additional code will go here remember)

    ModelsBuilder

    I wanted the models builder configured to use AppData so I can strongly type my models and use them in my controllers / services etc. I did this in stages to see what's happening, first just try AppData mode.

    In appsettings.json added under the Umbraco node:

      "ModelsBuilder": {
        "ModelsMode": "AppData"
        //"ModelsDirectory": "~/../../UmbracoV9Beta/Umbraco.Cms.Web.Common.PublishedModels",
        //"AcceptUnsafeModelsDirectory":  true
      }
    

    When I first did this I hit on my homepage:

      The type or namespace name 'Homepage' does not exist in the namespace 'Umbraco.Cms.Web.Common.PublishedModels' (are you missing an assembly reference?)
    

    This writes the models out to the /umbraco/models folder (after you run the models builder in the Settings).

    This is because these are not included properly in the solution and build - you need to set the build action on each file as a c# compiler (there must be a better way?)

    But doing this gets rid of the error. Once I was happy that was working as I usually have these models in a separate solution it was time to reconfigure and get these output to my project...

    Created a .netcore class library project titled Umbraco.Cms.Web.Common.PublishedModels

    Modified the appsettings.json (UmbracoV9Beta should be swapped out with your solution dir)

      "ModelsBuilder": {
        "ModelsMode": "AppData"
        "ModelsDirectory": "~/../../UmbracoV9Beta/Umbraco.Cms.Web.Common.PublishedModels",
        "AcceptUnsafeModelsDirectory":  true
      }
    

    Added a project reference from the website project -> Umbraco.Cms.Web.Common.PublishedModels project

    Now on build you'll get

    Error   CS0234  The type or namespace name 'Core' does not exist in the namespace 'Umbraco.Cms' (are you missing an assembly reference?)    Umbraco.Cms.Web.Common.PublishedModels  C:\Users\steve\source\repos\UmbracoV9Beta\Umbraco.Cms.Web.Common.PublishedModels\Article.generated.cs   13  Active
    

    In the old world we'd add the nuget package UmbracoCms.Core to the PublishedModels project... Wasn't sure what to do here so I went to nuget and changed the package source to the Umbraco Prereleases (top right dropdown) and ensured I checked the Include Prerelease checkbox.

    Then search for Umbraco.Cms.Infrastructure and installed that in my Umbraco.Cms.Web.Common.PublishedModels project. (Thanks to Benjamin Carleski for this tip)

    Huzzah - we build successfully again! And we can build out the models builder and rebuild.

    Then I did the same with the business logic project and then we can do some actual site dev... same deal add the Umbraco.Cms.Infrastructure package and also add Umbraco.Web.Website package. Add this as a project reference to your website.

    I created three folders. - Controllers - Models - ViewComponents

    ViewCompontents because I quickly realised my usual method of creating a partial that calls a child action on a controller to do things like render the Main nav and meta tags wasn't the .net core way of doing things.

    So a crash course in ViewComponents later - I have a folder in my business logic project that has the class that powers the ViewComponent partial view that is found in the very specific location: /Views/Shared/Components/Meta

    So create in the business logic project: MetaViewComponent.cs

      namespace UmbracoV9Beta.ViewComponents
    {
    // https://www.davepaquette.com/archive/2016/01/02/goodbye-child-actions-hello-view-components.aspx
    public class MetaViewComponent : ViewComponent
    {
        // no DI services yet...
        public MetaViewComponent()
        {
        }
    
        public IViewComponentResult Invoke(int numberOfItems)
        {
            string metaView = "Hello from the view component";
            return View("RenderMeta", metaView);
        }
    

    Then /Views/Shared/Components/Meta/RenderMeta.cshtml

    @model string
    
    <h2>What's New</h2>
    <h3>@Model</h3>
    

    Then in your partial view / view... @await Component.InvokeAsync("Meta", new { numberOfItems = 3 })

    Question - does this have to be async?!

    Now you should have a simple View Compontent that returns a hello world string. Not very interesting - we need services and code to do things.

    So now I need to inject my services into this View Component. I copied in a simple site settings service and then added it and the interface to the ConfigureServices Method in the Startup.cs file.

        services.AddScoped<ISiteSettingsService, SiteSettingsService>();
    

    After a few intellisense hint fixes my solution builds - I can inject my site settings service into the ViewComponent and start to build the site...

    As I said the above is a bit of blurb - but if you're picking through like me it might help you.

    Steve

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    May 20, 2021 @ 18:58
    Marc Goodson
    1

    Hi Steve

    Thanks for sharing!

    One thing, I noticed on the issue tracker: https://github.com/umbraco/Umbraco-CMS/pull/10250

    which should resolve your modelsbuilder query.,, as in next beta they should be generated as 'c# compiler'!

    regards

    Marc

  • David Armitage 505 posts 2073 karma points
    Jun 06, 2021 @ 09:30
    David Armitage
    1

    Hey All,

    I have started writing a blog series of how to get up and running with Umbraco 9.

    I am just learning how to do things and documenting at the same time. I plan to continue with these every weekend for the next few months up until an official release.

    https://www.umbrajobs.com/blog/categories/umbraco-9-getting-up-and-running/

    Keep checking back.

    Also if you want to be address a specific topic let me know and I will try and figure it out and document about it.

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft