At the moment I can create a controller and have an action execute for a specific template applied to the document but if I have a rewrite rule that does a rewrite to apply an alttemplate to the current page I can't have an action with that name execute in my custom controller.
Simplified example:
public class NewsController : RenderMvcController
{
// Default action for NewsSection documents
public ActionResult Index(RenderModel model, int? page)
{
var vm = new NewsSection(model.Content);
var pageNumber = page ?? 1;
var articles = model.Content.Children<NewsArticle>().Where(n => n.IsVisible());
vm.Articles = articles.OrderByDescending(n => n.ReleaseDate).ToPagedList(pageNumber, 10);
return this.CurrentTemplate(vm);
}
// Action to execute when the url is called with an alttemplate applied e.g. /news/?alttemplate=tagsearch&tag=tagname
public ActionResult TagSearch(RenderModel model, int? page, string tag)
{
[...]
}
}
Interested to know if anyone has got this work or taken a different approach?
Does it if you remove all the params from the action (page and tag)
No, still doesn't.
And I think you need to override the index method
If I do that I can't provide my custom parameters but I'm also not sure what the impact of my implementation is (doesn't seem to be any)? That said, I have changed it so that instead of overriding the Index action and sacrificing my custom parameters I am doing it via the template as follows:
public class NewsController : RenderMvcController
{
// Default action for NewsSection documents
public ActionResult TemplateName(RenderModel model, int? page)
{
var vm = new NewsSection(model.Content);
var pageNumber = page ?? 1;
var articles = model.Content.Children<NewsArticle>().Where(n => n.IsVisible());
vm.Articles = articles.OrderByDescending(n => n.ReleaseDate).ToPagedList(pageNumber, 10);
return this.CurrentTemplate(vm);
}
// Action to execute when the url is called with an alttemplate applied e.g. /news/?alttemplate=tagsearch&tag=tagname
public ActionResult TagSearch(RenderModel model, int? page, string tag)
{
[...]
}
}
…still, it doesn't advance me any closer to solving my issue, unfortunately.
Dave, immediately after submitting my last response the lightbulb switched on and I realised my mistake after re-reading your initial response. The key being matching the template alias. My template name (which I have been using) is not the same as my template alias! Replacing the name with the alias and things kick into action :)
Thanks again, always helps to bounce these things off of other people to make it become more obvious what you can't see which is right in front of you!
Hijacking Umbraco Routes for AltTemplates
Is this possible?
At the moment I can create a controller and have an action execute for a specific template applied to the document but if I have a rewrite rule that does a rewrite to apply an alttemplate to the current page I can't have an action with that name execute in my custom controller.
Simplified example:
Interested to know if anyone has got this work or taken a different approach?
Thanks, Simon
Hi Simon,
I think a action with the name with matching the template alias like you have would do the trick.
Does it hit when you ommit the tagname parameter ?
And does it hit when you use : /news/tagsearch ?
Another option is to check if alt templates aren't disabled in config : https://our.umbraco.com/documentation/Reference/Config/umbracoSettings/#webrouting
Dave
Hi Dave,
As I thought.
No. I have tried the following urls with no luck hitting my breakpoint within the action named afte the alttemplate:
/news/tagsearch
/news/?alttemplate=tagsearch
/news/?alttemplate=tagsearch&p=1
/news/?alttemplate=tagsearch&tag=test
/news/?alttemplate=tagsearch&p=1&tag=test
All of the above actually hit the
Index
action in my custom controller and not the action I have defined for the alttemplate.disableAlternativeTemplates
=false
in my umbracoSettings and the alttemplate is applied as expected.Simon
Hi Simon,
Does it if you remove all the params from the action (page and tag)
Maybe do the same for the Index action.
Dave
No, still doesn't.
If I do that I can't provide my custom parameters but I'm also not sure what the impact of my implementation is (doesn't seem to be any)? That said, I have changed it so that instead of overriding the
Index
action and sacrificing my custom parameters I am doing it via the template as follows:…still, it doesn't advance me any closer to solving my issue, unfortunately.
Is your TemplateName method hit ?
Dave
Dave, immediately after submitting my last response the lightbulb switched on and I realised my mistake after re-reading your initial response. The key being
matching the template alias
. My template name (which I have been using) is not the same as my template alias! Replacing the name with the alias and things kick into action :)Thanks again, always helps to bounce these things off of other people to make it become more obvious what you can't see which is right in front of you!
Glad to hear you have it working now !!
Dave
And I think you need to override the index method
https://our.umbraco.com/documentation/Reference/Routing/custom-controllers#creating-a-custom-controller
Dave
is working on a reply...