I'm having a little trouble hijacking a route (something that I've done a few times so probably missing something obvious).
Anyway, here's my controller:
using System.Web.Mvc;
using Umbraco.Web;
using Umbraco.Web.Models;
using UmbracoSandbox.Models;
namespace UmbracoSandbox.Controllers.Render
{
public class UmbracoRssFeedController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult UmbracoRssFeed(RenderModel model)
{
var feedsModel = new UmbracoRssFeedModel
{
Feeds = model.Content.GetPropertyValue("feeds")
};
return CurrentTemplate(feedsModel);
}
}
}
My model:
using Umbraco.Web;
using Umbraco.Web.Models;
namespace UmbracoSandbox.Models
{
public class UmbracoRssFeedModel : RenderModel
{
public UmbracoRssFeedModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
public object Feeds { get; set; }
}
}
The error you have indicates your view is loading... and is strongly typed to UmbracoSandbox.Models.UmbracoRssFeedModel but the model being sent by the controller is of the default type Umbraco.Web.Models.RenderModel.
So my guess is that the route isn't being hijacked, and the default RenderMvcConroller is firing and sending the default model to your view.
Route hijacking is by convention, so for your above route to hijack, you would need a document type called:
UmbracoRssFeed
published with a corresponding template called:
UmbracoRssFeed
So the first thing to check is the naming of your document type and template.
If you do have a document type called UmbracoRssFeed, then hijacking will look for an ActionResult matching the template name, and if not fallback to use an ActionResult called Index:
So try adding the following to your controller:
public override ActionResult Index(RenderModel model)
{
//set a breakpoint here
return CurrentTemplate(model);
}
Now you can add a breakpoint in visual studio to both of your ActionResults and determine which was/is being executed.
Thanks Marc, you can ignore the object type - it was just me being lazy while I set up the route hijacking. I do tend to make use of property converters anyway.
Route hijacking with custom controller
Hi,
I'm having a little trouble hijacking a route (something that I've done a few times so probably missing something obvious).
Anyway, here's my controller:
My model:
My view:
When I load my page, I get the following error:
As I said, I feel like I'm missing something obvious but it escapes me right now. I've followed the docs found here with no luck - https://our.umbraco.org/documentation/reference/routing/custom-controllers
I've also tried the solution in this thread: https://our.umbraco.org/forum/using-umbraco-and-getting-started/75998-update-to-742-all-custom-hijacked-controllers-stopped-working
If anyone could put me out of my misery, that'd be hugely appreciated.
Thanks!
Hi ONeji
I think you have followed the documentation well!
The error you have indicates your view is loading... and is strongly typed to UmbracoSandbox.Models.UmbracoRssFeedModel but the model being sent by the controller is of the default type Umbraco.Web.Models.RenderModel.
So my guess is that the route isn't being hijacked, and the default RenderMvcConroller is firing and sending the default model to your view.
Route hijacking is by convention, so for your above route to hijack, you would need a document type called:
UmbracoRssFeed
published with a corresponding template called:
UmbracoRssFeed
So the first thing to check is the naming of your document type and template.
If you do have a document type called UmbracoRssFeed, then hijacking will look for an ActionResult matching the template name, and if not fallback to use an ActionResult called Index:
So try adding the following to your controller:
Now you can add a breakpoint in visual studio to both of your ActionResults and determine which was/is being executed.
regards
Marc
Thanks Marc, you've hit the nail on the head!
I had this working before implementing the model but must have changed the template name without updating the ActionResult name.
I knew it was something obvious! Thanks for pointing me in the right direction!
Good to here ONeji!
I do wonder what type your feeds property is?
you should be able to cast it to a strong type rather than use 'object'
of if it is a complex object you can create you own property value converter
https://our.umbraco.org/documentation/extending/property-editors/value-converters
to have
(but if it is a core property type there will be a converter in this package you can use: https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/ now included in Umbraco v7.6+)
cheers
Marc
Thanks Marc, you can ignore the object type - it was just me being lazy while I set up the route hijacking. I do tend to make use of property converters anyway.
Appreciate the extra info though :)
Thanks,
Ben
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion