Try our conversational search powered by Generative AI!

Binh Nguyen Thi
Oct 15, 2020
  3965
(2 votes)

How to route a simple url address within action name

What is the simple address in the EpiServer?

The simple address is a single segment uniquely identifying content and its language. The simple address can be used as a direct address without parent nodes included in URL.

The situation here

You are using simple url address for your CMS pages and there are some pages you want to proceed data that sent from the client.

For example:

You are in the contact page "{base_url}/contact-page-seo-url", you have a submit form to allow user input data and send back to our system by POST endpoint "{base_url}/contact-page-seo-url/subcription".

Actually, you cannot use the endpoint like this, you will get 404 message immediately.

Why?

I realized that the default simple address route of Episerver always considers that all path after {base_url} is SIMPLE ADDRESS PATH includes ACTION name.

So the system cannot route any content that matchs to the simple address "contact-page-seo-url/subcription".

How can we solve this problem?

Here is it. You need to add more a route to allow that. And we do not need to create any custom route, just do like that

    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
    public partial class SiteInitializationModule : IConfigurableModule
    {

        public void Initialize(InitializationEngine context)
        {
            RegisterCustomSimpleAddressRoute(context, RouteTable.Routes);
        }

        public void ConfigureContainer(ServiceConfigurationContext context)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        private static void RegisterCustomSimpleAddressRoute(InitializationEngine context, RouteCollection routes)
        {
            var rewriteExtension = Settings.Instance.UrlRewriteExtension;

            var urlResolver = context.Locate.Advanced.GetInstance<UrlResolver>();
            var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

            var contentLanguageSettingsHandler = context.Locate.Advanced.GetInstance<IContentLanguageSettingsHandler>();

            var basePathResolver = context.Locate.Advanced.GetInstance<IBasePathResolver>();

            var addressSegmentRouter = new SimpleAddressSegmentRouter(sd => sd.StartPage);

            var parameters = new MapContentRouteParameters()
            {
                UrlSegmentRouter = addressSegmentRouter,
                BasePathResolver = basePathResolver.Resolve,
                Direction = SupportedDirection.Incoming,
                SegmentMappings = new Dictionary<string, ISegment>()
                {
                    {
                        RoutingConstants.SimpleAddressKey,
                        new SimpleAddressSegment(RoutingConstants.SimpleAddressKey, rewriteExtension, addressSegmentRouter, contentLoader, urlResolver, contentLanguageSettingsHandler)
                        {
                            MatchOneSegment = true
                        }
                    }
                }
            };


            routes.MapContentRoute("CustomSimpleAddress", "{simpleaddresslanguage}/{simpleaddress}/{partial}/{action}", (object)new
            {
                action = "index"
            }, parameters);

        }
    }

Tada, it is not much complicated. You just need to register more another simple address route with parameter "MatchOneSegment = true". 

This parameter indicates that only one segment is matching to a certain simple address instead of all segments as default.

Hope this help some of you.

Oct 15, 2020

Comments

Please login to comment.
Latest blogs
New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog

Webinar: Get Started with AI within Optimizely CMS

Join us for the webinar "Get Started with AI in Optimizely CMS" on Wednesday, May 8th. Don't forget to register!

Luc Gosso (MVP) | May 7, 2024 | Syndicated blog

Search & Navigation: Indexing job new features

From Episerver.Find version 16.1.0, we introduced some new features that make the indexing job in CMS more flexible and efficient: Support continuo...

Vinh Cao | May 7, 2024