asp.net-mvciroutehandler

Why not into RouteHandler ?


I'm trying write a demo about "Prevent Image Leeching", Reference resources: http://www.mikesdotnetting.com/article/126/asp-net-mvc-prevent-image-leeching-with-a-custom-routehandler

but when I using <img src="~/graphics/a.png" />, The ImageRouteHandler.cs will not work. Unfortunately, this ImageRouteHandler.cs will not work yet. Why ??

public class ImageRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new ImageHandler(requestContext);
    }
}

public class ImageHandler : IHttpHandler
{
    public ImageHandler(RequestContext context)
    {
        ProcessRequest(context);
    }

    private static void ProcessRequest(RequestContext requestContext)
    {
        var response = requestContext.HttpContext.Response;
        var request = requestContext.HttpContext.Request;
        var server = requestContext.HttpContext.Server;
        var validRequestFile = requestContext.RouteData.Values["filename"].ToString();
        const string invalidRequestFile = "thief.png";
        var path = server.MapPath("~/graphics/");

        response.Clear();
        response.ContentType = GetContentType(request.Url.ToString());

        if (request.ServerVariables["HTTP_REFERER"] != null &&
            request.ServerVariables["HTTP_REFERER"].Contains("http://localhost:8010/")) //mikesdotnetting.com
        {
            response.TransmitFile(path + validRequestFile);
        }
        else
        {
            response.TransmitFile(path + invalidRequestFile);
        }
        response.End();
    }

    private static string GetContentType(string url)
    {
        switch (Path.GetExtension(url))
        {
            case ".gif":
                return "Image/gif";
            case ".jpg":
                return "Image/jpeg";
            case ".png":
                return "Image/png";
            default:
                break;
        }
        return null;
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        throw new NotImplementedException();
    }
}

Solution

  • ~ is not a meaningful prefix in a URL. That is sometimes used in some ASP.NET contexts, like Server.MapPath, to refer to the application root, but in HTML this URL:

    <img src="~/graphics/a.png" />
    

    ...is not valid.

    Use / at the beginning to refer to the root of your site, or omit a leading / to refer to a relative URL. It's not clear if this is the only problem you're encountering, but it is one problem. You may have better luck doing this:

    <img src="/graphics/a.png" />
    

    By the way, pay attention to the network tab of your developer tools; that will let you see all requests (like your image request) and responses. Saying it "will not work" or similar is not useful. Things never "don't work"; they just do something other than what you expect. A better description would be "I'm getting a 404 error" or "the request for my image isn't being made."