Simple routing to get value

Using EpiServer 7 WebForms I want to be able to achieve the following:

Call on a EPiServer page for displaying info on a car. The car itself is not a page in EPiServer, much like the product page in EPiServer Commerce

I want to call the page by appending the car registration number in the url (site.com/car/ABC123) and then use that registration number on the page to call a search service with that registration number.

Read some articles about the routing but can't really figure out how to use it this way.

So on the page I would like to have the possibility to use the registration number in function calls like:

var carDetails = GetDetails(carRegNr);

Some pointers on how I would achive this would be rellay nice.

#77744 Nov 25, 2013 17:22
  • Member since: 2004
     

    Hi

    Sounds like partial routing could be the way, see e.g. http://joelabrahamsson.com/custom-routing-for-episerver-content/ or http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Partial-Routing/

    The example about Blog routing in the latter post could be useful. In that case the partial router returns the same page, that is if you still want to route to the same page. It uses extension method

    segmentContext.SetCustomRouteData<T>
    to set the partial routed data (in your case it would be the registration number).

    You could then in the template get the value using:
    HttpContext.Current.Request.RequestContext.GetCustomRouteData<T>

     

    #77746 Edited, Nov 25, 2013 17:35
  • This seems the correct way, just can't figure out what exactly to put in the SetCustomRouteData. I have this setup:

    The template page CarDetailsPageTemplate.aspx + .cs which inherits from CarDetailsPageBase

    public class CarDetailsPageBase : PageBase
        {
            private static PageData _carDetailsPage;
    
            public static PageData CarDetailsPage
            {
                get
                {
                    if (_carDetailsPage == null)
                    {
                        _carDetailsPage = DataFactory.Get<PageData>((PageReference)StartPage["CarDetailsPage"]);
                    }
    
                    return _carDetailsPage;
                }
            }
    
            public static string CarRegistrationNumber { get; set; }
        }

        

    The CarDetailsPartialRouter looks like this at the moment:

    public class CarDetailsPartialRouter : IPartialRouter<CarDetailsPage, CarDetailsPageTemplate>
        {
            public object RoutePartial(CarDetailsPage content, SegmentContext segmentContext)
            {
                //Expected format is RegNumber/
                var regNumber = segmentContext.GetNextValue(segmentContext.RemainingPath);
                if (!String.IsNullOrEmpty(regNumber.Next))
                {
    
                    segmentContext.SetCustomRouteData<CarDetailsPageTemplate>();
                    return content;
    
                }
    
                return null;
            }
    
            public PartialRouteData GetPartialVirtualPath(CarDetailsPageTemplate content, string language, RouteValueDictionary routeValues, RequestContext requestContext)
            {
                return new PartialRouteData()
                    {
                        BasePathRoot = CarDetailsPageBase.CarDetailsPage.PageLink,
                        PartialVirtualPath = String.Format("{0}/", CarDetailsPageBase.CarRegistrationNumber)
                    };
            }
        }

        

    What do I put in the SetCustomRouteData?

    #77778 Edited, Nov 26, 2013 10:43
  • Member since: 2004

    If registration number is the only thing you need, I guess it would be enough to set a string in the partial router like:

    segmentContext.SetCustomRouteData<string>("regnumber", theRegnr);

    And then in the template you would read out the routed reg nr as:

    string regNr = HttpContext.Current.Request.RequestContext.GetCustomRouteData<string>("regnumber")

    Note also that you should consume the segment (update RemainingPath) in the router if you handle the segment, that add the following line to your router:

    segmentContext.RemainingPath = regNumber.Remaining

    #77782 Nov 26, 2013 10:50
  • Yes got it all working now, so nice to be able to do this. Thanks for the input. Also found this article very helpful: http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/7/Routing/Partial-Routing/Example-of-News-Partial-Routing/

    Escpecially the InitalizationModule setup which I didn't really get from the start.

    #77807 Nov 26, 2013 14:19
This topic is locked because the last reply was posted more than 6 months ago. Please contact epw@episerver.com to unlock it.