Write an asp.net core endpoint that behaves differently for different sites with Sitecore MVP site as example

Close in heels with my previous article, this  is another basic article to show how to trigger different behaviours using same asp.net core api controller method while accessing from two  different site instances. Here is a diagram summarising the intent:


As prerequisites, if you want to setup a Docker multi-site environment using the Sitecore MVP Site, then perform the following:

Pull latest Sitecore MVP Github code and run .\up.ps1 -IncludeSugconSites:$true since IncludeSugconSites is a switch parameter.

After successful execution, you will be able to see both mvp-rendering and sugcon-eu-rendering containers up and running as follows:


Then, after usual rituals of content serialisation and publish (all taken care by up.ps1 script), you should be able to access both sugcon-eu and mvpsite through the following urls:

Home | SUGCON (sc.localhost)


Sitecore MVP - Home (sc.localhost)


Now, the next step is to create one endpoint but access it from the front-end by passing different values. This will be useful to trigger different logic like, generation of separate sitemap for each site! 

For this, under \src\Feature\Navigation\renderingMvp.Feature.Navigation.Rendering.csproj, create NavigationController.cs within Controllers folder and add the following method:

using Microsoft.AspNetCore.Mvc;

namespace Mvp.Feature.Navigation.Controllers

{

    public class NavigationController : Controller

    {

        [HttpGet]

        public IActionResult SitemapData(string siteName)

        {

            return Json(new { siteName });

        }

    }

}


Then, create StartupExtensions.cs under \src\Feature\Navigation\rendering\Extensions and configure the above endpoint so that this controller can be plugged-in the startup.cs so that the application will know there is this new endpoint:

using Microsoft.AspNetCore.Builder;

namespace Mvp.Feature.Navigation.Extensions

{

    public static class StartUpExtensions

    {

        public static void UseFeatureSitemap(this IApplicationBuilder app,string siteName)

        {

            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllerRoute(

                      name: "sitemapdata",

                      pattern: "navigation/sitemapdata/{sitename}",

                      constraints: new { sitename = siteName },

                      defaults: new { controller = "Navigation", action = "SitemapData" }

                 );


            });

        }

    }

}


In the above method, note the use of constraints to pass on the value using siteName argument that in turn gets its value from the url! Constraints help to restrict values passed and regex can be used here!

Next, there are two startup.cs files wherein this method must be registered:

1. \src\Project\MvpSite\rendering\Mvp.Project.MvpSite.Rendering.csproj


2. \src\Project\Sugcon\rendering\Mvp.Project.Sugcon.Rendering.csproj


That's it, fire the browser and access the following endpoints to see the end-result:

https://mvp.sc.localhost/navigation/sitemapdata/mvpsite


https://sugcon-eu.sc.localhost/navigation/sitemapdata/sugcon


Now, there is definitely scope to improve this in terms of triggering the method just based on domain name but, the intent here is to clearly prove that the value passed from front-end reaches the controller method!

Comments

Popular Posts