Copied to clipboard

Flag this post as spam?

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


  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Jun 01, 2022 @ 18:19
    Shaishav Karnani from digitallymedia.com
    0

    How to setup DefaultController in v9?

    Looking forward for a suggestion for a below problem. Thanks!

    I wanted to setup DefaultController in Umbraco v9 so that I donot need to create Controller for each DocumentType because we pass the ContentModel inside the mastermodel

    We created DefaultController and added override IActionResult Index

    But this code always show action as "Index". string template = ControllerContext.RouteData.Values["action"].ToString();

    In previous version, this action was name of the template. We wanted to get the name of the template when calling CurrentTemplate function.

            protected IActionResult CurrentTemplate<T>(T model)
        {
    
            string template = ControllerContext.RouteData.Values["action"].ToString();
            if (!EnsurePhsyicalViewExists(template))
            {
                //return HttpNotFound();
            }
            return View(template, model);
        }
    
    
        public class DefaultController : SurfaceRenderMvcController
    {
        private IPublishedValueFallback _publishedValueFallback;
        public DefaultController(IUmbracoContextAccessor umbracoContextAccessor,
            IUmbracoDatabaseFactory databaseFactory,
            ServiceContext services,
            AppCaches appCaches,
            IProfilingLogger profilingLogger,
            IPublishedUrlProvider publishedUrlProvider,
            ICompositeViewEngine compositeViewEngine,
            IPublishedValueFallback publishedValueFallback)
            : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider, compositeViewEngine, publishedValueFallback)
        {
    
            _publishedValueFallback = publishedValueFallback;
        }
    
        public override IActionResult Index(ContentModel model)
        {
            var masterModel = ModelLogic.CreateMasterModel(model.Content, _publishedValueFallback);
    
            return CurrentTemplate(masterModel);
        }
    }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 02, 2022 @ 06:03
    Dave Woestenborghs
    0

    Hi,

    I am not sure what you are trying to do. But this code works for me with a default controller.

    public class DefaultRenderController : RenderController {
    public DefaultRenderController(ILogger

    /// <inheritdoc/>
    public override IActionResult Index()
    {
        // custom code here
    
        return this.CurrentTemplate(this.CurrentPage);
    }        
    

    }

    This will render the default template for the doctype

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Jun 02, 2022 @ 06:52
    Shaishav Karnani from digitallymedia.com
    0

    Thanks - This works fine if I access from RenderControler but when I switch to IRenderControler then I am not getting access to CurrentTemplate. Any idea how can we use the CurrentTemplate function for IRenderControler.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 02, 2022 @ 06:59
    Dave Woestenborghs
    0

    Hi Shaishav,

    A route hijacked controller or a default controller should inherit from RenderController.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft