任何人都可以帮我解决这个问题,因为我不知道为什么 public IHttpHandler GetHttpHandler(RequestContext requestContext) 没有执行。在我的 Global.asax.cs 我有
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实现如下
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;
}
}
任何人都可以告诉我我在这里缺少什么。简单地 public IHttpHandler GetHttpHandler(RequestContext requestContext) 不会触发。
我也没有更改 web.config 中的任何内容。我在这里缺少什么?请帮忙。