Asp.net mvc ASP.NET MVC 2.0+;IRouteHandler的实现不会触发
我不知道为什么,有人能帮我吗 公共IHttpHandler GetHttpHandler(RequestContext RequestContext) 没有执行。在我的Global.asax.cs中Asp.net mvc ASP.NET MVC 2.0+;IRouteHandler的实现不会触发,asp.net-mvc,iroutehandler,Asp.net Mvc,Iroutehandler,我不知道为什么,有人能帮我吗 公共IHttpHandler GetHttpHandler(RequestContext RequestContext) 没有执行。在我的Global.asax.cs中 public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRo
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;
}
}
有谁能告诉我我错过了什么吗。简单地
公共IHttpHandler GetHttpHandler(RequestContext RequestContext)未激发
我也没有在web.config中更改任何内容。我错过了什么?请帮忙
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}。很抱歉,您的解决方案是问题的一部分,但我发现了更多:)这是我的错。我把图像放在名为Content的目录下,即Content/images/{filename}。应该在提交帖子之前仔细检查。抱歉,谢谢你的帮助。