Hallo, ich versuche, den Datensatz (HttpDelete) zu löschen. Die Methode in der Steuerung löst nicht aus und 405 Method Not Allowed error error.WEB API2/HTTP DELETE löst nicht aus - 405 Fehler
jquery unten
function deleteContract(contractId) {
var contract = new Object();
contract.ContractID = "ContractId";
$.ajax({
async: true,
type: 'DELETE',
data: contract,
dataType: "json",
url: 'http://localhost:4233/api/Contract/DeleteContractById/' + contractId,
}).done(function (data, textStatus, xhr) {
alert("deleted successfully")
}).error(function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText || textStatus);
})
}
Controller unten
// DELETE: api/Contract/5
[ResponseType(typeof(Contract))]
[AllowAnonymous]
[HttpDelete]
[ActionName("DeleteContractById")]
//[Route("api/Contract/{id}")]
[AcceptVerbs("DELETE")]
public HttpResponseMessage DeleteContract(Guid id)
{
Contract contract = db.Contract.Find(id);
if (contract == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Strata.Remove(contract);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, contract);
}
webapiconfig unten
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "GET", id = RouteParameter.Optional }
);
}
Web Config unten
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<handlers>
<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" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
</customHeaders>
</httpProtocol>
</system.webServer>
Wenn ich den Anruf mit Fiddler mache funktioniert es gut. Lass es mich wissen, wenn ich irgendwelche Einstellungen im Code verpasst habe.
Dank Dev
Mögliche Duplikate von [ASP.NET-Web-API - VERBINDEN UND LÖSCHEN Verben nicht zulässig - IIS 8] (http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs- not-allowed-iis-8) – Luke
Bitte werfen Sie einen Blick auf diese bestehenden Fragen: http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis- 8 http: // Stapelüberlauf.com/questions/15619075/webapi-delete-not-working-405-Methode-nicht-erlaubt http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed -iis-8 – Luke
Ich hatte dieses Problem bereits zuvor und ich _disabled_ WebDav für den Anwendungspool, auf dem meine Website gehostet wurde. – Luke