U4-2342 - Create an interface for route hijacker controllers

Created by Matt Brailsford 09 Jun 2013, 15:37:57 Updated by Shannon Deminick 27 Jan 2014, 21:20:52

Create an interface for route hijacker controllers so that a single controller can be used for both route hijacking and surface controllers, rather than requiring 2 controllers.


Jeroen Breuer 19 Jun 2013, 08:01:20

If it's for 6.0.7 it also means it will be part of 6.1.2 right? It's a bit confusing that it's not part of 6.1 yet, but will be part of a lower version.

Shannon Deminick 19 Jun 2013, 23:00:24

All fixes in lower versions get merged to higher versions, always.

Shannon Deminick 24 Jun 2013, 23:34:44

Complete in rev: 1ec7cd3b89a2897d8f8bc5ef49bfc90b34de39e7 - there's a new IRenderMvcController interface you can implement. So long as your controller is of type ControllerBase and IRenderMvcController, you can use any controller that you want for hijacking routes.

Jeroen Breuer 25 Jun 2013, 07:39:10

Is there some best practice now that this interface is availble? Currently I have a controller which inherits from RenderMvcController for route hijacking and a controller which inherits from SurfaceController for child actions or posting. With this inferface can I use the SurfaceController for both? I use the CurrentPage property in the RenderMvcController and SurfaceController a lot, but if I look at the source they do something different in both Controllers. Should the CurrentPage property still work if I use the SurfaceController for route hijacking? Maybe an example on how to use this interface would be nice ;-).

Shannon Deminick 25 Jun 2013, 07:43:26

If you use this interface you are going to have to do some extra work that is normally available in the RenderMvcController like the methods: CurrentTemplate, EnsurePhsyicalViewExists

If you don't have a good reason to separate your SurfaceControllers from your RenderMvcControllers then I would recommend to not use this interface since it will require you to do some manual work.

Jeroen Breuer 25 Jun 2013, 07:54:32

The main reason is that I now need 2 controllers to do the work which in a normal MVC webapp would be in 1 controller. I think that going for a custom base SurfaceController which has all the extra methods seems like a good solution. That way all my Controllers can inherit from 1 controller, because I now have some double code which is the same in my base RenderMvcController and base SurfaceController.

Shannon Deminick 25 Jun 2013, 07:58:06

I guess you'll just have to copy these two methods to your base surface controller class:


/// Checks to make sure the physical view file exists on disk /// /// /// protected bool EnsurePhsyicalViewExists(string template) { var result = ViewEngines.Engines.FindView(ControllerContext, template, null); if(result.View == null) { LogHelper.Warn("No physical template file was found for template " + template); return false; } return true; }

	/// <summary>
	/// Returns an ActionResult based on the template name found in the route values and the given model.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="model"></param>
	/// <returns></returns>
	/// <remarks>
	/// If the template found in the route values doesn't physically exist, then an empty ContentResult will be returned.
	/// </remarks>
	protected ActionResult CurrentTemplate<T>(T model)
		var template = ControllerContext.RouteData.Values["action"].ToString();
		if (!EnsurePhsyicalViewExists(template))
			return Content("");
		return View(template, model);

	/// <summary>
	/// The default action to render the front-end view
	/// </summary>
	/// <param name="model"></param>
	/// <returns></returns>
	public virtual ActionResult Index(RenderModel model)
		return CurrentTemplate(model);

I can't add these methods to the SurfaceController class since they don't make sense there. Can add them as extension methods or something though.

Jeroen Breuer 25 Jun 2013, 08:01:03

Thanks for the info. I could just copy these methods to my base SurfaceController so that's fine. I don't know if you need to create extension methods for them.

Matt Brailsford 25 Jun 2013, 08:01:26

Couldn't those 2 methods be extension methods and therfor be accessible to both?

Shannon Deminick 25 Jun 2013, 08:02:51

Thats what I said :)

Matt Brailsford 25 Jun 2013, 08:06:12

Haha, so you did. Good idea :)

Jeroen Breuer 23 Jan 2014, 09:12:57

Did those methods ever became extension methods?

Shannon Deminick 27 Jan 2014, 21:20:52

nope, not yet - please create a new task if that is still important to you (or a PR :)

Priority: Normal

Type: Feature (request)

State: Fixed

Assignee: Shannon Deminick

Difficulty: Normal


Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version: 6.0.7


Story Points: