ASP.NET MVC HttpVerbs.Delete/Put Routes not firing
原文地址: https://weblog.west-wind.com/posts/2015/Apr/09/ASPNET-MVC-HttpVerbsDeletePut-Routes-not-firing?utm_source=tuicool&utm_medium=referral
国内:http://www.tuicool.com/articles/Zv2EbmY
A few times in the last weeks I’ve run into a problem where I found that DELETE operations would not fire in ASP.NET MVC controllers. I’ve been building APIs mostly with Web API until recently, but started using MVC instead with current projects in light of vNext which essentially uses the MVC model for ‘APIs’. And I ran into trouble each time with PUT and DELETE verbs not firing.
What’s the Problem?
To demonstrate here’s a simple action method on an MVC controller that uses Attribute routing for a delete operation:
[Route("albums/{id}")]
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeleteAlbum(int id)
{
var albumBus = new AlbumBusiness();
if (!albumBus.Delete(id, saveChanges: true, useTransaction: true))
throw new CallbackException("Couldn't delete album: " + albumBus.ErrorMessage);
return Json(true, JsonRequestBehavior.AllowGet);
}
When this route is fired I’m getting a 404 error from IIS – it’s not finding the route. However, if I change the route to a HttpVerbs.Get it runs just fine.
What the heck is happening here?
Missing Verbs on ExtensionlessUrlHandler
The main culprit is the ExtensionlessUrlHandler Http handler that’s responsible for handling MVC’s Controller and Attribute routing. The default entry for this handler is defined in ApplicationHost.config doesn’t include the DELETE or PUT verb.
Here’s what’s in my ApplicationHost.config which determines the default handler settings:
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,DEBUG"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0"
responseBufferLimit="" />
Note that PUT and DELETE are not among the supported verbs.
To fix this you can add the following to your application’s web.config file:
<configuration>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
And voila, PUT and DELETE now work. Yay!
ASP.NET MVC doesn’t, Web API does
It’s interesting to note that the problem above applies specifically to ASP.NET MVC projects. When you create a new MVC project there’s no custom handler registration made. So for MVC project or any project other than an API project you’ll have to manually add the handler – even if you add WebAPI features later.
If you create an ASP.NET WebAPI project you do get the following in the default web.config created by the new project template:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
For me this was probably the main reason for confusion – I expected it to ‘just work’ since I never had an issue with WebAPI. But clearly different default configuration settings are made for API vs MVC applications (so much for ‘One ASP.NET’).
In a way I suppose this makes sense – if you’re not building API applications PUT and DELETE are unlikely to be something needed, but still it’s confusing to have things work sometimes and not others.
Additional Issues: WebDav
If you’re running an MVC application and you run into this issue, most likely the ExtensionlessUrlHandler is the culprit. However, if that does not resolve the issue, there are a few other things to check:
If you have WebDav installed on your server/site, that causes a more restrictive URL/routing rules to be applied including removing the DELETE verb by default.
< system.webServer > < security > < requestFiltering > < verbs applyToWebDAV = " false " > <add verb = " DELETE " allowed = " true "
/>
< add verb = " PUT " allowed = " true " /> </ verbs > </ requestFiltering > </ security > </system.webServer >
If you are using WebDav as part of your application or it’s defined at the server root, you can add additional verbs to the RequestFiltering section to explicitly allow the verbs you’re interested in through.
Alternately if you want to disable WebDav in your specific application:
<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
If you’re not using WebDav as part of your application (but it’s defined at the server root) you can just remove the module and the restrictions should actually go away.
ASP.NET MVC HttpVerbs.Delete/Put Routes not firing的更多相关文章
- ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- ASP.NET MVC 学习8、Controller中的Detail和Delete方法
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...
- ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...
- 【译】ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解
原文:[译]ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details ...
- [整理]IIS 6.0 下部署 Asp.net MVC Web Api 后 HTTP PUT and DELETE 请求失败
http://guodong.me/?p=1560 ASP.NET MVC 4 has a new feature called WebAPI which makes it much easier t ...
- [转]ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- http协议 put、delete请求asp.net mvc应用,报404错误
http协议 put.delete请求asp.net mvc应用,报404错误 更改web.config,在<modules>节点中设置 runAllManagedModulesForAl ...
- LowercaseRoutesMVC ASP.NET MVC routes to lowercase URLs
About this Project Tired of your MVC application generating mixed-case URLs like http://mysite.com/H ...
- 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性
原文:返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 [索引页][源码下载] 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 ...
随机推荐
- Shell-脚本只能运行1次
用空文件进行判断 path=`pwd` if [ -f ${path}/.runned ]; then { echo "This script can only execute once! ...
- crond检查服务状态
代码如下: * */1 * * * /etc/init.d/ntpd status;if [ $? -ne 0 ];then /etc/init.d/ntpd start; fi
- MySQL 删除数据
删除数据的语句有三种:DELETE.DROP.TRUNCATE. 1.DELETE语句 DELETE 语句用于删除表中的行. 语法 DELETE FROM 表名称 WHERE 列名称 = 值 例如 - ...
- image配准,发布geoserver服务
1.arcmap配准:注:png只保留需要显示范围,多余部分删除,,配准后再进行栅格裁剪(为了去除偏移后出现的NoData值) 2.导出tif:注:NoData值设置,一般为256(有时候经过裁剪会默 ...
- java基础24 线程、多线程及线程的生命周期(Thread)
1.1.进程 正在执行的程序称作为一个进程.进程负责了内存空间的划分 疑问1:windows电脑称之为多任务的操作系统,那么Windows是同时运行多个应用程序呢? 从宏观的角度:windows确实在 ...
- Python基础 - 正则表达式
Python自带正则表达式模块,即re模块. 导入正则模块: import re 用dir()函数查看re模块内的属性和方法: dir(re)
- Linux 相关
一.WCHAN的含义 WCHAN 进程正在睡眠的内核函数名称:该函数的名称是从/root/system.map文件中获得的. 参考:解析ANDROID ps命令执行后各项参数的含义 二.查看线程 ps ...
- Java之杨辉三角的实现
今天突然想温习一下Java的基础,想了想就写写杨辉三角吧 1.直接法,利用二维数组 import java.util.Scanner; public class Second { public sta ...
- JS黑魔法之this, setTimeout/setInterval, arguments
最近发现了JavaScript Garden这个JS黑魔法收集处,不过里面有一些东西并没有说得很透彻,于是边看边查文档or做实验,写了一些笔记,顺手放在博客.等看完了You don't know JS ...
- mongo blancer
在 sharded cluster 体系结构中,Balancer 进程的作用是转移数据,当一个 shard 中的数据比其它 shard 的数据多并达到一定条件时,Balancer 进程触发. 为了减少 ...