Trouble of url mapping in route hijacking in EventHandler
I am trying to hijack route in EventHandler.
I don't know how to map the first part of the url which is currently this:
"the-top-parent/parent/library/{id}/{slug}"
and it works without problem.
What I would like to do instead of the above is to have:
"{parentslug}/library/{id}/{slug}"
but that doesn't work - the app cannot find the template if the first bit of the url is dynamic/ not predefined - and displays error instead of the valid page:
Sequence contains no elements
Is it possible to make the start of the url optional? And how?
public ActionResult GetItem(RenderModel renderModel, string parentslug, int? id, string slug) {
...
}
public class MyStartupHandler : IApplicationEventHandler
{
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteTable.Routes.MapRoute(
"item",
"the-top-parent/parent/library/{id}/{slug}",
new
{
controller = "MyController",
action = "GetItem",
slug = UrlParameter.Optional
});
}
}
public class MyContentFinder : IContentFinder
{
public bool TryFindContent(PublishedContentRequest request)
{
var path = request.Uri.GetAbsolutePathDecoded();
if (!path.StartsWith("/woot"))
return false; // not found
// have we got a node with ID 1234?
var contentCache = UmbracoContext.Current.ContentCache;
var content = contentCache.GetById(1234);
if (content == null) return false; // not found
// render that node
request.PublishedContent = content;
return true;
}
}
Trouble of url mapping in route hijacking in EventHandler
I am trying to hijack route in
EventHandler
. I don't know how to map the first part of the url which is currently this:and it works without problem.
What I would like to do instead of the above is to have:
but that doesn't work - the app cannot find the template if the first bit of the url is dynamic/ not predefined - and displays error instead of the valid page:
Is it possible to make the start of the url optional? And how?
I'm using Umbraco 7.1.8.
You could create a contentfinder.
Especially the example on the linked page above:
Hope that helps.
Thanks but I read the article you quoted and am not sure how to implement it so I can route correctly what I need?
Ok. Fair question. Let's turn things around. Why do you want to route to a controller? Is their a umbraco node matching the url?
To keep consistent with the page parent structure
look this:
https://shazwazza.com/post/custom-mvc-routing-in-umbraco
https://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/
Thanks Marcio but I'm not having problem with routing - it works fine.
I want to make the start of url in
MapRoute
optional and this is what I don't know how to achieve.Eg. instead of
I want to use
and pass
parentslug
as parameter as well to the handling method.is working on a reply...