Problems with custom routes and Umbraco 7 (404 error for children)
Hi,
I'm trying to create a custom routing (umbraco v7.4.3)
This is my first time with Umbraco/MVC and custom routes.
I created in content section an umbraco node (doctype: dati) called: dati (url: www.miodominio.it/dati)
This will have an undefined number of child and the url could be something like this:
- www.miodominio.it/dati/italy
- www.miodominio.it/dati/italy/univeristy
- www.miodominio.it/dati/italy/university/public
- etc.
Where italy, university and public are parameters.
In content tree I create for now only the "dati" node (document type: dati).
I create a DynamicPageRoutesHandler.cs
I create a ContentFinder.cs
I create a DatiController.cs
I create a DatiModel.cs
I create a DatiView.cshtml
The "dati" page function properly because the node exist in content section and use my custom view correctly passed by my custom routing.
When I try to display a child of dati (that not exist in content tree), I receive a 404 error
What I do now?
First of all I don't know what and how many children will have the "dati" node
I must create all rewriting urls for each parameter?
Can I tell to Umbraco to render a virtual node (document type: "dati") that not exist in content tree?
What is the right way?
Hi Lee,
I tried to use the same code like your, but it isn't working.
I try to display this url: www.mydomain.com/dati/dsu/italy/university
This is the code of MyApplication.cs:
using System.Linq;
using Umbraco.Core;
using Umbraco.Web.Routing;
namespace ustat.dati.Application
{
public class MyApplication : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, ContentFinderForWhatever>();
base.ApplicationStarting(umbracoApplication, applicationContext);
}
}
// the example here is to have a 'virtual url'.
// this is required on a specific DocType, after @level=3
public class ContentFinderForWhatever : IContentFinder
{
public bool TryFindContent(PublishedContentRequest contentRequest)
{
if (contentRequest != null)
{
var path = contentRequest.Uri.GetAbsolutePathDecoded();
var parts = path.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 2)
{
var level3 = string.Concat('/', string.Join("/", parts.Take(3)), '/');
var node = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(level3);
if (node.DocumentTypeAlias == "dati")
contentRequest.PublishedContent = node;
}
}
return contentRequest.PublishedContent != null;
}
}
}
But I receive this error: Object reference not set to an instance of an object.
"dati" (doctype: dati - controller: DatiController - action: Index) is the only node created in content.
When I try to view /dati/dsu/italy/university, I would to call a specific action (controller: DatiController - action: DataFilter) with dsu, italy and university as parameters. The node to render is always "dati" but it must display different views based on the action.
Hi Lee,
I used the AutoRouteTemplate package as you suggest and it function properly in a simple project (umbraco node with assigned template)...but not if I try to use my custom controller.
Problems with custom routes and Umbraco 7 (404 error for children)
Hi, I'm trying to create a custom routing (umbraco v7.4.3) This is my first time with Umbraco/MVC and custom routes.
I created in content section an umbraco node (doctype: dati) called: dati (url: www.miodominio.it/dati) This will have an undefined number of child and the url could be something like this: - www.miodominio.it/dati/italy - www.miodominio.it/dati/italy/univeristy - www.miodominio.it/dati/italy/university/public - etc.
Where italy, university and public are parameters.
In content tree I create for now only the "dati" node (document type: dati).
I create a DynamicPageRoutesHandler.cs
I create a ContentFinder.cs
I create a DatiController.cs
I create a DatiModel.cs
I create a DatiView.cshtml
The "dati" page function properly because the node exist in content section and use my custom view correctly passed by my custom routing. When I try to display a child of dati (that not exist in content tree), I receive a 404 error
What I do now? First of all I don't know what and how many children will have the "dati" node I must create all rewriting urls for each parameter? Can I tell to Umbraco to render a virtual node (document type: "dati") that not exist in content tree? What is the right way?
Thank you in advance for help Adriano
Hi Adriano,
The last time I did a custom routing with dynamic/wildcard parameters, I came up with this gist...
https://gist.github.com/leekelleher/5966488#file-myapplication-cs-L21
(See the comments under the code snippet)
Alternatively there's a package called AutoRouteTemplate that might be able to help.
Cheers,
- Lee
Hi Lee, I tried to use the same code like your, but it isn't working.
I try to display this url: www.mydomain.com/dati/dsu/italy/university
This is the code of MyApplication.cs:
But I receive this error: Object reference not set to an instance of an object.
"dati" (doctype: dati - controller: DatiController - action: Index) is the only node created in content. When I try to view /dati/dsu/italy/university, I would to call a specific action (controller: DatiController - action: DataFilter) with dsu, italy and university as parameters. The node to render is always "dati" but it must display different views based on the action.
I hope I explained clearly my need
Adriano
Hi Lee, I used the AutoRouteTemplate package as you suggest and it function properly in a simple project (umbraco node with assigned template)...but not if I try to use my custom controller.
Can you help me?
You can refer directly to my post in AutoRouteTemplate package forum for details.
Thank you Adriano
Hi Adriano,
I'm not sure where you're at with your code now... have you decided on using AutoRouteTemplate?
(I haven't actually used that package before, but I know the developer, Jonathan - who's been replying on the other forum thread)
Interesting that the custom
IContentFinder
code didn't work. I'd be curious what is causing the null reference error.Cheers,
- Lee
Hi Lee, yes...at the end I used the AutoRouteTemplate and I solved.
This is the last post I send to Jonathan that explain what I did.
Thank you again for your support.
Have a nice day
Adriano
is working on a reply...