Bro*_*ato 11 c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing
我有一个VS2012 MVC4解决方案,我测试Web API控制器.
我成功测试了GET,POST,PUT但DELETE仍然给我一个http 404错误.当我在api控制器中的'DeleteMovie'动作中设置断点时,永远不会到达断点.
我读了很多关于这个问题的帖子,但没有人帮助过我.
这是我的DELETE API控制器:
[HttpDelete]
public HttpResponseMessage DeleteMovie(int id)
{
// Delete the movie from the database
// Return status code
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
Run Code Online (Sandbox Code Playgroud)
这是我的html页面:
<script type="text/javascript">
deleteMovie(1, function ()
{
alert("Movie deleted!");
});
function deleteMovie(id, callback) {
$.ajax({
url: "/api/Movie",
data: JSON.stringify({ id: id }),
type: "DELETE",
contentType: "application/json;charset=utf-8",
statusCode: {
204: function () {
callback();
}
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的经典路线如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
我的API路由如下:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
在我的解决方案属性中,我配置了"使用本地IIS Web服务器"并选中了"使用IIS Express"
我也试过'使用Visual Studio开发服务器'但同样的问题.
任何的想法?
谢谢.
小智 40
如果您收到的错误是来自IIS的html内容类型,则错误404.0
确保您的web.config中包含Web Api模板添加的部分.默认情况下,IIS不会提供DELETE谓词,此配置会覆盖该行为.
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10051 次 |
最近记录: |