I'm having some troubles configuring WebApi routes in an asp.net WebForms app.
In the global.asax file, I have something like this:
void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
I have another file that has this code:
using System.Web.Http;
namespace WebConfig
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
}
}
Then, in the root, I have a folder called api and in that folder I have a file that contains a class that looks somewhat like that:
public class MyController : ApiController
{
[Route("MyController")]
[HttpPost]
public HttpResponseMessage Post([FromBody]string value)
{....}
And then finally, in my client code, I have this ajax call:
$.ajax({
url: "/api/MyController",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
cache: "false",
data: someData});
However, when I run this code, all I get is a 404 error. What do I need to change in the routing mechanism to make this work?