Enabling Custom WebApi in Sitecore 9 CD Server without api/sitecore


Accessing custom webApi from CD server without using the api/sitecore url is not complex. Just a pipeline config and a routeConfig code will all enable the webapi for your CD server easily.

 

Create a CS File for custom Routing

 

namespace Vikash.Web.Feature.WebApi.Pipelines
{
public class RegisterCustomRoutes
{
public void Process(PipelineArgs args)
{
const string solutionName = “Vikash.Web.Feature.WebApi”;

RouteTable.Routes.MapRoute(
name: $”{solutionName}.Api”,
url: $”MyApi/Custom/UnSubscribe/{{action}}/{{id}}”,
defaults: new { controller = “UnSubscribe”, id = UrlParameter.Optional },
namespaces: new[] { $”Vikash.Web.Feature.WebApi.Controllers” }
);
}
}
}

Now we need to create a custom pipeline config to register the Route.

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”>
<sitecore>
<pipelines>
<initialize>
<processor type=”Vikash.Web.Feature.WebApi.Pipelines.RegisterCustomRoutes, Vikash.Web.Feature.WebApi”
patch:before=”processor[@type=’Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc’]” />
</initialize>
</pipelines>
</sitecore>
</configuration>

Now lets create our controller and start accessing the same the method via webAPI.

 

 

 

Have any Question or Comment?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discussion Topics