1

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();
    }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
qingyun1029
  • 45
  • 1
  • 9
  • 1
    Can you copy the relevant parts of `ImageRouteHandler.cs` here? The problem with just having a link to an article is that the article can change or the link may not be valid at a future time. Plus it makes it easier for us to help because we won't have to search for what you're talking about. – Jacob Oct 13 '16 at 04:14
  • Have modified the post,thanks! – qingyun1029 Oct 13 '16 at 04:30
  • Can you also post what error you're getting? It's still not clear what you mean by "will not work." – Jacob Oct 13 '16 at 15:39

1 Answers1

0

~ 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."

Jacob
  • 77,566
  • 24
  • 149
  • 228