原文地址: 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的更多相关文章

  1. ASP.NET MVC 5 - 查询Details和Delete方法

    在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...

  2. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...

  3. ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解

    原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...

  4. 【译】ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解

    原文:[译]ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details ...

  5. [整理]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 ...

  6. [转]ASP.NET MVC 5 - 查询Details和Delete方法

    在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...

  7. http协议 put、delete请求asp.net mvc应用,报404错误

    http协议 put.delete请求asp.net mvc应用,报404错误 更改web.config,在<modules>节点中设置 runAllManagedModulesForAl ...

  8. 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 ...

  9. 返璞归真 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 新特性 ...

随机推荐

  1. Exif xss

    这种XSS出现的状况会特别少. Exif是啥??? 可交换图像文件格式(英语:Exchangeable image file format,官方简称Exif),是专门为数码相机的照片设定的,可以记录数 ...

  2. Python图片验证码降噪 — 8邻域降噪

    简介 图片验证码识别的可以分为几个步骤,一般用 Pillow 库或 OpenCV 来实现,这几个过程是: 1.灰度处理&二值化 2.降噪 3.字符分割 4.标准化 5.识别 所谓降噪就是把不需 ...

  3. Word Ladder I & II

    Word Ladder I Given two words (start and end), and a dictionary, find the length of shortest transfo ...

  4. textarea保留换行和空格

    <style> pre {white-space: pre-wrap;} </style> //替换textare <pre class="feedback_q ...

  5. 阿里云slb+https 实践操作练习

    如果只是练习按照文档步骤逐步执行即可. 如果是业务需要,只供参考. 有道笔记链接->

  6. MongoDB安全:创建第1个、第2个、第3个用户

    Windows 10家庭中文版,MongoDB3.6.3, 前言 使用mongod命令基于某个空白文件夹(存放数据)启动MongoDB服务器时,要是没有使用--auth选项,启动后,任何客户端是可以无 ...

  7. tf.metrics.accuracy ==>坑货

    tf.metrics.accuracy输出两个值,第一个值为上几步的平均精度,第二值是上几步与该步的精度的平均值. 正常的计算单个batch正确率的代码 self.correct_prediction ...

  8. 微信 JS API 支付教程

    最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了. 用 ...

  9. Java 泛型和类型安全的容器

    使用java SE5之前的容器的一个主要问题就是编译器允许你向容器插入不正确的类型,例如: //: holding/ApplesAndOrangesWithoutGenerics.java // Si ...

  10. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...