Then you should also use RouteTable.Routes.MapUmbracoRoute, which allows to add a VirtualNodeHandler. The last one gives you the possibility to map a route to a Node in your umbraco tree.
First of all I've restored global.asax to it's original state :)
Then I've placed an UrlRoute.cs in the app_code folder and moved the route to it, so right now it looks like this:
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using System.Web.Routing;
namespace UrlRoute.EventHandlers
{
public class RegisterEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteTable.Routes.MapPageRoute("Route1_StudentAge", "age/{j}", "~/test");
}
}
}
So far so good :) No compiling errors :) Obviously this isn't enough, just for fun I tried domain.dk/age/123/ and of course I got a 404 :)
Thanks, I did take a look at it but the docs seemed to be incomplete :) Anyway, did a fast rewrite, but that just leaves me just as stuck as before :)
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using System.Web.Routing;
using Umbraco.Web.Mvc;
namespace UrlRoute.EventHandlers
{
public class RegisterEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteTable.Routes.MapUmbracoRoute(
"Route1_StudentAge",
"age/{j}",
new{
controller = "MyTest",
j = UrlParameter.Optional
},
new UmbracoVirtualNodeByIdRouteHandler(1264)
);
}
}
}
Sorry, I may be a bit of a noob in this area :) But,
First of all I get this error:
Compiler Error Message: CS1061: 'System.Web.Routing.RouteCollection' does not contain a definition for 'MapUmbracoRoute' and no extension method 'MapUmbracoRoute' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)
When I change the code to:
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using System.Web.Routing;
using Umbraco.Web.Mvc;
using Umbraco.Web.Models;
namespace UrlRoute.EventHandlers
{
public class RegisterEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
/*RouteTable.Routes.MapUmbracoRoute(
"Route1_StudentAge",
"age/{j}",
new{
controller = "MyTest",
j = UrlParameter.Optional
},
new UmbracoVirtualNodeByIdRouteHandler(1264)
);*/
}
}
public class MyTestController : RenderMvcController
{
public ActionResult Product(RenderModel model, string j)
{
}
}
}
I get this one:
Compiler Error Message: CS0246: The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)
Haha things are getting a bit more complicated than what I started out with :)
7.6.1: Url routing
Hi
I'm trying to do some url routing, but I'm not quite sure if I'm trying to do the right thing the right place :)
I've tried adding to the global.asax file so it would look like this:
Well, of course it didn't work at first try - it told me that RouteTable didn't exist in that context.
Anyway, before I even try to fix that, I was wondering if I was even on the right track? :)
Kind regards, Bruno
Hi Bruno,
I'd use a class inheriting from ApplicationEventHandler for routing - override the ApplicationStarting method and copy your routes in there.
Hi Bruno,
You should be using ApplicationEventHandlers to hook into the application startup.
Then you should also use
RouteTable.Routes.MapUmbracoRoute
, which allows to add a VirtualNodeHandler. The last one gives you the possibility to map a route to a Node in your umbraco tree.Kind regards
Damiaan
Hi james and Damiaan
Thanks for your replies :)
First of all I've restored global.asax to it's original state :)
Then I've placed an UrlRoute.cs in the app_code folder and moved the route to it, so right now it looks like this:
So far so good :) No compiling errors :) Obviously this isn't enough, just for fun I tried domain.dk/age/123/ and of course I got a 404 :)
Kind regards, Bruno
Hmm. I can't seem to get it working. I'm sure I have to do something with global.asax but it won't accept anything I put in it.
Like Damiaan said try the MapUmbracoRoute. Here are the docs: https://our.umbraco.org/documentation/reference/routing/custom-routes
Another blog: https://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/
Jeroen
Hi Jeroen
Thanks, I did take a look at it but the docs seemed to be incomplete :) Anyway, did a fast rewrite, but that just leaves me just as stuck as before :)
I've got the using directives completely wrong...
Kind regards, Bruno
And you do have a "MyTest" controller right? Do you get an exception? Or just a 404 error?
Hi Damiaan
Sorry, I may be a bit of a noob in this area :) But,
First of all I get this error:
Compiler Error Message: CS1061: 'System.Web.Routing.RouteCollection' does not contain a definition for 'MapUmbracoRoute' and no extension method 'MapUmbracoRoute' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)
When I change the code to:
I get this one:
Compiler Error Message: CS0246: The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?)
Haha things are getting a bit more complicated than what I started out with :)
Kind regards, Bruno
Hi again
I think I'll drop this approach, as I can't seem to get it to work. I'll try through urlrewriting.config again - which means new thread.
Hi Bruno,
I've setup a demo project for showing some Umbraco URL/route handling possibilities.
In that, I also implemented custom routes by using UmbracoVirtualNodeByIdRouteHandler
You can find it here: https://github.com/Mantus667/UmbracoUrlHandling
Maybe that helps in getting started.
Regards David
Hi David
Thanks :) I'll have a look and learn :)
Kind regards, Bruno
Wow!
The ImmoUrlSegmentProvider is the EXACT code (including comments) I shared at a codegarden with someone a few years ago.
Love to see it is still served as an example.
Kind regards
Damiaan
Yes,
think I grabbed it from 24days.in and reused it for my demo project. It was such a good example I had to copy it :)
Regards David
is working on a reply...