首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IRouteHandler的ASP.NET MVC2.0+实现不会触发

IRouteHandler的ASP.NET MVC2.0+实现不会触发
EN

Stack Overflow用户
提问于 2010-03-14 13:00:18
回答 1查看 2.1K关注 0票数 3

有人能帮我解决这个问题吗?因为我不知道为什么公共IHttpHandler GetHttpHandler(RequestContext requestContext)没有执行。在我的Global.asax.cs中

代码语言:javascript
复制
public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

//CustomRouteHandler实现如下

代码语言:javascript
复制
public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // IF I SET A BREAK POINT HERE IT DOES NOT HIT FOR SOME REASON.  
        string filename = requestContext.RouteData.Values["filename"] as string;

        if (string.IsNullOrEmpty(filename))
        {
            // return a 404 HttpHandler here 
        }
        else
        {
            requestContext.HttpContext.Response.Clear();
            requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());

            // find physical path to image here.   
            string filepath = requestContext.HttpContext.Server.MapPath("~/logo.jpg");

            requestContext.HttpContext.Response.WriteFile(filepath);
            requestContext.HttpContext.Response.End();

        }
        return null;
    }
}

有人能告诉我我错过了什么吗。简单地说,公共IHttpHandler GetHttpHandler(RequestContext requestContext)不会触发。

我也没有改变web.config中的任何东西。我错过了什么?请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-14 13:30:29

代码语言:javascript
复制
routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

您需要颠倒这两个声明。{controller}/{action}/{id}可能与传入URL匹配,因此您需要在它之前声明您的特殊图像/{filename}。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2441268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档