---
description: >-
  * Controllers are completely optional, but recommended to make the most out of
  the CMS
---

# Controllers (Route Hi-Jacking)

If you want to have your own Model passed to a View it works exactly like a normal MVC app. We can create a controller, and a custom View Model and pass these to the View. You just have to adhere to naming conventions.

### Naming Conventions

The naming convention is pretty simple. The controller name, must match your 'Content Type' alias, and the ActionResult name must match the View you are wanting to use. Also, all controllers need to inherit from&#x20;

```csharp
ZauberRenderController
```

Example below shows the Home Content Type Controller from the starter site.

```csharp
/public class HomeController(ILogger<HomeController> logger, IOptions<ZauberSettings> options, IMediator mediator) 
    : ZauberRenderController(logger, options, mediator)
{
    private readonly IMediator _mediator = mediator;

    /// <summary>
    /// Route hijacked controller, 'Home' ContentType and 'Home' View 
    /// </summary>
    /// <returns></returns>
    public async Task<IActionResult> Home()
    {
        var homeViewModel = new HomeViewModel(CurrentPage!)
        {
            // Get the header image
            HeaderImage = await CurrentPage!.GetMedia("HeaderImage", _mediator, "/assets/img/home-bg.jpg")
        };

        // Get the blog posts
        var posts = await _mediator.Send(new QueryContentCommand
        {
            AmountPerPage = 4,
            ContentTypeAlias = "BlogPage",
            OrderBy = GetContentsOrderBy.DateUpdatedDescending,
            Cached = true
        });

        homeViewModel.BlogPosts = posts.Items.ToList();
        
        return CurrentView(homeViewModel);
    }
}
```

As you can see, we have a custom view model called HomeViewModel and this is passed to the View using **CurrentView()** which is also important.

Using the View name as the ActionResult, means you can have different ActionResults for each View, so for differently layouts you could (Optionally) pass in&#x20;

### View Location

By default the Views are searched for in the Views folder route, but if you want to keep the normal MVC Views convention you can. So for the Home Controller, you can also put your views in /Views/Home/ if that's how you like to organise your views.

### Custom View Models

Custom View Models must inherit from&#x20;

```csharp
ZauberPageViewModel
```

The example below, shows the full view model for the Home View Model used above in the controller.

```csharp
public class HomeViewModel(Content content) : ZauberPageViewModel(content)
{
    public Media? HeaderImage { get; set; }
    public List<Content> BlogPosts { get; set; } = [];
}
```

### CurrentPage

CurrentPage gets the Content model for the current page you are on.
