🔧 Maintenance Alert: World will be on Read-Only Mode on February 18th, 10:00 PM – 11:00 PM EST / 7:00 PM – 8:00 PM PST / 4:00 AM – 5:00 AM CET (Feb 19). Browsing available, but log-ins and submissions will be disabled.

Binh Nguyen Thi
Oct 15, 2020
  4541
(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
The missing globe can finally be installed as a nuget package!

Do you feel like you're dying a little bit every time you need to click "Options" and then "View on Website"? Do you also miss the old "Globe" in...

Tomas Hensrud Gulla | Feb 14, 2025 | Syndicated blog

Cloudflare Edge Logs

Optimizely is introducing the ability to access Cloudflare's edge logs, which gives access to some information previously unavailable except throug...

Bob Davidson | Feb 14, 2025 | Syndicated blog

Comerce Connect calatog caching settings

A critical aspect of Commerce Connect is the caching mechanism for the product catalog, which enhances performance by reducing database load and...

K Khan | Feb 14, 2025

CMP DAM asset sync to Optimizely Graph self service

The CMP DAM integration in CMS introduced support for querying Optimizly Graph (EPiServer.Cms.WelcomeIntegration.Graph 2.0.0) for metadata such as...

Robert Svallin | Feb 13, 2025