Copied to clipboard

Flag this post as spam?

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


  • Souvik 9 posts 89 karma points notactivated
    Sep 08, 2023 @ 17:41
    Souvik
    0

    returning GetCurrentUmbracoPage() inside an ActionMethod from a controller?

    Hello, I have an Action method called IActionResult HandleLogin which is returning GetCurrentUmbracoPage(). But I get this error whenever the method is hit.

    An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object. Umbraco.Cms.Web.Website.ActionResults.UmbracoPageResult.ExecuteControllerAction(IActionInvoker actionInvoker)

    This return path is only there incase some validations are wrong and the currentpage needs to be refreshed with the validations showing.

    I am new to Umbraco so am I missing anything here? Thank you.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 09, 2023 @ 14:58
    Marc Goodson
    1

    Hi Souvik

    You might need to post your controller / action to see exactly where this is going awry but

    is your HandleLogin inside a controller that inherits from SurfaceController?

    If so then there is a specific helper method you can return that will direct the User back to the current Umbraco page but keep Modelstate intact, eg showing all the validation errors! It's called CurrentUmbracoPage() example:

         [HttpPost]
            public IActionResult HandleLogin()
            {
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
    // if all is valid - do some stuff - eg send an email, update database etc
                return RedirectToCurrentUmbracoPage();
            }
    

    The 'RedirectToCurrentUmbracoPage takes the user to the current page but wipes the modelstate, so it's likely if you are on a special login page that you don't want to return to the login page but instead redirect somewhere else...

    In which case you can use the other RedirectTo helpers listed here:

    https://docs.umbraco.com/umbraco-cms/reference/routing/surface-controllers/surface-controllers-actions#redirecttocurrentumbracourl

    to go to a specific page etc

    regards

    marc

  • Souvik 9 posts 89 karma points notactivated
    Sep 17, 2023 @ 07:00
    Souvik
    0

    Hi Marc, Thanks for the reply. I fixed the issue. I was making the mistake of passing all the parameters of the Surface controller inside the constructor as private readonly which gave me an error whenever I tried to call the CurrentUmbracoPage().

Please Sign in or register to post replies

Write your reply to:

Draft