gpt4 book ai didi

c# - Web API + jQuery AJAX DELETE 请求返回 404

转载 作者:太空狗 更新时间:2023-10-29 20:09:52 26 4
gpt4 key购买 nike

当我请求 PUT 或 DELETE 时,我的 ASP.NET WebAPI 和 MVC 应用程序返回 404 错误。它曾经返回 405,但我通过启用 CORS 解决了这个问题。我尝试了各种不同的解决方案(禁用 WebDAV、更改路由、将查询字符串放入请求中),但似乎没有一个对我有用。我希望我只是错过了一些非常简单的事情。以下是我的应用程序中每个相关文件的相关简化代码:

jQuery AJAX 请求:

$.ajax({
url: "api/Signout?id=3",
type: "DELETE",
crossDomain: true,
});

SignoutController(GET 和 POST 方法在这里工作得很好):

public void Delete([FromUri] int id)
{
//Do things
}

WebApiConfig 路由:

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

//For another part of the application
config.Routes.MapHttpRoute(
name: "SaveSignout",
routeTemplate: "api/{controller}/{signout}"
);

网络配置:

<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<modules>
<remove name="FormsAuthenticationModule" />
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"
path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
modules="IsapiModule"
scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
preCondition="classicMode,runtimeVersionv4.0,bitness64"
responseBufferLimit="0" />
</handlers>
</system.webServer>

RouteConfig.cs(在其他地方看到过)

routes.IgnoreRoute("{*x}", new { x = @".*\.asmx(/.*)?" }); 

Fiddler DELETE 请求(简化​​的 referer):

DELETE /api/Signout?id=45 HTTP/1.1
Host: localhost:51301
Connection: keep-alive
Cache-Control: no-cache
Authorization: Negotiate (large base64 here)
Pragma: no-cache
Accept: */*
Origin: http://localhost:51301
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Referer: http://localhost:51301/Home/Controller/Id
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

fiddler 响应:

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B? (base64 of full local path to api/Signout)
Persistent-Auth: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
WWW-Authenticate: Negotiate oRswGaADCgEAoxIEEAEAAABDh+CIwTbjqQAAAAA=
Date: Tue, 17 Feb 2015 18:05:18 GMT
Content-Length: 4966

这只是我遇到的一系列各种“解决方案”,显然对相关人员都有效。我哪里错了?

最佳答案

默认情况下,IIS 不处理 DELETE 请求:Web.Config 中定义的 system.webServer 处理程序可以设置当您的网站的应用程序池有一个“集成 (IIS7) 或经典(ISAPI 32 和 64 位版本)的托管管道模式”。在您的示例中,仅控制 ISAPI 64 位。以下显示了其他变体。

<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>

引用:

关于c# - Web API + jQuery AJAX DELETE 请求返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com