Copied to clipboard

Flag this post as spam?

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


  • Damien Holley 179 posts 540 karma points
    May 09, 2023 @ 02:07
    Damien Holley
    0

    Umbraco 10 - Need to create normal MVC controller + Route + views

    I am upgrading an older site and cannot get a custom mvc controller to work.

    This controller uses views that are not umbraco nodes (although I may want to grab some data from Umbraco content)

    The documentation merely goes through some methods to get data externally and put it on an Umbraco content node view.

    None of the examples of routing to the controller seem to work, all resulting in a 404 page.

    u.EndpointRouteBuilder.MapControllerRoute("testnew", "/TestNew/{action}/{id}", new { Controller = "Index", Action = "Index" });
    

    ^ does not fire a controller called TestNewController or the index method.

    What would be the best way of implementing this bearing in mind the views I would need to get are not umbraco views.

    Any help or a small example appreciated.

  • Marcio Goularte 374 posts 1346 karma points
    May 09, 2023 @ 13:22
    Marcio Goularte
    1

    I believe the error is here. You are setting the Controller as Index

    u.EndpointRouteBuilder.MapControllerRoute("testnew", "/TestNew/{action}/{id}", new { Controller = "Index", Action = "Index" });
    

    try this

    u.EndpointRouteBuilder.MapControllerRoute("testnew", "/TestNew/{action}/{id}", new { Controller = "TestNew", Action = "Index" });
    
  • Damien Holley 179 posts 540 karma points
    May 10, 2023 @ 01:06
    Damien Holley
    0

    sorry that was a copy in mid testing, I had already done as in your example, no dice.

    I have started using a surface controller (so the route is actually registered) for now but would still like to get a proper controller that is not using all of the Umbraco overhead.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    May 10, 2023 @ 08:00
    Marc Goodson
    0

    Hi Damien

    I have usually routed controllers through Umbraco as I've usually needed something from Umbraco on the model, and most examples are geared towards doing that - but what you are describing is having like an MVC app work alongside Umbraco - which in V7 was totally possible by just adding the custom route and using Umbraco.Core.ReservedPaths/ReservedUrls appsetting to tell Umbraco to leave well alone.

    I suspect in .net core this is a bit different, and I've not done it before so this is all a guess.

    It looks like it's still possible to specify ReservedPaths and ReservedUrls -

      "Umbraco": {
        "CMS": {
          "Global": {
            "Id": "",
            "SanitizeTinyMce": true,
            "ReservedPaths": "",
            "ReservedUrls": ""
          },
          "Content": {
            "AllowEditInvariantFromNonDefault": true,
            "ContentVersionCleanupPolicy": {
              "EnableCleanup": true
            }
          }
        }
    

    in appsettings.json

    And it 'looks like' it's possible to register an MVC route endpoint outside of Umbraco in startup.cs

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "supertest",
            pattern: "{controller=SuperTest}/{action=Index}/{id?}");
    });
    

    I'd put it below and outside the UseUmbraco stuff

    And then that controller can 'just' inherit from Controller - No Umbraco stuff.

    public class SuperTestController : Controller
    {
        public ActionResult Index()
        {
            string model = "hello";
            return View(model);
        }
    }
    

    and a request to /SuperTest/index should just work?

    When looking at the source, I did see this new setting called TryMatchingEndpointsForAllPages - which by default is set to false.

    https://docs.umbraco.com/umbraco-cms/reference/configuration/webroutingsettings#try-matching-endpoints-for-all-pages

    but if you are attempting to map the route to a controller completely outside of Umbraco, what I'd expect from my guess that if it routes correctly this setting won't come in to play.

    regards

    Marc

  • Damien Holley 179 posts 540 karma points
    May 16, 2023 @ 23:52
    Damien Holley
    0

    Yeah this process is what I tested, it's weird the docs say it should work, yet it doesn't, maybe I have to add the path to the reserved paths or something.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    May 18, 2023 @ 07:32
    Marc Goodson
    0

    Hi Damien

    From your screenshot, it looks like you have called

    u.EndpointRouteBuilder.MapControllerRoute
    

    Eg calling the extension method on 'u' which is within

    app.UseUmbraco().WithEndponts(u=>

    ...

    But my suggestion is to call it 'after' the Umbraco stuff, directly on app.

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "supertest",
            pattern: "{controller=SuperTest}/{action=Index}/{id?}");
    });
    

    It worked for me locally. (I didn't need to add as a reserved path or send the trymatchingendpoints setting)

    I was going to update the docs so that it reflected this subtlety - but are you saying you've tried this and it didn't work???

    regards

    marc

  • Damien Holley 179 posts 540 karma points
    Oct 28, 2023 @ 23:37
    Damien Holley
    0

    Yeah doing that did not work either, never found a solution that actually worked on that version, haven't tried doing this for any since.

Please Sign in or register to post replies

Write your reply to:

Draft