Copied to clipboard

Flag this post as spam?

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


  • npack 42 posts 288 karma points
    Nov 21, 2023 @ 21:39
    npack
    0

    MVC Based Backoffice Section - Routing Newb

    Hi, trying to add an MVC based section to the backoffice that will call an external API. Not sure if I'm setting this up correctly. I'm using Umbraco 12.

    I added a section following instructions on https://docs.umbraco.com/umbraco-cms/extending/section-trees/sections

    Now when I click on a tree node I end up at this url, which expectedly results in a 404. https://localhost:44365/umbraco#/blogs/blogeditor/edit/2

    Then I tried creating a controller now to catch those requests. I put it in App_Plugins/Blogs but I'm still getting a 404 and struggling to know what to try next. Any ideas?

         [Area("blogs")]
        public class BlogsController : UmbracoAuthorizedController { 
    
         [HttpGet]
         public IActionResult Index()
         {
             return Content($"Hello world - index");
         }
    
         [HttpGet]
         public IActionResult Edit([FromRoute] int id)
         {
             return Content($"Hello world - blog {id}");
         }
    

    Then in my startup.cs file I tried the following:

    services.Configure[UmbracoPipelineOptions](c =>
    {
        // Custom Blogs Backoffice Editor
        c.AddFilter(new UmbracoPipelineFilter(nameof(BlogsController))
        {
            Endpoints = app => app.UseEndpoints(endpoints =>
            {
                var globalSettings = app.ApplicationServices.GetRequiredService[IOptions[GlobalSettings]]().Value;
                var hostingEnvironment = app.ApplicationServices.GetRequiredService[Umbraco.Cms.Core.Hosting.IHostingEnvironment]();
                var backofficeArea = Constants.Web.Mvc.BackOfficePathSegment;
    
                var rootSegment = $"{globalSettings.GetUmbracoMvcArea(hostingEnvironment)}";
                endpoints.MapUmbracoRoute[BlogsController]($"{rootSegment}/{backofficeArea}", "blogs", "blogeditor", "Index", true);
            })
        });
    });
    

    Had to swap some less than and greater thans for square brackets in this forum post

  • npack 42 posts 288 karma points
    Nov 22, 2023 @ 00:00
    npack
    0

    Maybe what I'm trying to do is too much a mishmash of server and client. I just found this github issue that sounds exactly like what I'm describing.

    https://github.com/umbraco/Umbraco-CMS/issues/8438

    Perhaps supporting this conclusion is that when I visit https://localhost:44365/umbraco/blogs I finally seem to be hitting my controller but I get no menu.

    https://localhost:44365/umbraco#/blogs however gives me a section with the header. I am not an angular developer but apparently that pound sign is important.

    I don't really want to spend $3000 per year to use the new UI Builder so I'll keep struggling on through an MVC solution.

  • Bo Jacobsen 604 posts 2402 karma points
    Nov 22, 2023 @ 12:27
    Bo Jacobsen
    100

    Like Sebastiaan write in the https://github.com/umbraco/Umbraco-CMS/issues/8438 there are no way around using an angular controller and a html view (as fare as he knows).

    So when the url hit https://localhost:44365/umbraco#/blogs/blogeditor/edit/2 it is looking for a html file in ~/App_Plugins/{yourPluginName}/backoffice/{treeAlias}/edit.html

    In the edit.html file, you can then wrap an angular controller that use custom services to call your UmbracoAuthorizedController and return the data needed.

    Good luck.

  • npack 42 posts 288 karma points
    Nov 22, 2023 @ 19:03
    npack
    0

    Thanks, Bo.

    I ended up creating that view as you suggested, but putting an Iframe on it pointing to https://localhost:44365/umbraco/blogs, then created a controller there that could return an MVC view

    [Area("blogs")]
    [Authorize(Policy = AuthorizationPolicies.BackOfficeAccess, Roles = "admin,editor")]
    public class BlogsController : Controller
    {
    
    
        [HttpGet]
        public IActionResult Index()
        {
            var model = new BlogsModel();
            model.Blogs.Add("President's Blog");
    
            return View("~/App_Plugins/blogs/Views/Index.cshtml", model);
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft