我有一个.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;
}
}
知道为什么它不会拿起PUT? (即使OPTIONS工作正常)
路由:
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>
附录即使在重新编译之后,它看起来的速度也是即时的,所以我猜它甚至没有进入应用程序,因此必须是配置问题。
答案 0 :(得分:3)
确定。事实证明这是由于这个被注释掉了:
<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>
...在web.config中,我最初做的是能够使用共享主机。 Do'h!