I have a .NET MVC RESTful API that works fine for GET and POST, but returns 404 for PUT requests:
[Authorize]
public class TasksController : ApiController
{
// GET api/tasks
/// <summary>
/// Get all users tasks.
/// </summary>
/// <returns>Task Object (JSON serialised)</returns>
public IEnumerable<Task> Get()
{
List<Task> tasks = new List<Task>();
...
return tasks;
}
// GET api/tasks/5
public Task Get(Int64 id)
{
Task thisTask = new Task();
...
return thisTask;
}
// POST api/tasks
public void Post(Task item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
...
}
// PUT api/tasks/5
public void Put(Int64 id, Task item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
...
}
// DELETE api/tasks/5
public void Delete(int id)
{
...
}
// PUT, GET, POST, DELETE api/tasks...
[AllowAnonymous]
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
return response;
}
}
Any idea why it wouldn't be picking up the PUT? (Even OPTIONS works fine)
Routing:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
// CORS Enabled
//var cors = new EnableCorsAttribute("localhost", "*", "*");
//config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
Web.Config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Authorization, Content-Type" />
</customHeaders>
</httpProtocol>
Addendum It looks like the speed at which it is coming back is instant, even after a recompile, So I'm guessing it's not even getting to the application, so must be a configuration issue.