Copied to clipboard

Flag this post as spam?

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


  • Ole Andreas Myhre Olsen 15 posts 85 karma points
    Aug 08, 2024 @ 23:24
    Ole Andreas Myhre Olsen
    0

    SurfaceController does not post to defined action

    I have a surface controller like this:

        using Microsoft.AspNetCore.Mvc;
    using Umbraco.Cms.Core.Cache;
    using Umbraco.Cms.Core.Logging;
    using Umbraco.Cms.Core.Routing;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Cms.Infrastructure.Persistence;
    using Umbraco.Cms.Web.Common.Filters;
    using Umbraco.Cms.Web.Website.Controllers;
    
    namespace MyControllers
    {
        public class TestController : SurfaceController
            {
                public TestController(
                IUmbracoContextAccessor umbracoContextAccessor,
                IUmbracoDatabaseFactory databaseFactory,
                ServiceContext services,
                AppCaches appCaches,
                IProfilingLogger profilingLogger,
                IPublishedUrlProvider publishedUrlProvider)
                : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
                {
                }
    
                [HttpPost]
                [ValidateUmbracoFormRouteString]
                public IActionResult HandleSubmit()
                {
                    return RedirectToCurrentUmbracoPage();
                }
    
                [HttpGet]
                public IActionResult Index()
                {
                    return View("FormTest");
                }
    
                [HttpPost]
                public IActionResult HandleTest()
                {
                    return RedirectToCurrentUmbracoPage();
                }
            }
        }
    

    and my view is like this:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage 
    @using Microsoft.AspNetCore.Http.Extensions
    @using Umbraco.Cms.Web.Common.Models
    @using Umbraco.Cms.Web.Website.Controllers
    @using Umbraco.Extensions
    @using MyControllers
    
    <div class="login-status">
    
        @using (Html.BeginUmbracoForm<TestController>("HandleTest"))
        {
            <input type="submit" value="Test" />
        }
    
    </div>
    

    However, when I click "Test" I get 404 instead of redirecting. What am I doing wrong?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 09, 2024 @ 10:10
    Huw Reddick
    0

    Is it hitting the HandleTestMethod ?

    If you remove [ValidateUmbracoFormRouteString] does it make any difference? (I don't think you need this as it is for Umbraco Forms)

  • Ole Andreas Myhre Olsen 15 posts 85 karma points
    Aug 09, 2024 @ 13:01
    Ole Andreas Myhre Olsen
    0

    Hi Huw

    No, it's not hitting HandleTest :(

    If I create another Index action for [HttpPost] it will hit that. Is surface controllers supposed to post to the controller and then call the desired action?

  • Ole Andreas Myhre Olsen 15 posts 85 karma points
    Aug 09, 2024 @ 13:03
    Ole Andreas Myhre Olsen
    0

    This is from https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers#routing-for-plugin-based-controllers

    Whenever you render an Umbraco form within your view using
    Html.BeginUmbracoForm<MyController>(...), the forms action will be the
    URL of the current page (not the auto-routed URL of the surface
    controller). Umbraco will therefore add a hidden ufprt field to the
    form with an encrypted value containing the controller, action and
    optional area (known as the 'Umbraco form route string'). On form
    submission, this value is decrypted and Umbraco will activate the
    specified action of the surface controller.
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 09, 2024 @ 15:19
    Huw Reddick
    0

    What is the exact version of Umbraco you are using? The route decoding is done automatically, I've not had any problems posting to Surface Controllers

  • Ole Andreas Myhre Olsen 15 posts 85 karma points
    Aug 11, 2024 @ 10:11
    Ole Andreas Myhre Olsen
    0

    I'm using Umbraco 14

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 11, 2024 @ 11:34
    Huw Reddick
    0

    I just tested your code in my v14.2.0-rc site and it works fine, HandleTest gets called and redirects back to the currentpage as expected.

  • Ole Andreas Myhre Olsen 15 posts 85 karma points
    Aug 11, 2024 @ 19:55
    Ole Andreas Myhre Olsen
    0

    I think there is something else I do wrong. I made a new site and tried to add the surfacecontroller and it worked as expected.

    Thanks for you help anyway :)

Please Sign in or register to post replies

Write your reply to:

Draft