Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:15:50+00:00 2026-06-17T00:15:50+00:00

I have an asp.net web forms application that uses an ImageHandler to output images.

  • 0

I have an asp.net web forms application that uses an ImageHandler to output images. Basically to prevent leeching but also to get image files from another server.

Here’s the implementation of ProcessRequest:

public void ProcessRequest(HttpContext ctx)
    {
        HttpRequest req = ctx.Request;
        string path = req.PhysicalPath.ToLower();
        string extension = Path.GetExtension(path);

        if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)
        {
            if (CultureInfo.InvariantCulture.CompareInfo.Compare(req.Url.Host, req.UrlReferrer.Host, CompareOptions.IgnoreCase) != 0)
            {
                path = ctx.Server.MapPath("~/images/noimage.jpg");
            }
        } 

        // Rewrite path if not in production
        if (imagePath != null)
        {
            if (path.Length > path.IndexOf("\\images\\", StringComparison.Ordinal) + 7)
            {
                string end = path.Substring(path.IndexOf("\\images\\", StringComparison.Ordinal) + 7);
                path = string.Concat(imagePath, end).Replace("\\", "/");
            }
        }

        string contentType;

        switch (extension)
        {
            case ".gif":
                contentType = "image/gif";
                break;
            case ".jpg":
                contentType = "image/jpeg";
                break;
            case ".png":
                contentType = "image/png";
                break;
            default:
                throw new NotSupportedException("Unrecognized image type.");
        }

        if (!File.Exists(path))
        {
            ctx.Response.Status = "Image not found";
            ctx.Response.StatusCode = 404;
        }
        else
        {
            ctx.Response.StatusCode = 200;
            ctx.Response.ContentType = contentType;
            ctx.Response.WriteFile(path);
        }
    }

The above code fails because I want to rewrite the path to a url not a file path. The reason I want to rewrite is because the actual image files is on another server and is not accessible via UNC paths. What am I doing wrong, and is it possible at all to accomplish this?

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:15:52+00:00Added an answer on June 17, 2026 at 12:15 am

    When you are on the call for this handler – then you do not have any more the ability to rewrite your path as I see that you try to do.

    The rewrite path is to say to IIS and asp.net that a call for example to http://www.url.com/one/page1.aspx is served by the http://www.url.com/someotherpage.aspx?id=one

    You are all ready not on the final processing of your data, there you must read the file and send it to the browser, for example as:

     public void ProcessRequest(HttpContext context)
        {
          // your first code
          // ...
           if (!File.Exists(path))
            {
                ctx.Response.Status = "Image not found";
                ctx.Response.StatusCode = 404;
            }
            else
            {
                // load here the image 
                ....
                // and send it to browser
                ctx.Response.OutputStream.Write(imageData, 0, imageData.Length);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# web forms ASP.NET 4.0 web application that uses Routing for
I have an asp.net web forms application that uses linq 2 sql. A lot
I have an older ASP.NET MVC application that uses classic web forms views. As
I have an ASP.NET web application that is using forms authentication. Everything is configured
I have a asp.net web forms app that uses System.Web.Caching.Cache to cache xml data
I have a ASP.NET Web Forms application that internally makes many SOAP and REST
I have an ASP.NET web application that uses jQuery on client side. On one
I am a developer working on an ASP.Net Web Application that uses forms authentication.
I have an ASP .NET Web Forms application that makes use of the 'WebMethod'
I have an existing Web Site that uses multiple ASP .Net Web Forms projects.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.