asp.net-web-api – ASP.NET Web API – 404删除
|
我试图在Web API控制器上实现一个Delete方法.但是,我总是得到404 – 找不到.在这一点上,我有GET,POST和PUT方法工作正常.我一直在阅读一些关于同样问题的其他SO帖子 – 没有一个是正常的. 控制器动作 public virtual HttpResponseMessage Delete(string customerId)
{
adapter.RemoveCustomer(customerId);
return Request.CreateResponse(HttpStatusCode.OK,"The customer was deleted.");
}
AJAX请求 function remove(customer,success,error) {
var url = '/api/Customer';
var data = JSON.stringify({ 'customerId': customer.CustomerId });
$.ajax({
url: url,type: 'DELETE',data: data,contentType: 'application/json'
})
.done(function (data,textStatus,handler) {
success(data);
})
.fail(function (handler,errorThrown) {
error(errorThrown);
});
};
Web.Config 这是我的web.config文件.除了模块部分,一切都与创建项目时相同: <system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<!--<modules runAllManagedModulesForAllRequests="true"></modules>-->
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<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.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我正在使用IIS Express,但如果我切换回Visual Studio Development Server,仍然会出现此问题. 原始HTTP 以下是Fiddler捕获的原始HTTP请求: DELETE http://localhost:63654/TestMvcApplication/api/Customer HTTP/1.1
Host: localhost:63654
Connection: keep-alive
Content-Length: 49
Accept: application/json,text/javascript,*/*; q=0.01
Origin: http://localhost:63654
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:63654/TestMvcApplication/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
{"customerId":"e107e2dc20834545ae209849bff195f0"}
这里是回应: HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcdHBhcmtzXERvY3VtZW50c1xHaXRIdWJcVGVzdE12Y0FwcGxpY2F0aW9uXFRlc3RNdmNBcHBsaWNhdGlvblxhcGlcQ3VzdG9tZXI=?=
X-Powered-By: ASP.NET
Date: Tue,02 Jul 2013 13:11:52 GMT
Content-Length: 220
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:63654/TestMvcApplication/api/Customer'.","MessageDetail":"No action was found on the controller 'Customer' that matches the request."}
这是一个开源的project,用于教我自己.我已经检查了最新的情况,如果有人想看到完整的来源. 解决方法public virtual HttpResponseMessage Delete(string customerId) 参数是一个简单的类型,并且来自URI,而不是来自请求体.或者将查询字符串中的客户ID传递给http:// localhost:63654 / TestMvcApplication / api / Customer?customerId = 123或将签名更改为公共虚拟HttpResponseMessage Delete(string id)并使用URI http:// localhost :63654 / TestMvcApplication / API /客户/ 123. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – VS2012启用NuGet包恢复消失,缺少
- asp.net-mvc – 你如何指定在列表框中显示多少项目(高度)
- asp.net操作xml增删改示例分享
- asp.net – 将appsettings.production.json发布到azure上
- asp.net-mvc – 使用ViewModel模式与MVC 2强类型的HTML助手
- asp.net-mvc-3 – 在Asp.net MVC中为optgroup功能使用Helpe
- ASP.Net MVC Script Bundle导致404
- 编辑并在ASP.NET Web项目中继续
- asp.net – ASP:登录总是生成一个,我怎么能让它停止?
- 在ASP.NET中的Web.Config中模拟标签
- .net – Autofac和IDisposable界面
- asp.net-web-api – Asp.net Web Api的基本项目模
- 在ASP.NET应用程序的global.asax中处理Applicati
- 在ASP.NET中如何识别/处理404异常?
- asp.net – 从Application_BeginRequest()中设置
- asp.net – IE 11中的报表查看器打印按钮
- 实体框架 – 等同于.HasOptional在实体框架核心1
- asp.net-mvc – 部署的ASP.NET MVC 4项目不会运行
- asp.net – .NET Developer的Vagrant文件(具有Wi
- 在ASP.Net网站上实现自定义错误页面
