Create an empty ASP.NET Web Application (.NET Framework)
in Visual Studio. Then run this following NuGet command:
PM> Install-Package UmbracoCms
Some of the requirements has changed for Umbraco v8, so make sure you development environment is up to speed otherwise you'll get stuck before even getting started.
Most importantly you'll need:
- Visual Studio Code or Microsoft Visual Studio 2017 version 15.9.6+
- ASP.NET Framework 4.7.2
Umbraco v8 now ships with Dependency Injection out of the box, so you do not need to install any packages to start injecting your own services. You do however need to read a bit on a new concept called Composing. Luckily, there are some great documentation available at Our, plus I found this blogpost very helpful.
Here is an example of how easy it is to register your own custom service in Umbraco v8 by creating an IUserComposer
:
public class MyCustomServiceComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<ICustomService, MyCustomService>(Lifetime.Scope);
}
}
I prefer to build my view models using a package called UmbracoMapper.
PM> Install-Package UmbracoMapper
The reason for this updated post not being available sooner is because I've been waiting for a v8 compatible version of UmbracoMapper. So I was really happy when I found this blogpost announcing that a v8 version was available.
To be able to inject UmbracoMapper in my controllers I need to register it using another IUserComposer
:
public class UmbracoMapperComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IUmbracoMapper, UmbracoMapper>(Lifetime.Scope);
}
}
I prefer to route my views using my own controllers by implementing something called Route Hijacking. This is done by having my controllers inheriting a class called RenderMvcController
. What I like to do is create a base controller for all my page controllers, that inherits RenderMvcController and takes in a generic view model, and then maps the current requested content to that view model using UmbracoMapper and then returns that view model to my view.
public abstract class BasePageController<TModel> : RenderMvcController where TModel : class, new()
{
private readonly IUmbracoMapper umbracoMapper;
protected BasePageController(IUmbracoMapper umbracoMapper)
{
this.umbracoMapper = umbracoMapper;
}
public override ActionResult Index(ContentModel model)
{
return View(this.MapModel(model.Content));
}
public virtual TModel MapModel(IPublishedContent content)
{
var model = new TModel();
this.umbracoMapper.Map(content, model);
return model;
}
}
Worth mentioning here is that the Index method no longer takes in a RenderModel
, but instead now it takes in a ContentModel
. So here is an example of how my StandardPageController
now looks like:
public class StandardPageController : BasePageController<StandardPageViewModel>
{
public StandardPageController(IUmbracoMapper umbracoMapper) : base(umbracoMapper) { }
public override ActionResult Index(ContentModel model)
{
// TODO: Put custom magic code here please..
return base.Index(model);
}
}
As mentioned before, everyone builds Umbraco differently and there is no right or wrong way. This is a v8 updated version of how I usually prefer to build Umbraco for v7, but it should be said that I have yet not release a v8 site using this strategy, so there might be some updates to this post along the way. Next up on my todo is updating my Skrift article to a v8 compatible version. Stay tuned. :)
Have a great summer everyone, and take care of each other! ❤
Thanks currently putting this in practice for a new site, so will update you on how i get on.