Copied to clipboard

Flag this post as spam?

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


  • andrew shearer 409 posts 517 karma points
    Jul 16, 2019 @ 07:39
    andrew shearer
    0

    Umbraco 8 and calling MapMvcAttributeRoutes

    hi

    What is the best practice way of calling MapMvcAttributeRoutes in an Umbraco 8 application? In umbraco 7 I called it from an ApplicationEventHandler method (ApplicationStarted) but that's replaced with Composers/Components in umb8 I believe. I created a component but now get an error about a missing service for my custom route?

    thanks for the help

  • Sibren 28 posts 170 karma points
    Jul 16, 2019 @ 07:46
    Sibren
    0

    Could you provide some code and what error you're getting?

    Or you could create a starting class which inherits from UmbracoDefaultOwinStartup, if all else fails.

  • andrew shearer 409 posts 517 karma points
    Jul 16, 2019 @ 19:54
    andrew shearer
    0

    thanks Sibren. The exception is

    Unable to resolve type: MyWebsite.WidgetsController, service name:
    

    where the controller is

        [RoutePrefix("widgets")]
    public class WidgetsController : Controller
    {
        [ChildActionOnly]
        [Route("show")]
        public ActionResult Show()
        {
            return PartialView();
        }
    }
    

    and the view has

    @Html.Action("Show", "Widgets")
    

    Yes using Owin could be one option. Essentially I just want to register (the non umbraco) mvc routes in my application, so figured i need to call MapMvcAttributeRoutes somehow?

    The Umbraco component is:

        public class RegisterCustomRouteComponent : IComponent
    {
        public void Initialize()
        {
            RouteTable.Routes.MapMvcAttributeRoutes();
        }
    
        public void Terminate()
        {
            throw new NotImplementedException();
        }
    }
    
  • andrew shearer 409 posts 517 karma points
    Jul 17, 2019 @ 20:16
    andrew shearer
    0

    update: following another thread i found, i made my controller inherit from RenderMvcController so that umbraco "knows" about the controller/"service".

  • Sibren 28 posts 170 karma points
    Jul 24, 2019 @ 05:13
    Sibren
    0

    Did this work? Because RouteTable.Routes.MapMvcAttributeRoutes(); does not what you're describing here.

    It does map the routes you're using in your controller though. I am reading up on https://our.umbraco.com/documentation/reference/routing/custom-routes where it states you need to do some other stuff. Did you read that? I am trying the same thing but with an API, but am also struggling.

    I'll keep you informed!

  • andrew shearer 409 posts 517 karma points
    Jul 24, 2019 @ 21:38
    andrew shearer
    0

    Hi - yes it worked. So at this point I have a Component that calls "RouteTable.Routes.MapMvcAttributeRoutes();" and also a custom controller that inherits from RenderMvcController.

    This was the only way i could register the routes for my custom controller methods even though the methods aren't concerning umbraco content. I figure it makes sense that this is how an "UmbracoApplication" would know about all of the registered routes (umbraco or not). Ideally i wouldn't need to use RenderMvcController if there was some other/simpler way

  • andrew shearer 409 posts 517 karma points
    Jul 24, 2019 @ 21:41
    andrew shearer
    0

    basically, my original question was how do i get an UmbracoApplication to honour the stock standard mvc route attribute on my custom controller method:

            [Route("/my/custom/method")]
        public ActionResult MyMethod()
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies