我已经在Google和StackOverflow上检查了所有关于ASP.NET Web API中405方法不允许的答案,但是没有一个解决方案能够解决问题。
- 检查CORS
- 检查WebDAV
- 检查HTTPDelete属性
我正在创建一个ASP.NET Web API,并有两个虚拟控制器。
我能够在一个控制器中使用HTTP Delete方法,但在另一个控制器中却不能。
Value Controller
using System.Collections.Generic;
using System.Web.Http;
namespace JobSite_WebAPI.Controllers
{
public class ValuesController : ApiController
{
List<string> strings = new List<string>()
{
"value1", "value2","value3"
};
// GET api/values
public IEnumerable<string> Get()
{
return strings;
}
// GET api/values/5
public string Get(int id)
{
return strings[id];
}
// DELETE api/values/5
public void Delete(int id)
{
strings.RemoveAt(id);
}
}
}
工作详情控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DataAccess;
namespace JobSite_WebAPI.Controllers
{
public class JobDetailController : ApiController
{
public JobDetailController()
{
JobSiteEntities entities = new JobSiteEntities();
entities.Configuration.ProxyCreationEnabled = false;
}
public IEnumerable<JobDetail>Get()
{
using (JobSiteEntities entities = new JobSiteEntities())
{
return entities.JobDetails.ToList();
}
}
[HttpGet]
public HttpResponseMessage Get(int id)
{
using (JobSiteEntities entities = new JobSiteEntities())
{
var entity = entities.JobDetails.FirstOrDefault(e => e.JOBID == id);
if (entity != null)
{
return Request.CreateResponse(HttpStatusCode.OK, entity);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Job With Id = " + id.ToString() + " not found");
}
}
}
[HttpGet]
public HttpResponseMessage RetrieveJobByLocation(string locationName)
{
try
{
using (JobSiteEntities entities = new JobSiteEntities())
{
IEnumerable<JobDetail> jobDetails = entities.JobDetails.Where(e => e.Location.LocationName.ToLower() == locationName).ToList();
if (jobDetails != null)
return Request.CreateResponse(HttpStatusCode.OK, jobDetails);
else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
catch(Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
[HttpDelete]
public HttpResponseMessage Delete(int jobId)
{
try
{
using (JobSiteEntities entities = new JobSiteEntities())
{
var entity = entities.JobDetails.FirstOrDefault(e => e.JOBID == jobId);
if (entity == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound, "Job Id with id " + jobId + "is not found");
}
else
{
entities.JobDetails.Remove(entity);
entities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
}
}
WebAPIConfig.cs
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
Web.Config
<remove name="WebDAV" />
<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" />
我已启用了CORS,禁用了WebDAV,还向我的删除方法添加了HTTPDelete属性。
我遇到的问题是Value Controller Delete方法在Fiddler中正常工作,但对于JobDetails Controller,我会收到一个405方法不允许的错误。
我能够使用GET和POST方法。在PUT方面,我也遇到了同样的问题。
从Fiddler中添加了错误消息截图。 Fiddler URL Fiddler错误消息
{id}
占位符,而JobDetailController.Delete(int jobId)
由于jobId
的原因与路由模板不匹配。请将这些参数更改为int id
,以便它与路由模板匹配。 - Nkosi