ASP.NET MVC 2.0 + Iroutehandlerの実装は発生しません
-
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;
}
}
どんな体でも私がここで見逃していることを教えてもらえますか。単純にpublic 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()));
これら2つの宣言をひっくり返す必要があります。 {controller}/{action}/{id} おそらく着信URLと一致しているので、あなたはあなたの特別を宣言する必要があります 画像/{ファイル名} その前に。
所属していません StackOverflow