Copied to clipboard

Flag this post as spam?

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


  • Ryan 9 posts 30 karma points
    Apr 10, 2015 @ 04:57
    Ryan
    1

    custom route for regular content

    I'm using 7.2.4. In my site I have a page where I want to use a route like this profile/{vanityurl}. In my case, the profiles do not exist as Umbraco content but rather they come from intergration with another system. I have a document type, template and page for the profile. My questions is, how do I allow this page to accept the /{vanityurl} in the route?

    I've registered a route for the controller

    RouteTable.Routes.MapRoute(
                    "profile",
                    "profile/{vanityurl}",
                    new
                    {
                        controller = "Profile",
                        action = "ShowProfile"
                    });

     

    And used EnsurePublishedContentRequest on the cotroller action

        public class ProfileController : SurfaceController
        {
            [EnsurePublishedContentRequest(2263)]
            public ActionResult ShowProfile(string vanityurl)
            {
                //todo: do something...
                return PartialView("~/Views/Partials/Profile.cshtml");
            }
    
        }

     

    However, the view that's rendered isn't using the master template of the page. Instead, just the partial template is rendered.

    I've also looked at this article and some other forum posts and they seem geared towards content that actually exists in Umbraco, using UmbracoVirtualNodeRouteHandler to find the child node and associate it with the route. In my case, all I want to do is to be able call the one page with a custom route. 

    Any Suggestions?

    Thanks,

    Ryan

     

  • Ryan 9 posts 30 karma points
    Apr 10, 2015 @ 14:22
    Ryan
    0

    I think I found a solution following this post.

    My route:

       RouteTable.Routes.MapUmbracoRoute(
                "profile",
                "profile/{vanityurl}",
                new
                {
                    controller = "Profile",
                    action = "ShowProfile"
                },
                new ProfileRouteHandler(2263));
    

    The route handler:

    public class ProfileRouteHandler : UmbracoVirtualNodeByIdRouteHandler
        {
            public ProfileRouteHandler(int nodeId)
                : base(nodeId)
            {
            }
        }
    

    Controler:

    public class ProfileController : SurfaceController
        {
    
            public ActionResult ShowProfile(RenderModel model, string vanityurl)
            {
                //todo: do something...
                return View("~/Views/Profile.cshtml", model);
            }
    
        }
    

    The reason my master template was not being used is because I incorrectly assumed that my controller's action was being called by Umbraco as a child action. When I added the Layout to the view and passed the RenderModel it worked as expected.

    Now I'm stuck on another issue. In my view I want to use a model:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ExternalSite.Models.ProfileModel>
    

    I've tried this:

     public ActionResult ShowProfile(RenderModel model, string vanityurl)
            {
    
                RouteData.DataTokens["umbraco"] = model;
    
                var profileModel = new ProfileModel() {FirstName = "test", LastName = "data"};
    
                //todo: do something...
                return View("~/Views/Profile.cshtml", profileModel);
            }
    

    But it results in a error: The model item passed into the dictionary is of type 'ExternalSite.Models.ProfileModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'.

    Any ideas on how I can use a custom model? If I'm going about this the wrong way please let me know.

  • Daniel 9 posts 80 karma points
    Aug 01, 2016 @ 18:41
    Daniel
    0

    Did you made any progress on this ?

  • Ryan 9 posts 30 karma points
    Aug 15, 2016 @ 15:20
    Ryan
    0

    Yes, I can't remember where I found some documentation but it looks something like this. I just reviewed it to see if I could refresh my memory more but it's been too long.

                RouteTable.Routes.MapUmbracoRoute(
                "profile",
                "profile/{vanityurl}/{zipCode}",
                new
                {
                    controller = "Profile",
                    action = "ShowProfile",
                    zipCode = UrlParameter.Optional
                },
                new PageRouteHandler(int.Parse(WebConfigurationManager.AppSettings["ProfilePageId"])));
    

    The view that the controller is returning inherits a class derived from RenderModel.

    public class ProfileModel : RenderModel
    {...}
    
Please Sign in or register to post replies

Write your reply to:

Draft