Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bruno Olsen 75 posts 316 karma points
    Jul 07, 2017 @ 10:53
    Bruno Olsen
    0

    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:

    <%@ Application Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>
    
    
    <script runat="server">
    void Application_Start(object sender, EventArgs e) {
        RouteTable.Routes.MapPageRoute("Route1_StudentAge", "age/{j}", "~/test");
    }
    </script>
    

    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

  • james 37 posts 121 karma points c-trib
    Jul 07, 2017 @ 11:53
    james
    0

    Hi Bruno,

    I'd use a class inheriting from ApplicationEventHandler for routing - override the ApplicationStarting method and copy your routes in there.

  • Damiaan 437 posts 1288 karma points MVP 3x c-trib
    Jul 07, 2017 @ 11:55
    Damiaan
    0

    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

  • Bruno Olsen 75 posts 316 karma points
    Jul 13, 2017 @ 07:46
    Bruno Olsen
    0

    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:

    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 :)

    Kind regards, Bruno

  • Bruno Olsen 75 posts 316 karma points
    Jul 14, 2017 @ 06:23
    Bruno Olsen
    0

    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.

  • Jeroen Breuer 4861 posts 12134 karma points MVP 3x admin c-trib
    Jul 14, 2017 @ 08:36
  • Bruno Olsen 75 posts 316 karma points
    Jul 14, 2017 @ 09:24
    Bruno Olsen
    0

    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 :)

    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)
            );
            }
        }
    }
    

    I've got the using directives completely wrong...

    Kind regards, Bruno

  • Damiaan 437 posts 1288 karma points MVP 3x c-trib
    Jul 14, 2017 @ 10:44
    Damiaan
    0

    And you do have a "MyTest" controller right? Do you get an exception? Or just a 404 error?

  • Bruno Olsen 75 posts 316 karma points
    Jul 14, 2017 @ 11:47
    Bruno Olsen
    0

    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:

    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 :)

    Kind regards, Bruno

  • Bruno Olsen 75 posts 316 karma points
    Aug 01, 2017 @ 07:16
    Bruno Olsen
    0

    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.

  • David Brendel 774 posts 2843 karma points c-trib
    Aug 01, 2017 @ 08:52
    David Brendel
    100

    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

  • Bruno Olsen 75 posts 316 karma points
    Aug 01, 2017 @ 09:11
    Bruno Olsen
    0

    Hi David

    Thanks :) I'll have a look and learn :)

    Kind regards, Bruno

  • Damiaan 437 posts 1288 karma points MVP 3x c-trib
    Aug 01, 2017 @ 11:53
    Damiaan
    0

    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

  • David Brendel 774 posts 2843 karma points c-trib
    Aug 01, 2017 @ 13:16
    David Brendel
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft