C# 自定义 HttpHandler 未触发,在 ASP.NET MVC 应用程序中返回 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/863243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:41:03  来源:igfitidea点击:

Custom HttpHandler not firing, returning 404 in ASP.NET MVC Application

c#asp.net-mvchttphandler

提问by Peter

I don't know if it is relevant that this is happening in an MVC website but thought I'd mention it anyway.

我不知道这是否发生在 MVC 网站上是否相关,但我想无论如何我都会提到它。

In my web.config I have these lines:

在我的 web.config 我有这些行:

<add verb="*" path="*.imu" type="Website.Handlers.ImageHandler, Website, Version=1.0.0.0, Culture=neutral" />

in the Website project I have a folder named Handlers which contains my ImageHandler class. It looks like this (I have removed the processrequest code)

在网站项目中,我有一个名为 Handlers 的文件夹,其中包含我的 ImageHandler 类。看起来像这样(我已经删除了 processrequest 代码)

using System;
using System.Globalization;
using System.IO;
using System.Web;

namespace Website.Handlers
{
    public class ImageHandler : IHttpHandler
    {
        public virtual void ProcessRequest(HttpContext context)
        {
            //the code here never gets fired
        }

        public virtual bool IsReusable
        {
            get { return true; }
        }
    }
}

If I run my website and go to /something.imu it just returns a 404 error.

如果我运行我的网站并转到 /something.imu,它只会返回 404 错误。

I am using Visual Studio 2008 and trying to run this on the ASP.Net development server.

我正在使用 Visual Studio 2008 并尝试在 ASP.Net 开发服务器上运行它。

I've been looking for several hours and had it working in a seperate empty website. So I don't understand why it won't work inside an existing website. There are no other references to the *.imu path btw.

我一直在寻找几个小时,让它在一个单独的空网站上工作。所以我不明白为什么它不能在现有网站中工作。顺便说一句,没有其他对 *.imu 路径的引用。

采纳答案by samjudson

I suspect that this has everything to do with the fact that you are using MVC, as basically it takes control of all incoming requests.

我怀疑这与您使用 MVC 的事实有关,因为基本上它控制所有传入的请求。

I suspect you will have to use the routing table, and possibly create a new routing handler. I haven't done this myself but something like this might work:

我怀疑您将不得不使用路由表,并可能创建一个新的路由处理程序。我自己没有这样做,但这样的事情可能会奏效:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "{action}.imu"
         , new ImageRouteHandler()
    ));
}

and then the ImageRouteHandlerclass will then return your custom ImageHttpHandler, although from looking at examples on the web it might be better to change that so it implements MvcHandler, rather than straight IHttpHandler.

然后ImageRouteHandler该类将返回您的自定义ImageHttpHandler,尽管通过查看 Web 上的示例可能会更好地更改它以便它实现MvcHandler,而不是直接IHttpHandler

Edit 1: As per Peter's comment, you can also ignore the extension by using the IgnoreRoutemethod:

编辑 1:根据 Peter 的评论,您还可以使用以下IgnoreRoute方法忽略扩展名:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.imu/{*pathInfo}");
}