Search code examples
c#asp.net-web-apiasp.net-mvc-routing.net-4.5

404 Errors for WebAPI - Registering routes in HttpModule not working


I am trying to add WebAPI endpoints to an existing forms application but I am getting 404 errors.

I cannot use the Global.asax Application_Start() to register the api routes as suggested by Microsoft here because the current application already has a compiled customization of the Global class which inherits from HttpApplication and they did not mark any of the methods as virtual. doh!

I am trying to load the routes using an HttpModule. I am getting 404 errors for the following URL: https://example.com/webapplication/myapi/Authorize/User

Module code:

using System;
using System.Web;
using System.Web.Http;

public class MyHttpModule : IHttpModule
{
    private static bool HasAppStarted = false;
    private readonly static object _syncObject = new object();

    public void Init(HttpApplication context)
    {
        //https://stackoverflow.com/a/2416546/579148
        if (!HasAppStarted)
        {
            lock (_syncObject)
            {
                if (!HasAppStarted)
                {
                    GlobalConfiguration.Configure(config => RegisterRoutes.Load(config));
                    HasAppStarted = true;
                }
            }
        }
    }

    #region IDisposable Implementation
    #endregion
}

My registration class is in a standalone library:

using System.Web.Http;

public static class RegisterRoutes
{
    public static void Load(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute("DefaultApi", "myapi/{controller}");
    }
}

And my controller:

using System;
using System.Web.Http;
using MyLibrary.Models;

[Route("myapi/[controller]/[action]")]
public class AuthorizeController : ApiController
{
    [HttpGet, ActionName("User")]
    [AllowAnonymous]
    public WebUser GetUser()
    {
        WebUser user = new WebUser();
        user.Key = Guid.Empty.ToString();
        return user;
    }
}

And finally the web.config:

<configuration>
  <!--...-->
  <system.webServer>
    <!--...-->
    <modules>
      <add name="MyModule" type="MyLibrary.MyHttpModule" />
    </modules>
    <!--...-->
  </system.webServer>
  <!--...-->
<configuration>

The webapplication is its own IIS application (its own Global.asax and web.config). I am on Windows 10, CLR version v4.0, and Managed Pipeline is Integrated.

I've tried several other options described here, here, and here but have not had anything but 404s.

TIA!


Solution

  • Turns out the problem was in the MapHttpRoute() method call. It seems that the routing does not like not having a value for the defaults and constraints parameters. I updated the map call to this:

    config.Routes.MapHttpRoute(
        name: "DefaultApi", 
        routeTemplate: Constants.ApiBaseRoute + "/{controller}/{action}",
        defaults: new {  }, 
        constraints: new {  }
    );
    

    I also had to add the action template parameter. And I removed the route attributes on the controller.