Copied to clipboard

Flag this post as spam?

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


  • Henrik Sunesen 84 posts 281 karma points
    May 10, 2022 @ 12:02
    Henrik Sunesen
    0

    Custom routing on specific URL

    Hi

    I have some problem making custom routing on specific urls like:

    /case.aspx?branch=2736&casenumber=2736&utm_campaign=google&utm_source=google.dk&utm_medium=exitlinks
    

    In Umbraco 8 I just make the routeconfig use a method in a custom controller. But I have some issues accessing the umbraco context when I trying that in Umbraco 9.

    My controller:

     public class RoutingController : SurfaceController
    {
        private IEnumerable<IPublishedContent> umbRoot;
    
        public RoutingController(
           IUmbracoContextAccessor umbracoContextAccessor,
           IUmbracoDatabaseFactory databaseFactory,
           ServiceContext services,
           AppCaches appCaches,
           IProfilingLogger profilingLogger,
           IPublishedUrlProvider publishedUrlProvider)
           : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            umbRoot = umbracoContextAccessor.GetRequiredUmbracoContext().Content.GetAtRoot();
    
        }
    
    
        public IActionResult HandleCustomUrl([FromQuery] string branch, [FromQuery] string casenumber, [FromQuery] string utm_campaign, [FromQuery] string utm_source, [FromQuery] string utm_medium)
        {
            var allCases = umbRoot.Where(x => x.IsDocumentType("cases")).FirstOrDefault();
            var targetCase = allCases.Children.Where(x => x.Value<string>("caseID").Equals(casenumber)).FirstOrDefault();
    
    
            return RedirectToUmbracoPage(targetCase);
        }
    
    }
    

    My startup.cs:

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.EndpointRouteBuilder.MapControllerRoute(
                      "Routing Controller",
                      "/case.aspx",
                      new { Controller = "Routing", Action = "HandleCustomUrl" });
    
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
        }
    

    I hope someone can point me in the right direction here :)

Please Sign in or register to post replies

Write your reply to:

Draft