Heya Florian,
Id need a bit more info to diagnose this specific error - but generally that exception means kentico has the deafult ComponentDefaultViewModel<HomePage>
and expects your view to have @model ComponentDefaultViewModel<HomePage>
, but your view instead has a @model DashboardViewModel
There are a few ways you couldve set this up, but for this case I would reccommend doing the is logged in/not logged in using a controller eg HomePageController
for the page template and registering it with
[assembly: RegisterPageRoute(HomePage.CLASS_NAME, typeof(HomePageController))]
This links any urls that resolve to the home page in the content tree to the Index() method of your HomePageController.
From there you can check if there is a logged in user, and Redirect to the Dashboard url.
Best pratice here is to not use static urls in the BE - so that the content tree is fully functional - so in this case HomePageController.Index() could look something like this;
[assembly: RegisterPageRoute(HomePage.CLASS_NAME, typeof(HomePageController))]
public class HomePageController
{
//Used to get the current page context
private readonly IPageDataContextRetriever pageDataContextRetriever;
//Used to convert a TreeNode instance into its url in the content tree
private readonly IPageUrlRetriever pageUrlRetriever;
//Standard way to inject the httpcontext into the controller
private readonly IHttpContextAccessor httpContextAccessor;
//Used to make a query to the Kentico Tree db to retrieve a TreeNode (in our case a Dashboard page)
private readonly IPageRetriever pageRetriever;
public HomePageController(IPageDataContextRetriever pageDataContextRetriever, IPageUrlRetriever pageUrlRetriever, IHttpContextAccessor httpContextAccessor, IPageRetriever pageRetriever){
this.pageDataContextRetriever = pageDataContextRetriever;
this.pageUrlRetriever = pageUrlRetriever;
this.httpContextAccessor = httpContextAccessor;
this.pageRetriever = pageRetriever;
}
[HttpGet]
public IActionResult Index(){
// Check the request is actually for the page type we expect
if (pageDataContextRetriever.TryRetrieve<HomePage>(out var data)
&& data?.Page is HomePage currentPage)
{
if (httpContextAccessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false)
{
//User is logged in -> redirect to the Dashboard page type
// We need to find the Dashboard page TreeNode we want to redirect to
// Im presuming theres just a single Dashboard page on the site here.
var dashboardTreeNode = pageRetriever.Retrieve<Dashboard>().FirstOrDefault();
// Then ask kentico what the url it has is
var dashboardURL = mPageUrlRetriever.Retrieve(dashboardTreeNode).AbsoluteUrl;
// Then use the standard redirect
return Redirect(dashboardURL);
}
else
{
return View(new HomePageViewModel(currentPage)); //return to the home page view
}
}
}
}