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

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.

 

 

  https://xchangesitecore.com/enabling-custom-webapi-in-sitecore-9-cd-server-without-api-sitecore/ Vikash Raaj

Comments

Popular posts from this blog

Razl for Sitecore

Sitecore Rocks web Service 403 Forbidden error

Multi Single Line Text Custom Field Plugin