# Custom Controller using View()

**URL:** <https://forum.litium.com/t/custom-controller-using-view/156>\
**Category:** Questions\
**Created:** [July 5, 2018, 11:31am UTC](https://forum.litium.com/t/custom-controller-using-view/156 "2018-07-05T11:31:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [July 5, 2018, 11:31am UTC](https://forum.litium.com/t/custom-controller-using-view/156/1 "2018-07-05T11:31:29Z")

</div>

Hi, this is most probably my c# skill lacking and not directly related to Litium, but I’m trying to create a custom controller.

I create the controller:  
Litium.Accelerator.Mvc/Controllers/TestController.cs  
And the views:  
Litium.Accelerator.Mvc/Views/Test/Index.cshtml  
Litium.Accelerator.Mvc/Views/Shared/\_ViewTest.cshtml

From the testController I try call return View() this returns an error about  
context.CurrentStae.PageType.Name beeing null from the ContentProvider.cs

So am thinking I need to create a new “base view” so I create it and put it in the Shared view folder, then run: View(“Index”,“ViewTest”), but i still get the same error.

The only thing that I manage to get working is PartialView(), but that is not really what I want 🙂

Any suggestions?

Litium version: 6

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [July 5, 2018, 11:53am UTC](https://forum.litium.com/t/custom-controller-using-view/156/2 "2018-07-05T11:53:41Z")

</div>

You should be working with pagetypes and templates, see this article for more information:

[https://docs.litium.com/more/training/start-with-litium/pagetypes-and-templates](https://docs.litium.com/more/training/start-with-litium/pagetypes-and-templates)

---

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [July 5, 2018, 12:12pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/3 "2018-07-05T12:12:41Z")

</div>

This is how I do it when I want to create a new pageType inside Litium (For CMS pages etc).

This time I just want to create a completely custom controller and view that has nothing to do with the adminpanel, no admin model or data should be used.

Is this not possible using view? Is partialView my only option then?

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [July 5, 2018, 12:30pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/4 "2018-07-05T12:30:57Z")

</div>

> Standard [ASP.NET](http://ASP.NET) MVC will have a route registered for the controller, when using Litium you got the hierarchic url to reflect the page structure in the site together with the product structure.

\_from [https://docs.litium.com/documentation/architecture/mvc\_](https://docs.litium.com/documentation/architecture/mvc_)

However the accelerator adds another route in `Litium.Accelerator.Mvc.RouteConfig`:

```auto
// Register direct access for controllers
RouteTable.Routes.MapRoute(
   "ControllerDirect", // Route name
   "site.axd/{controller}/{action}/{id}", // URL with parameters
   new {action = "Index", id = UrlParameter.Optional} // Parameter defaults
).DataTokens["SkipAutoLayout"] = true;

```

So you should be able to access your controller on \_[http://domain/site.axd/test\_](http://domain/site.axd/test_) and if you need to you can define some other route for access.

---

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [July 5, 2018, 12:40pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/5 "2018-07-05T12:40:21Z")

</div>

Thank you for your patience, I don’t think that I’m explaining myself very well. I can access the controller with no problem.

[url.com/api/test/](http://url.com/api/test/) and this render just fine to the browser if i return a manual string or a partialView from the controller.  
I just want to know if it is possible to use the View(‘template’,‘master’) logic somehow 😕  
Thanks!

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [July 5, 2018, 1:00pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/6 "2018-07-05T13:00:37Z")

</div>

So is the problem that you are unable to use a layout with your custom controller?

I tried to reproduce the problem but for me it works (if I understand the problem), I have created a testcontroller for which i can define a layout (see attached image).

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/4ef2d78bf6bbf633524d2b39f769ca996bbac7b4.png)

---

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [July 5, 2018, 1:34pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/7 "2018-07-05T13:34:10Z")

</div>

Yes we seam to understan each other now! So your test is with the Litium.Accelerator.Mvc.RouteConfig config from before?

Else it seams exactly like my setup: except I try to use the RoutePrefix insted of RouteConfig :  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/daf705678235449f8228047c989c32e5c7432d60.png)

Is this the problem, going via RoutePrefix will demand a Litium pageType being defined??

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [July 5, 2018, 1:38pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/8 "2018-07-05T13:38:19Z")

</div>

In the route that is registered in the accelerator we are adding an extra data token with name `SkipAutoLayout` (see last row).

> [@marten](#):
>
> ```auto
> // Register direct access for controllers
> RouteTable.Routes.MapRoute(
> "ControllerDirect", // Route name
> "site.axd/{controller}/{action}/{id}", // URL with parameters
> new {action = "Index", id = UrlParameter.Optional} // Parameter defaults
> ).DataTokens["SkipAutoLayout"] = true;
> 
> ```

In the `_ViewStart.cshtml` we are checking if this data token have been set, if the data token not have been set we are automatic setting the `Layout` for the view depend on the settings in accelerator.

From `_ViewStart.cshtml`

```auto
@using Litium
@using Litium.Accelerator.ContentProviders
@{
    if (ViewContext.RouteData.DataTokens["SkipAutoLayout"] == null 
        || ((bool?)ViewContext.RouteData.DataTokens["SkipAutoLayout"]).GetValueOrDefault() == false)
    {
        if (IoC.Resolve<IContentProvider>().ShowLeftColumnData)
        {
            Layout = "~/Views/Shared/_LayoutWithLeftColumn.cshtml";
        }
        else
        {
            Layout = "~/Views/Shared/_Layout.cshtml";
        }
    }
}

```

To be able to set the view from the controller and use litium hierarchical url routing you manually need to add the `DataTokens["SkipAutoLayout"] = true` before render the view.

I think if you update `LitiumController` with this extra override you will get your master layout to be used instead of the automatic selection. You need to inherit the `LitiumController` instead of the `Controller` in your implementation.

```auto
        protected override ViewResult View(string viewName, string masterName, object model)
        {
            RouteData.DataTokens["SkipAutoLayout"] = true;
            return base.View(viewName, masterName, model);
        }

```

---

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [July 5, 2018, 1:47pm UTC](https://forum.litium.com/t/custom-controller-using-view/156/9 "2018-07-05T13:47:21Z")

</div>

Thank you both! And thank for this full explanation, I understand now and got it all working the way I want!
