Copied to clipboard

Flag this post as spam?

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


  • Lennard Fonteijn 17 posts 73 karma points MVP 4x
    Apr 21, 2020 @ 10:13
    Lennard Fonteijn
    0

    Use-cases for PluginController

    So there are a bunch of different kind of base Controller classes in Umbraco, all of which have their purpose:

    • RenderMvc to hijack template routes
    • Surface to post forms, but can also be used for eg. a SiteMap route.
    • Api (and authorized counterparts) to make API's.

    But there is also PluginController (not the attribute), on which the SurfaceController is based.

    I was wondering if there are any use-case to ever directly extend PluginController?

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Apr 22, 2020 @ 08:29
    Marc Goodson
    100

    Hi Lennard

    PluginController is just a base Controller that other core Controllers inherit from in order to to help with MVC routing and for defining MVC areas using the [PluginController("areaName")] attribute.

    https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/Mvc/PluginController.cs

    Controllers that inherit from PluginController have a GetMetadata method that is used internally in Umbraco to track what kind of Controller the Controller is (backoffice etc) and how it is routed, eg AreaName.

    One of the benefits this facilitates is if you were building an Umbraco Package to share with other Umbraco users and it contained a SurfaceController perhaps with the name LoginController it would be highly likely it would clash with an existing controller in someone else's Umbraco site, or another package... by decorating your SurfaceController with the PluginController attribute you can ascribe a hopefully unique name that won't create a clash.

    [PluginController("superloginpackage")]
    public LoginController : SurfaceController
    

    Would you inherit directly from PluginController?

    yes, in the circumstance where you would not want your mvc controller to be auto-routed in the way that a SurfaceController is... eg map a custom mvc route to an MVC Controller without the context of a current request... but that is quite a niche circumstance... but useful to know about!

    This article from 'back in the day' explains a little about the emergence of that approach: https://shazwazza.com/post/custom-mvc-routing-in-umbraco/

    but these days you would more likely use Route Hijacking by Doc Type to handle an incoming route by convention inheriting from RenderMvcController or by custom route using a VirtualNodeRouteHandler to associate a custom IPublishedContent item with the request:

    https://our.umbraco.com/documentation/reference/routing/custom-routes

    regards

    Marc

  • Lennard Fonteijn 17 posts 73 karma points MVP 4x
    Apr 22, 2020 @ 09:18
    Lennard Fonteijn
    0

    Thanks Marc, was indeed suspecting it was very niche to do. Thanks for the link to Shannon's blog post!

Please Sign in or register to post replies

Write your reply to:

Draft