Copied to clipboard

Flag this post as spam?

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


  • Ulf Möllerström 46 posts 190 karma points
    Jun 27, 2016 @ 12:34
    Ulf Möllerström
    0

    MVC style route mapping

    I'm trying to get my head around getting MVC type routing...

    This works fine, and the url looks like: https://(sitename)/currenttimefor/location/51.507222;-0.1275

    //This code works perfect
    RouteTable.Routes.MapRoute(
        "CurrentTimeForLocation",
        "currenttimefor/{action}/{latlon}",
        new
        {
            controller = "TimeService",
            action = "Location",
            latlon = UrlParameter.Optional
        });
    

    Now I'd also would like to have this for named places, like: https://(sitename)/currenttimefor/london

    But can't find any working code for this, how much I try.

    (I'm using a class that inherits from UmbracoApplication, to map the route.)

  • Jamie Pollock 172 posts 846 karma points c-trib
    Jun 27, 2016 @ 15:43
    Jamie Pollock
    0

    Hi Ulf,
    Is this code using .net WebApi (eg. intended to return JSON or XML) or is it using MVC returning a HTML view?

    Thanks,
    Jamie

  • Ulf Möllerström 46 posts 190 karma points
    Jun 28, 2016 @ 08:33
    Ulf Möllerström
    0

    It returns JSON (JsonResponse) in a MVC-application inside Umbraco.

  • Lars-Erik Aabech 348 posts 1096 karma points MVP 4x c-trib
    Jun 27, 2016 @ 18:13
    Lars-Erik Aabech
    0

    The place URL you refer to does not have an action specified. You're calling a parameterless action called "London". If you go https://(sitename)/currenttimefor/place/london and you have an action named Place, it should work just as well as the Location one.

    This is pure MVC stuff and not Umbraco stuff, btw. ;)

  • Ulf Möllerström 46 posts 190 karma points
    Jun 28, 2016 @ 08:32
    Ulf Möllerström
    0

    Well, that one works, but not as intended, since the "/place/" part should'n be in the url... That one I can manage to get right. :)

  • Lars-Erik Aabech 348 posts 1096 karma points MVP 4x c-trib
    Jun 28, 2016 @ 08:35
    Lars-Erik Aabech
    0

    Then you'll need to configure two different routes:

    RouteTable.Routes.MapRoute(
        "CurrentTimeForLocation",
        "currenttimefor/location/{latlon}",
        new
        {
            controller = "TimeService",
            action = "Location"
            // I guess latlon shouldn't really be optional?
        });
    
    RouteTable.Routes.MapRoute(
        "CurrentTimeForPlace",
        "currenttimefor/{place}",
        new
        {
            controller = "TimeService",
            action = "Place"
        });
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies