0
votes

I have been running an Umbraco v7 site for about 3 years. Traditionally I have not rendered any pages using controllers, I have however set up some controllers that I POST forms to but that is about it. My existing View pages have logic baked into them and use external helper methods instead of controllers.

I am about to develop a new page and thought this would be my time to test a better design pattern. This page will render data from a controller as well as required form submission via a controller. What is the best route to proceed that will also go smoothly if I decide to do a refactor on my existing View pages?

I am more specifically looking for an answer around Render vs Surface controllers and which one would be better.It is my understanding that my routing would be unchanged if I went with a Render controller, but if I went with surface, I would have to have special routing?

But if I used a render controller, this does not support form submission?

Not sure what else I am missing?

Thanks Again, Devin

1

1 Answers

2
votes

You don't have to configure any special routing - everything is baked right in to Umbraco.

As a rule of thumb Surface Controllers are best used for reusable actions, custom controllers (Route Hijacking) are better for adding custom logic to whole pages (Document Types/Templates) in Umbraco.

Both approaches will enable you to accomplish exactly the same results - the only difference between them is abstraction.

Surface controllers are MVC Child Actions that inherit from Umbraco.Web.Mvc.SurfaceController - this adds helpful Umbraco specific properties and methods.

Surface Controllers are good for creating reusable things like forms or anywhere you need a partial to do anything complicated (i.e. backed by a controller). Have a look at the documentation here.

When you use a custom controller to change how pages are rendered that's called Route Hijacking

To do this you create your own controller than inherits from Umbraco.Web.Mvc.RenderMvcController like this:

public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
    public ActionResult MobileHomePage(RenderModel model)
    {
        //Do some stuff here, the return the base Index method
        return base.Index(model);
    }
}

This is a custom controller for the "Home" Document Type. You can of course return a custom model that inherits from RenderModel with your own properties and methods.

Fuller examples and documentation can be found here.

Post Requests

Both options allow you to handle POST requests by adding the [httppost] attribute like so:

Surface Controller:

public class YourSurfaceController: SurfaceController
{
    public ActionResult YourAction()
    {
      // Do stuff
    }

    [HttpPost]
    public ActionResult YourAction()
    {
      // Do stuff on POST
    }
}

Controller for route Hijacking:

public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
    public ActionResult MobileHomePage(RenderModel model)
    {
        //Do some stuff here, the return the base Index method
        return base.Index(model);
    }

    [HttpPost]
    public ActionResult MobileHomePage(RenderModel model)
    {
        //Do some stuff on POST, the return the base Index method
        return base.Index(model);
    }
}