Frage

Kann mir jemand bitte helfen, da ich keine Ahnung habe, warum öffentliche IHTTPHANDLER Gethttphandler (RequestContext RequestContext) nicht ausführt. In meinem global.asax.cs habe ich

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 -Implementierung finden Sie unten

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;
    }
}

Kann mir jeder Körper sagen, was mir hier fehlt? Einfach public ihttphandler Gethttphandler (RequestContext RequestContext) feuert nicht aus.

Ich habe auch nichts im Web.config geändert. Was fehlt mir hier? Bitte helfen Sie.

War es hilfreich?

Lösung

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

Sie müssen diese beiden Erklärungen umdrehen. {Controller}/{action}/{id} Übereinstimmt wahrscheinlich mit der eingehenden URL, daher müssen Sie Ihr Special deklarieren Bilder/{Dateiname} bevor.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top