Copied to clipboard

Flag this post as spam?

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


  • Vyacheslav 13 posts 113 karma points
    Jun 19, 2023 @ 09:50
    Vyacheslav
    0

    Umbraco Controller. Routing. Why I have 404 error for all actions?

    I have custom controller like this:

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using HttpGetAttribute = System.Web.Mvc.HttpGetAttribute;
    using RouteAttribute = System.Web.Mvc.RouteAttribute;
    using RoutePrefixAttribute = System.Web.Mvc.RoutePrefixAttribute;
    
    namespace MyProject.Host.Controllers
    {
        [RoutePrefix("partnerClient")]
        public class PartnerClientController : UmbracoController
        {
            [HttpGet]
            [Route("firstClient")]
            public ActionResult FirstClient()
            {
                return View(@"~/Views/Partner/FirstPartnerClientView.cshtml");
            }
        }
    }
    


    I am trying to call action {hostname}/partnerClient/firstClient
    But I got an error:
    Page not found
    No umbraco document matches the URL '/partnerClient/firstClient'.

    But when I use UmbracoApiController routing is ok

    using Umbraco.Web.WebApi;
    using HttpGetAttribute = System.Web.Http.HttpGetAttribute;
    using RouteAttribute = System.Web.Http.RouteAttribute;
    using RoutePrefixAttribute = System.Web.Http.RoutePrefixAttribute;
    
    namespace MyProject.Host.Controllers
    {
        [RoutePrefix("partnerClient")]
        public class PartnerClientController : UmbracoApiController
        {
            [HttpGet]
            [Route("firstClient")]
            public object FirstClient()
            {
                return "{test}";
            }
        }
    }
    


    I have to return View for this action and UmbracoApiController doesn't allow it.
    How I should configure routing for UmbracoController? Or maybe I should use some other way to return some View?

  • Steffen Hornbæk Nielsen 56 posts 168 karma points
    Jun 19, 2023 @ 10:44
    Steffen Hornbæk Nielsen
    0

    Hi Vyacheslav,

    You can try and use a full path instead like this:

    [Route("umbraco/backoffice/partnerClient/firstClient")]
    

    Then you should be able to call it from [hostname]/umbraco/backoffice/partnerClient/firstClient

    If you still have issues, you might want to read the documentation for "Attribute routing with IVirtualPageController" here: https://docs.umbraco.com/umbraco-cms/reference/routing/custom-routes#custom-routes-within-the-umbraco-pipeline

  • 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