[英]ASP.NET MVC API PUT getting 404 error
I have a .NET MVC RESTful API that works fine for GET and POST, but returns 404 for PUT requests: 我有一个.NET MVC RESTful API,适用于GET和POST,但对于PUT请求返回404:
[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? 知道为什么它不会接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: 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. 附录看起来,即使重新编译后,它返回的速度也是即时的,所以我猜想它甚至还没有到达应用程序,因此一定是配置问题。
Ok. 好。 So turns out it was due to having this commented out:
原来是由于对此进行了注释:
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
...in the web.config, which I originally did to be able to use shared hosting. ...在web.config中,我最初是为了能够使用共享托管而做的。 Do'h!
h!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.