Copied to clipboard

Flag this post as spam?

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


  • Jon 92 posts 166 karma points
    Feb 18, 2021 @ 15:51
    Jon
    0

    Redirect from Surface Controller to different URL

    Hi,

    Is it possible to redirect to a different url - for example a Thank you page from a surface controller. Im using the following but the Thank you page is inside the Original page - I need it to redirect to a new URL.

    I have used the following:

     [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitPersonalDetailsForm(PersonalDetails model)
        {
    
    return new RedirectResult(ViewDelegateURL, true);
    return this.RedirectToUmbracoPage(ViewDelegateId);
    

    Any ideas what I need to do to get this working?

  • Joep 96 posts 698 karma points
    Feb 18, 2021 @ 15:54
    Joep
    0

    Hi Jon,

    Can't you just use return Redirect(ViewDelegateURL);?

    -Joep

  • Jon 92 posts 166 karma points
    Feb 18, 2021 @ 15:59
    Jon
    0

    I must be doing something wrong - it doesnt actually redirect - but puts the new page inside the old page

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Feb 18, 2021 @ 16:25
    Huw Reddick
    0

    you should definitely just be able to do

    return Redirect("/some-url");

    or

    return RedirectToUmbracoPage(umbracoPageId);

  • Jon 92 posts 166 karma points
    Feb 18, 2021 @ 17:18
    Jon
    0

    Would this stop it from working?

    My Front end partial form is using this:

    @using (Ajax.BeginForm("SubmitPersonalDetailsForm", "PersonalDetailsSurface", new AjaxOptions() {

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Feb 18, 2021 @ 20:44
    Huw Reddick
    1

    In your controller, return JSON

    return Json(new { url = yoururl });

    Add an onsuccess function and assign it in your ajaxform options.

    var onSuccess = function(result) {
    if (result.url) {
        // if the server returned a JSON object containing an url 
        // property we redirect the browser to that url
        window.location.href = result.url;
    }
    

    }

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Feb 18, 2021 @ 17:46
    Huw Reddick
    0

    Yes, you can't redirect from an Ajax post, you need to return the URL and redirect in the onsuccess function

  • Nurhak Kaya 56 posts 150 karma points MVP 3x c-trib
    15 days ago
    Nurhak Kaya
    0

    For my Umbraco v13+ website, below is how I managed to do my redirects. In older versions of Umbraco, maybe "Redirect" or "RedirectToUmbracoPage" could work but not for v13+ websites as far as I see.

    Razor view:

    @inject IUmbracoContentHelperService _umbracoContentHelperService
    @inject IGoogleAnalyticsHelperService _googleAnalyticsHelperService
    @inherits UmbracoViewPage<ResetPasswordFormViewModel>
    
    @model ResetPasswordFormViewModel
    @{
        var portalPageUrl = _umbracoContentHelperService.GetPortalEditAccountPage()?.Url() ?? "/";
        var passwordUpdateSuccessful = TempData["ResetPasswordIsSuccessful"] as bool? == true;
        var displayCurrentPasswordIncorrectMessage = passwordUpdateSuccessful == false && TempData["DisplayCurrentPasswordIncorrectMessage"] as bool? == true;
    }
    
    <div>
        @if (passwordUpdateSuccessful)
        {
            <p>Your password has been successfully changed.</p>
            <p><a href="@portalPageUrl" class="btn btn-pink">Back</a></p>
    
    
    
    <script>
                try {
                    window.dataLayer.push(@(Html.Raw(_googleAnalyticsHelperService.GetPortalAnalytics("reset password"))))
                } catch (e) {
                    console.log("Failed to push data to GA.");
                }
            </script>
    
    
        }
        else if (displayCurrentPasswordIncorrectMessage)
        {
            <p>Your current password is not correct.</p>
            <p><a href="@portalPageUrl" class="btn btn-pink">Back</a></p>
        }
        else
        {
            using (Html.BeginUmbracoForm<ResetPasswordController>("Reset", FormMethod.Post))
            {
                @*Starting from Umbraco 9 the __RequestVerificationToken token is automatically added to forms for use, so we no longer need to add @Html.AntiForgeryToken() to our forms.*@
                @*For details: https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers#protecting-surface-controller-routes*@
    
                @Html.HiddenFor(m => m.Token)
    
                <div class="profile-preferences-form__field">
                    @Html.LabelFor(x => x.CurrentPassword)
                    <div class="w-100">
                        @Html.TextBoxFor(x => x.CurrentPassword, new { type = "password", maxlength = 64 })
                        @Html.ValidationMessageFor(x => x.CurrentPassword)
                    </div>
                </div>
                <div class="profile-preferences-form__field">
                    @Html.LabelFor(x => x.Password)
                    <div class="w-100">
                        @Html.TextBoxFor(x => x.Password, new { type = "password", maxlength = 64 })
                        @Html.ValidationMessageFor(x => x.Password)
                    </div>
                </div>
                <div class="profile-preferences-form__field">
                    @Html.LabelFor(x => x.ConfirmPassword)
                    <div class="w-100">
                        @Html.TextBoxFor(x => x.ConfirmPassword, new { type = "password", maxlength = 64 })
                        @Html.ValidationMessageFor(x => x.ConfirmPassword)
                    </div>
                </div>
                <p>@RegexConstants.PasswordRulesUserNotice</p>
    
                <input type="submit" class="btn btn-pink" title="Submit" value="Submit" />
                <a href="@portalPageUrl" class="btn btn-default ml-4">Back</a>
            }
        }
    </div>
    

    Here is the surface controller:

        public class ResetPasswordController : SurfaceController
        {
    
            // Removed code for brevity
    
            [ValidateAntiForgeryToken]
            [HttpPost]
            public async Task<IActionResult> Reset(ResetPasswordFormViewModel model)
            {
    
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
    
                if (await _memberManager.ValidateCredentialsAsync(model.Email, model.CurrentPassword))
                {
                   // Removed code for brevity
    
                    // Set TempData to true to display the confirmation message
                    TempData["ResetPasswordIsSuccessful"] = true;
                }
                else
                {
    
                   // Removed code for brevity
                   // Current password is not correct
    
                    // Set TempData to true to display the error message
                    TempData["ResetPasswordIsSuccessful"] = false;
                    TempData["DisplayCurrentPasswordIncorrectMessage"] = true;
                }
    
                return RedirectToCurrentUmbracoPage();
            }
     }
    

    Related links: https://docs.umbraco.com/umbraco-cms/13.latest/reference/routing/surface-controllers/surface-controllers-actions

    https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers#protecting-surface-controller-routes

  • 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