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!
You are mixing up frameworks. [Route("myapi/[controller]/[action]")]
is for asp.net-core while your code appears to be for asp.net-web-api
A few suggested changes.
Module code can be simplified
GlobalConfiguration.Configure(RegisterRoutes.Load);
Since attribute routing is configured
config.MapHttpAttributeRoutes();
Use the correct attributes on the APIController
and action
[RoutePrefix("myapi/Authorize")]
public class AuthorizeController : ApiController {
[HttpGet, ActionName("User")]
[Route("user")] //GET myapi/authorize/user
[AllowAnonymous]
public IHttpActionResult GetUser() {
WebUser user = new WebUser();
user.Key = Guid.Empty.ToString();
return Ok(user);
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.