Copied to clipboard

Flag this post as spam?

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


  • Joe Begly 7 posts 77 karma points
    Oct 07, 2020 @ 18:11
    Joe Begly
    0

    Custom Controllers (Route Hijacking) in Umbraco 8.7 Async/Await not working

    Hi,

    I've started a new site that's using Umbraco 8.7.0 and I'm struggling with route hijacking as defined here:

    https://our.umbraco.com/DOCUMENTATION/Reference/Routing/custom-controllers

    I've done this numerous times in older versions, like 8.1, but for some reason it's not working for me in 8.7.0. I'm not sure if I'm missing something obvious or if you don't use this method anymore. I've pretty much copied/pasted something I had in the 8.1 project and just changed all the namespaces and classnames to suit my situation. I have a doc type named My Account (alias is myAccount) and I've created a controller like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Models;
    using Umbraco.Web.Mvc;
    
    namespace explorer.web.Controllers
    {
        public class MyAccountController : RenderMvcController
        {
            // GET: MyAccount
            public async Task<ActionResult> Index(Umbraco.Web.Models.ContentModel model)
            {
                return View(model);
            }
        }
    }
    

    When I view a page of the My Account doctype (using its default template), it never hits the breakpoint I set in my custom controller. Now, interestingly, I noticed if I remove the async and Task, and add override to it like this:

    public override ActionResult Index(Umbraco.Web.Models.ContentModel model)
    {
        return View(model);
    }
    

    it hits the breakpoint just fine. The trouble is, I'm going to need aysnc/await support and as I mentioned I've done this before in older version of Umbraco 8.

    I know if this won't work I can just code something up in a macro and get/post to a surface controller. Async/await seems to work in that implementation, but I'm trying to avoid macros except places where it would be a reusable component. I'll only ever have one "My Account" page in this project though so a template with a custom controller seems the way to go.

    Can anyone help me? Thanks!

    EDIT: I mentioned using the async/await approach in a surface controller as an alternative...I looked through my project again and noticed I'm doing async/awain in an UmbracoApiController, not a SurfaceController.

    EDIT 2: On a whim I upgraded to 8.8 and now my async/await RenderMvcController is just fine. Maybe it's a 8.7 bug that I ran into or something...leaving the rest of this post in tact in case it helps someone else that has this happen to them. Sorry if I wasted anyone's time who may have been looking into a solution for me!

  • Dominic Rinaldi 1 post 71 karma points
    Jan 15, 2021 @ 10:45
    Dominic Rinaldi
    0

    Hi, we've run into the same issue on version 8.10 - Do you have an example of an async/await RenderMvcController that works? Thanks :)

  • Joe Begly 7 posts 77 karma points
    Jan 22, 2021 @ 15:20
    Joe Begly
    0

    Hi Dominic,

    I am now on 8.10.1 and this is still working just fine for me. Here's a condensed version of RenderMVCController I'm using

    public class MyAccountController : RenderMvcController
    {
        public MyAccountController()
        {
    
        }
    
        // GET: MyAccount
        public async Task<ActionResult> Index(Umbraco.Web.Models.ContentModel model)
        {
            //add any async/await operations here
            return View(model);
        }
    }
    

    I have a docType with the alias 'myAccount'.

    Keep in mind the other requirement is a page made from this docType with a template that expects a model of the same type

    enter image description here

    In /Views/MyAccount.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<explorer.core.Models.MyAccount>
    

    I should also note I've used the ModelsBuilder API to generate strongly typed classes of my docTypes. I don't think that is required but it does keep the code more clean since you can directly specify property names. Here is the tutorial I used to set that up in case you are interested and/or unfamiliar https://www.youtube.com/watch?v=XPGV1vwQ0HU

    Since I've done that in a project in my solution named "explorer.core", that is the reason the namespace of my model is in the view template is explorer.core.Models.MyAccount

Please Sign in or register to post replies

Write your reply to:

Draft