ASP.NET MVC 2.0 + IRouteHandler 的实现不会触发
谁能帮我解决这个问题,因为我不知道为什么 公共 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 中的任何内容。我在这里缺少什么?请帮忙。
Can anybody please help me with this as I have no idea why
public IHttpHandler GetHttpHandler(RequestContext requestContext)
is not executing. In my Global.asax.cs I have
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 implementation is below
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;
}
}
Can any body tell me what I'm missing here. Simply
public IHttpHandler GetHttpHandler(RequestContext requestContext) does not fire.
I havn't change anything in the web.config either. What I'm missing here? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要翻转这两个声明。 {controller}/{action}/{id} 可能与传入的 URL 匹配,因此您需要在其之前声明特殊的Images/{filename}。
You need to flip these two declarations. {controller}/{action}/{id} is probably matching the incoming URL, so you need to declare your special Images/{filename} before it.