问题映射HttpHandler --> HTTP 错误 404 未找到

发布于 2024-09-01 15:29:07 字数 1070 浏览 5 评论 0原文

我在尝试在 web.config 中映射 HttpHandler 时遇到问题。

这是相关的配置位:

<httpHandlers>
  <add verb="*" path="*.hndlr" type="MyAssembly.MyHandler, MyAssembly" validate="false" />
</httpHandlers>

当我导航到 http://localhost/myApp/whatever.hndlr 时,我收到服务器错误 404(未找到)。

这是我第一次连接 HttpHandler,所以我可能会丢失一些东西 - 感谢任何帮助!

更新

到目前为止,我设法使用这两个答案使其正常工作 - 谁能够解释它为什么有效,就会得到答案标记!

这是我的配置(如果两者都没有,则不起作用 - 我在经典模式下运行IIS7

System.web:

<httpHandlers>
    <add verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false"/>
</httpHandlers>

System.webserver:

<handlers>
    <add name="MyHandler" verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script"/>
</handlers>

I am having problems trying to map an HttpHandler in the web.config.

This is the relevant config bit:

<httpHandlers>
  <add verb="*" path="*.hndlr" type="MyAssembly.MyHandler, MyAssembly" validate="false" />
</httpHandlers>

When I navigate to http://localhost/myApp/whatever.hndlr I am getting a server error 404 (not found).

It's the 1st time I am hooking up an HttpHandler so I might be missing something - any help appreciated!

UPDATE:

I managed to get it working using both answers so far - who's able to explain why it works gets the answer marked!

This is my config (won't work if you don't have both - I am running IIS7 in classic mode)

System.web:

<httpHandlers>
    <add verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false"/>
</httpHandlers>

System.webserver:

<handlers>
    <add name="MyHandler" verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script"/>
</handlers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

太阳哥哥 2024-09-08 15:29:07

您是否使用 IIS7,如果是,应用程序池是否以经典模式或管道模式运行?如果是管道模式下的 IIS7,则处理程序引用需要进入下一节

<system.webServer>
    <handlers>
    </handlers>
<system.webServer>

而不是下一节。

<system.web>
    <httpHandlers>
    </httpHandlers>
</system.web>

Are you using IIS7, if so is the application pool running in classic or pipelined mode? If it is IIS7 in pipelined mode then the handler reference needs to go into the following section

<system.webServer>
    <handlers>
    </handlers>
<system.webServer>

rather than in the following section.

<system.web>
    <httpHandlers>
    </httpHandlers>
</system.web>
沧笙踏歌 2024-09-08 15:29:07

作为那些遇到这个问题的人的指南,我发现关键的属性是..

resourceType="Unspecified"

我最初按照微软的示例来设置它,他们的设置

resourceType="File"

一直给我404错误。我的 HTTPHandler 正在返回图形。

希望这有帮助:)

Just as a guide for those stuck with this problem I found the crucial attribute to be..

resourceType="Unspecified"

I originally followed a Microsoft example to set this up and they had it as

resourceType="File"

which just kept giving me 404 errors. My HTTPHandler is returning graphics.

Hope this helps :)

戴着白色围巾的女孩 2024-09-08 15:29:07

我使用的是IIS7,解决方案是:

在节

<system.web>
    <httpHandlers>
        <add verb="*" path="*.ashx" type="CVOS.MyDocumentHandler"/>
    </httpHandlers>
<system.web>

和节中

<system.webServer>
    <handlers>
       <add name="pdfHandler" verb="*" path="*.ashx"   type="CVOS.MyDocumentHandler" /> 
    </handlers>
</system.webServer>

i am using IIS7, the solution is:

in section

<system.web>
    <httpHandlers>
        <add verb="*" path="*.ashx" type="CVOS.MyDocumentHandler"/>
    </httpHandlers>
<system.web>

and section

<system.webServer>
    <handlers>
       <add name="pdfHandler" verb="*" path="*.ashx"   type="CVOS.MyDocumentHandler" /> 
    </handlers>
</system.webServer>
夏尔 2024-09-08 15:29:07

您的处理程序的扩展名是什么?如果您使用像 .hndlr 这样的自定义扩展,您可能还需要在 IIS 中添加 ScriptMap 并将其指向 ASP.NET 运行时,以便 IIS 可以转发请求到正确的处理器。


  1. 在 IIS7 中,转到您的网站
  2. 在 IIS 组下,转到处理程序映射
  3. 操作下单击添加脚本映射
  4. 将请求路径设置为 *.hndlr
  5. 设置路径到 ASP.NET 运行时 (%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll) 或您正在运行的任何版本。

然后,在您的 web.config 中,您将需要在适当的部分中注册处理程序,如其他答案中所述。

What is the extension of your handler? If you are using a custom extension like .hndlr you may also need to add a ScriptMap in IIS and point it to the ASP.NET runtime so that IIS can forward the request to the correct processor.


  1. In IIS7 go to your website
  2. Under the IIS group go to Handler Mappings
  3. Under Actions click Add Script Map
  4. Set Request Path to *.hndlr
  5. Set Path to the ASP.NET runtime (%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll) or whatever version you are running.

Then in your web.config you will need to register the handler in the appropriate section as described in the other answer.

旧人九事 2024-09-08 15:29:07

如果您已将处理程序设置为 32 位,但在 64 位中运行(反之亦然),也可能会遇到此错误。两者的设置都很简单,并且涵盖了所有基础。

注意“preCondition”和“scriptProcessor”的区别。

<handlers>
    <add name="MyHandler_32bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness32" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
    <add name="MyHandler_64bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness64" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
</handlers>

It is also possible to experience this error if you have set up the handler for 32 bit, but you are running in 64 bit (or vice versa). It's easy to set up both and have all the bases covered.

Note "preCondition", and "scriptProcessor" differences.

<handlers>
    <add name="MyHandler_32bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness32" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
    <add name="MyHandler_64bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness64" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
</handlers>
说好的呢 2024-09-08 15:29:07

以前的答案都不适合我。
我正在使用 IIS 8.5、.Net v4.0、Integrated,但使用以下处理程序配置时仍然收到 404:

<system.webServer>
    <handlers>
       <add name="testEmail" path="*.em" verb="*" type="MyApp.testRazorEmailHandler, MyApp" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
</system.webServer>

我启用了跟踪并发现了以下内容:

116. -HANDLER_CHANGED 

    OldHandlerName              testEmail 
    NewHandlerName              System.Web.Mvc.MvcHandler 
    NewHandlerModules           ManagedPipelineHandler 
    NewHandlerScriptProcessor
    NewHandlerType              System.Web.Mvc.MvcHandler, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 

正如您所看到的,它看起来像是使用我的自定义 HttpHandler testEmail 正确接收了请求,但是 MVC 窃取了它
我在 RouteConfig.cs 中打开路由定义,发现添加了:

   routes.IgnoreRoute("{resource}.em");

我让它忽略针对我的处理程序的请求。

希望这对某人有帮助 - 我正在撕扯我的头发!

None of the previous answers worked for me.
I'm using IIS 8.5, .Net v4.0, Integrated, and was still getting a 404 with the following handler config:

<system.webServer>
    <handlers>
       <add name="testEmail" path="*.em" verb="*" type="MyApp.testRazorEmailHandler, MyApp" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
</system.webServer>

I enabled tracing and found the following :

116. -HANDLER_CHANGED 

    OldHandlerName              testEmail 
    NewHandlerName              System.Web.Mvc.MvcHandler 
    NewHandlerModules           ManagedPipelineHandler 
    NewHandlerScriptProcessor
    NewHandlerType              System.Web.Mvc.MvcHandler, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 

As you can see it looks like it had correctly picked up the request using my custom HttpHandler testEmail but MVC had stolen it.
I opened my route definitions in RouteConfig.cs and found that adding:

   routes.IgnoreRoute("{resource}.em");

I got it to ignore requests meant for my Handler.

Hope this helps someone - I was tearing my hair out!

小瓶盖 2024-09-08 15:29:07

希望我的解决方案对其他人有帮助。在服务器从 IIS6 迁移到 7.5(两者都集成了 .Net 4.0)时,我有一个 Captcha 控件停止工作。事实证明,从 中的 节点删除此属性 preCondition="integratedMode,runtimeVersionv2.0" ; 解决了这个问题。

Hopefully my solution will help others. On a server move from IIS6 to 7.5, both .Net 4.0 Integrated, I had a Captcha control that quit working. It turns out that removing this attribute preCondition="integratedMode,runtimeVersionv2.0" from the <add> node in <system.webserver><handlers> resolved the issue.

凉宸 2024-09-08 15:29:07

这似乎是一个边缘情况,但我有一个客户,我们的应用程序中使用的 httpHandler 无法在他们的任何服务器上工作。该处理程序指向一个 .ashx 页面,并从 JavaScript 调用它。

处理程序映射显示在 IIS 中,处理程序工厂也在那里,但当浏览器请求与处理程序关联的 ashx 页面时,我会收到 404。经过多次不同的修复尝试后,我们最终浏览到服务器上 IIS 中的文件,它特别显示了 404.7 与此消息一起返回。

•为 Web 服务器配置了请求过滤,并且此请求的文件扩展名被明确拒绝。

•验证 applicationhost.config 中的configuration/system.webServer/security/requestFiltering/fileExtensions 设置和web.config。

如果您收到此消息,则说明已在您的应用程序或网站级别为 .ashx 扩展名启用了请求过滤。转到站点和应用程序级别 IIS 中的“请求过滤”选项,并验证扩展程序未被阻止。有两种不同的方式可以配置请求过滤。

默认情况似乎是它仅显式阻止列表(黑名单)中配置的文件扩展名。另一种配置方式是,仅允许列表中专门配置的文件通过(白名单)。第二个选项是客户默认配置其所有 Windows 服务器的方式,结果发现 .ashx 文件扩展名不在允许的扩展名列表中。

This seems to be an edge case, but I had a customer where our httpHandler used in our application did not work on any of their servers. The handler pointed to an .ashx page and it was called from JavaScript.

The handler mapping showed up in IIS, the handler factory was there, but I would get a 404 when the browser requested the ashx page associated with the handler. After many different attempts to fix we finally browsed to the file in IIS on the server and it specifically showed a 404.7 being returned with this message.

•Request filtering is configured for the Web server and the file extension for this request is explicitly denied.

•Verify the configuration/system.webServer/security/requestFiltering/fileExtensions settings in applicationhost.config and web.config.

If you get this then Request Filtering is enabled for the .ashx extension at either your app or site level. Go to the Request Filtering option in IIS at both your site and app level and verify that the extension is not blocked. There are two different ways Request Filtering can be configured.

The default seems to be that it explicitly blocks only file extensions that are configured in the list (blacklist). The other way it can be configured is that only files specifically configured as allowed in the list are let through (whitelist). This second option is how the customer had configured all of their Windows Servers by default and it turns out that the .ashx file extension was not in the list of allowed extensions.

非要怀念 2024-09-08 15:29:07

这是当我查找响应 404 的动词时出现的第一个线程。在我的例子中,解决方案是 VS 的配置

Tools > Options > Web projects > [x] Use 64 bit version

抱歉,我的 VS 是西班牙语

This is the first thread that appears when I look for a verb that responds with a 404. In my case the solution was a configuration of VS

Tools > Options > Web projects > [x] Use 64 bit version

Sorry, my VS is in spanish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文