Search code examples
c#asp.netasp.net-web-apikentico

Kentico v8.2.12: Why is my WebAPI Route not registered correctly (usually only on the first release)?


Summary

A Kentico 8.2 website fo which I have recently implemented a Web API service isn't registering routes on first deployment and all calls return 404. Further redeployments usually fix the issue, but I would like to fix it permanently before it is released to PROD.

What is preventing the first deployment from registering the route properly?

Background

We have a Kentico v8.2.12 website that uses Web Forms using .NET Framework v4. I have registered a Web API Controller, but it appears on the first release the route isn't registered and any calls to the service returns "404 (Not Found)".

When I first deployed to the DEV environment the Web API route wasn't registered, but upon deploying another build it magically worked. One or two other releases into the DEV environment caused similar issues, but in these instances re-deploying the same build worked.

The same issue has now occurred when released to UAT, however as the deployments are carried out by another team it will be more time-consuming to re-deploy builds and looks unprofessional. I am also wary of this occurring in PROD---which may cause the live website to be down further than necessary.

Web API Implementation

The Web API Controller is inside the CMS Website project and not a separate library.

Global.asax.cs

The Global.asax.cs file's Application_Start() method registers the route and looks similar to the below:

protected void Application_Start()
{
    // Scripts Bundling here, which havs been removed for brevity
    BundleTable.EnableOptimizations = false;

    // Registering the API
    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApiWithAction",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new {id = RouteParameter.Optional}
        );
}

MyController.cs

My Controller looks similar to the below stored under the CMS website: CMSApp/ApiControllers/MyController.cs

[assembly: RegisterApiController(typeof(CMSApp.ApiControllers.MyController))]

namespace CMSApp.ApiControllers
{
    public class MyController : ApiController
    {
        Channel Channel = new Channel();
    
        [HttpPost]
        public int Create()
        {
            Response objResponse = Channel.Instance.DoSomething();
            HandleResponse(objResponse);
            return objResponse.SessionHandle;
        }
    }
}

In the webbrowser, accessing /api/my/create returns a 404 (Not Found), but I expect it to tell me it's a POST method.

Lib versions [Edit, I have since updated the libs but issue still prevails]

  • Microsoft.AspNet.WebApi : v4.0.30506
  • Microsoft.AspNet.WebApi.Client : v4.0.30506
  • Microsoft.AspNet.WebApi.Core : v4.0.30506
  • Microsoft.AspNet.WebApi.WebHost : v4.0.30506

Question?

Why do the first deployments into an environment not work, but most further deployments work as I expect them to?


Solution

  • The issue was due to ASP.NET caching.

    Once "MS-ApiControllerTypeCache.xml" was removed under "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files" and IIS was restarted, the controller was picked up.