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.
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:
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().
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.
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.
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:
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
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().
is working on a reply...