HTTP 错误 405.0 - IIS Express 中不允许的方法 [英] HTTP Error 405.0 - Method Not Allowed in IIS Express

查看:18
本文介绍了HTTP 错误 405.0 - IIS Express 中不允许的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2013 下向本地 IIS Express 网站发出完美的cromulent 动词时:

CROMULENT http://localhost:7579/Handler.ashx HTTP/1.1主机:本地主机:7579

服务器响应错误:

HTTP/1.1 405 方法不被允许允许:获取、头部、选项、跟踪

这是对通用处理程序"(即.ashx)的请求.如果我再次尝试静态资源:

SCHWIFTY http://localhost:7579/Default.htm HTTP/1.1主机:本地主机:7579

服务器响应错误:

HTTP/1.1 405 方法不被允许允许:获取、头部、选项、跟踪

这完全是为了尝试使用 HTTP 动词:

DELETE http://localhost:7579/Handler.ashx HTTP/1.1主机:本地主机:7579HTTP/1.1 405 方法不允许允许:获取、头部、选项、跟踪PUThttp://localhost:7579/Handler.ashx HTTP/1.1主机:本地主机:7579HTTP/1.1 405 方法不允许允许:获取、头部、选项、跟踪

这个问题已经问到死了,有时是我.但从来没有人想出解决办法.

微软的错误

从根本上说,问题在于 Microsoft 提供的 IIS 和 IISExpress 在默认情况下已损坏.他们不处理动词,而不是像网络服务器那样处理 HTTP 动词.

当管理在 Windows Server 上运行的完整 IIS 时,最容易看到这一点.选择任何内置处理程序(例如 cshtml 处理程序),您会看到有人认为如果它只与 GET 一起使用会搞笑HEADPOSTDEBUG 动词:

而不是在 HTTP 服务器中正确实现对 HTTP 的支持.

问题变成:

  • 为什么完全不起作用
  • 如何完全修复它
  • 如何在 IIS Express 中修复它(无需任何管理工具)
  • 为什么它年复一年地继续发货,坏了

问题 1. 为什么它不起作用?

第一个问题是为什么它不起作用.让我们看看一个 IIS 服务器,我们已经删除了除基本的静态文件处理程序之外的所有处理程序:

处理程序配置为所有所有动词:

剩下的唯一处理程序设置为允许任何动词.但是,如果我们向 Web 服务器发出请求,则会收到错误消息:

DELETE http://scratch.avatopia.com/HTTP/1.1主持人:scratch.avatopia.comHTTP/1.1 405 方法不允许允许:获取、头部、选项、跟踪服务器:Microsoft-IIS/7.5

<块引用>

为什么会这样?

为什么它不起作用?配置选项在哪里说:

  • GET
  • HEAD
  • 选项
  • TRACE

因为服务器本身说这些是唯一支持的动词.

然而,如果我们将其更改为 GET,它就可以正常工作:

GET http://scratch.avatopia.com/HTTP/1.1用户代理:提琴手主持人:scratch.avatopia.comHTTP/1.1 200 正常内容类型:文本/html

问题 2. 如何解决?

通常的做法是删除WebDAV.没有人知道 WebDAV 是什么,它怎么会是一个问题,它为什么会是一个问题,或者如果它只会引起问题,它为什么会存在.WebDAV 可以作为处理程序从 IIS 管理用户界面中删除:

这与从 web.config(UI本身会为您添加 web.config 条目):

<处理程序><删除名称="WebDAV"/></处理程序></system.webServer>

除非这不起作用.那么我们如何修复它?

从 IIS 开始,WebDAV 似乎变得更像是一种病毒.而不是简单地将其禁用为处理程序,您必须将其作为模块完全安装或删除:

<模块><remove name="WebDAVModule"/></模块><处理程序><删除名称="WebDAV"/></处理程序></system.webServer>

这听起来像是一个合理的想法,除了在我的测试用例中,在 IIS 7.5 上,WebDAV 既没有安装,也没有作为模块删除:

HTTP/1.1 405 方法不被允许允许:获取、头部、选项、跟踪服务器:Microsoft-IIS/7.5X-Powered-By: ASP.NET日期:2016 年 5 月 10 日,星期二 19:19:42 GMT内容长度:0

所以,如果我们能弄清楚如何解决这个问题,我们就可以回答第二个问题.

问题 3. 如何在 IIS Express 中修复它

从 Visual Studio 20131 开始,Visual Studio 不再使用名为 Cassini 的小型网络服务器.它使用 IIS Express 本身的便携式安装(即不需要安装 IIS Express Windows 功能).

上述大部分修复(尝试)(失败)在 IIS 中.但是没有人在 Visual Studio 2013 的 IIS Express 中处理过它们(这就是这个问题与其他问题的不同之处).

问题 4. 为什么会出现这种情况?

15 年过去了,这种情况仍在发生.IIS 不能用作 Web 服务器肯定有好的原因.但它是什么?我找不到任何知识库文章或博客文章来解释 IIS 团队拒绝正常运行的原因.

奖励阅读

研究工作

解决方案

我似乎对这个问题感到有些沮丧,所以 实际 问题有点不清楚.您正在尝试但失败的具体内容是什么?您对答案有何期待?

无论如何,基于问题中的评论:

<块引用>

这完全是为了尝试使用 HTTP 动词:

以及涉及通用处理程序的相应示例,我将尝试展示使PUTDELETE 成为通用处理程序所需的条件.

为了在通用处理程序上允许 PUTDELETE,您必须在应用程序的 web.config 中允许它.为此,您应该为 *.ashx 注册一个处理程序,如下所示:

<处理程序><add name="SimpleHandlerFactory-Integrated-WithPutDelete"路径="*.ashx"动词="GET,HEAD,POST,DEBUG,PUT,DELETE"type="System.Web.UI.SimpleHandlerFactory"资源类型 =未指定"requireAccess="脚本"preCondition="integratedMode"/></处理程序></system.webServer>

根据您最初设置网站/应用程序的方式,您的 web 中可能有也可能没有为 type="System.Web.UI.SimpleHandlerFactory" 注册的处理程序.config 文件.如果有,您应该能够修改该条目并添加您想要允许的动词.

您会注意到该条目具有 preCondition="integratedMode".我相信,在使用 IIS Express 在 Visual Studio 中进行调试时,这应该可以工作.在实际的 IIS 部署中,可能需要修改处理程序注册以匹配将运行应用程序的应用程序池.对于以经典模式(未集成)运行的应用程序,它看起来像这样(未经测试,因此可能是错误的):

<处理程序><add name="SimpleHandlerFactory-ISAPI-4.0_64bit-WithPutDelete"路径="*.ashx"动词="GET,HEAD,POST,DEBUG,PUT,DELETE"模块 =IsapiModule"scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll"preCondition="classicMode,runtimeVersionv4.0,bitness64"responseBufferLimit="0"/></处理程序></system.webServer>

确切的细节将取决于框架版本和应用程序池的位.

如果您正在使用 IIS Express 在 Visual Studio 中进行调试,您应该查看 applicationhost.config,它设置了许多关于 IIS Express 的默认值.它位于:

我的文档IISExpressconfig

上述经典管道应用程序池的未经测试的处理程序注册是对该文件中的处理程序注册的轻微修改.在我的环境中,*.ashx 有 6 个单独的条目,在该文件中具有不同的先决条件.

如果您想拥有自己的允许 PUTDELETE 的注册,最好在 web.config 中明确删除所有这些.并非所有这些都实际上同时处于活动/注册状态,因为前提条件(我想)是相互排斥的,但至少对我来说,将它们一个接一个地全部删除是可行的.在我的环境中,删除部分如下所示:

<remove name="SimpleHandlerFactory-ISAPI-4.0_32bit"/><remove name="SimpleHandlerFactory-Integrated-4.0"/><remove name="SimpleHandlerFactory-Integrated"/><remove name="SimpleHandlerFactory-ISAPI-2.0"/><remove name="SimpleHandlerFactory-ISAPI-2.0-64"/>

希望这至少能照亮黑暗的地方!

When issuing a perfectly cromulent verb to a local IIS Express web-site under Visual Studio 2013:

CROMULENT http://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579

the server responds with the error:

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE

That is a request to a "generic handler" (i.e. .ashx). If if i try again to a static resource:

SCHWIFTY http://localhost:7579/Default.htm HTTP/1.1
Host: localhost:7579

the server responds with the error:

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE

This is all by way to trying to use HTTP verbs:

DELETE http://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE

PUThttp://localhost:7579/Handler.ashx HTTP/1.1
Host: localhost:7579

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE

This question has been asked to death, sometimes by me. But nobody has ever come up with a solution.

</Question>

Microsoft's Bug

The problem, fundamentally, is that Microsoft ships IIS and IISExpress broken by default. Rather than handling HTTP verbs, as a web-server is required to do, they don't handle verbs.

This can most easily be seen when managing full IIS running on Windows Server. Pick any of the built-in handlers (e.g. the cshtml handler), and you can see that someone thought it would be hilarious if it only worked with GET, HEAD, POST, and DEBUG verbs:

rather than correctly implementing support for HTTP in an HTTP server.

The question becomes:

  • why exactly doesn't it work
  • how exactly to fix it
  • how to fix it in IIS Express (without any management tools)
  • why it continues to be shipped, year after year, broken

Question 1. Why doesn't it work?

The first question is why doesn't it work. Let's look at an IIS server where we've removed every handler except the basic Static file handler:

The handler is configured to all all verbs:

The only handler left is set to allow any verb. Yet if we issue a request to the web server we get the error:

DELETE http://scratch.avatopia.com/ HTTP/1.1
Host: scratch.avatopia.com

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Server: Microsoft-IIS/7.5

Why is this happening?

Why didn't it work? Where is the configuration option that says:

  • GET
  • HEAD
  • OPTIONS
  • TRACE

because the server itself is saying those are the only supported verbs.

Yet if we change it to a GET it works fine:

GET http://scratch.avatopia.com/ HTTP/1.1
User-Agent: Fiddler
Host: scratch.avatopia.com

HTTP/1.1 200 OK
Content-Type: text/html

Question 2. How to fix it?

The common wisdom is to remove WebDAV. Nobody knows what WebDAV is, how it could be a problem, why it is a problem, or why it exists if it only causes problems. WebDAV can be removed as a handler from the IIS administration user interface:

which is identical to adding a remove entry from the handlers section in web.config (the UI itself adds the entry to web.config for you):

<system.webServer>
    <handlers>
        <remove name="WebDAV" />
    </handlers>
</system.webServer>

Except this doesn't work. So how do we fix it?

Starting with IIS it seems that WebDAV has become even more of a virus. Rather than simply disabling it as a handler, you have to completely install or remove it as a module:

<system.webServer>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
  <handlers>
    <remove name="WebDAV" />
  </handlers>
</system.webServer>

That sounds like a reason idea, except in my test case, on IIS 7.5, WebDAV is both not installed, and removed as a module:

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Tue, 10 May 2016 19:19:42 GMT
Content-Length: 0

So, if we can figure out how to solve the problem, we can answer question number two.

Question 3. How to fix it in IIS Express

Starting with Visual Studio 20131, Visual Studio no longer uses a mini web-server called Cassini. It uses a portable install of IIS Express itself (i.e. IIS Express Windows feature doesn't need to be installed).

Most of the above fix (attempts) (fail) in IIS. But nobody has dealt with them in IIS Express of Visual Studio 2013 (which is how this question is different from any others).

Question 4. Why does this keep happening?

It's been over 15 years, and this still keeps happening. There must be a good reason why IIS does not function as a web-server. But what is it? I've not been able to find any knowledge base article, or blog post, explaining why the IIS team refuses to function correctly.

Bonus Reading

Research Effort

解决方案

I seem to pick up on a bit of frustration in the question, so the actual question is a bit unclear. What specifically is it that you are trying, but failing, to do? What do you expect of the answer?

Anyway, based on this comment in the question:

This is all by way to trying to use HTTP verbs:

and the corresponding samples involving a generic handler, I'll take a stab at showing what is needed to make it possible to PUT and DELETE a generic handler.

In order to allow PUT and DELETE on a generic handler, you must allow it in the web.config of the application. To do that you should register a handler for *.ashx as follows:

<system.webServer>
    <handlers>
        <add name="SimpleHandlerFactory-Integrated-WithPutDelete"
            path="*.ashx"
            verb="GET,HEAD,POST,DEBUG,PUT,DELETE"
            type="System.Web.UI.SimpleHandlerFactory"
            resourceType="Unspecified"
            requireAccess="Script"
            preCondition="integratedMode" />
    </handlers>
</system.webServer>

Depending on how you originally set up the web site/application, there may or may not be a handler registered for type="System.Web.UI.SimpleHandlerFactory" in your web.config file. If there is one, you should be able to just modify that entry and add the verbs you want to allow.

You'll note that this entry has the preCondition="integratedMode". This should, I believe, work when debugging in Visual Studio using IIS Express. In a real IIS deployment, the handler registration may need to be modified to match the application pool that will run the application. For an application running in classic mode (not integrated), it would look something like this (not tested so may be wrong):

<system.webServer>
    <handlers>
        <add name="SimpleHandlerFactory-ISAPI-4.0_64bit-WithPutDelete" 
            path="*.ashx" 
            verb="GET,HEAD,POST,DEBUG,PUT,DELETE" 
            modules="IsapiModule" 
            scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" 
            preCondition="classicMode,runtimeVersionv4.0,bitness64" 
            responseBufferLimit="0" />
    </handlers>
</system.webServer>

The exact details would depend on the framework version an bit-ness of the application pool.

If you are debugging in Visual Studio using IIS Express, you should have a look at the applicationhost.config which sets up a lot of the defaults regarding IIS Express. It is located in:

My DocumentsIISExpressconfig

The untested handler registration above for a classic pipeline application pool is a slight modification of a handler registration in that file. There are in my environment 6 separate entries for *.ashx, with varying preconditions, in that file.

It might be a good idea to explicitly remove all of these in your web.config if you want to have your own registration which allows PUT and DELETE. Not all of them would actually be active/registered at the same time since the preconditions are (I suppose) mutually exclusive, but at least for me it works to just remove them all, one after the other. In my environment the section with the removes looks like this:

<remove name="SimpleHandlerFactory-ISAPI-4.0_64bit"/>
<remove name="SimpleHandlerFactory-ISAPI-4.0_32bit"/>
<remove name="SimpleHandlerFactory-Integrated-4.0"/>
<remove name="SimpleHandlerFactory-Integrated"/>
<remove name="SimpleHandlerFactory-ISAPI-2.0"/>
<remove name="SimpleHandlerFactory-ISAPI-2.0-64"/>

Hope this shines at least a bit of light into dark places!

这篇关于HTTP 错误 405.0 - IIS Express 中不允许的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆