문제

공개 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()));

이 두 가지 선언을 뒤집어 야합니다. {컨트롤러}/{action}/{id} 아마도 들어오는 URL과 일치하므로 특별한 것을 선언해야합니다. 이미지/{filename} 전에.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top