ASP.NET MVC一个IRouteHandler的2.0 +执行不火
-
19-09-2019 - |
题
任何人都可以请帮我这个,我不知道为什么 公众的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;
}
}
任何机构可以告诉我,我在这里失踪。只是 公共的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()));
您需要翻转这两个声明。的 {控制器} / {行动} / {ID} 的可能是匹配传入的URL,所以你需要声明你的特殊的图片/ {文件名} 的面前。
不隶属于 StackOverflow