Can this be done and if so how?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I have a client application that requests many large jpeg mages from my asp
site. However these images can be programatically generated very easily and
very quickly.

Suppose my site receives a request for images/bigImage.jpg. Is it possible
for me to intercept this request and process it programatrically - say using
a processRequest? Or do I have to at least have some file called
images/bigImag.jpg on my site. Note that I can't use a query string since I
have no control over the client - it will simply request
images/bigImage.jpg.

If it is possible, where would be a good place to start looking?

Thanks for any help.

Jeff
 
Answer yes and no.
If you have access to IIS Management Console (meaning you have admin rights
on a server) you can associate jpg extension with ASP.NET engine and then
using Application_BeginRequest or (write a custom module or handler) do what
you want.

If you are on shared hosting you are out of luck (unless your provider will
configure that option for you)

PS: If you can create custom 404 page then it might work for you. Create
custom 404.aspx and do the job in there.

George.
 
George said:
Answer yes and no.
If you have access to IIS Management Console (meaning you have admin
rights on a server) you can associate jpg extension with ASP.NET engine
and then using Application_BeginRequest or (write a custom module or
handler) do what you want.

If you are on shared hosting you are out of luck (unless your provider
will configure that option for you)

PS: If you can create custom 404 page then it might work for you. Create
custom 404.aspx and do the job in there.

George.

Thanks a million George, I think your idea about the custom 404 will work.
I registered a custome error module:-

<httpModules>
<add type="AspNetResources.CustomErrors4.MyErrorModule" name="MyErrorModule"
/>
</httpModules>

Now, when I receive a request for a non existant jpeg I can capture it in
MyErrorModule and look at the request. If the request is for a bitmap that I
know how to make then I make the bitmap and stream it out as a jpeg.
Othwewise I just serve up a custor error message or image.

Seems pretty straightforward. I thought it would be a lot harder than this -
maybe I'm missing something!

Thanks again
Jeff
 
Unfortunately it might not work in production.
Depending on which IIS version you have. On IIS 6.0 or Win2003 your request
for non-existent image *.jpg will never make it to .NET runtime. Hence your
handler will not work

George.
 
George said:
Unfortunately it might not work in production.
Depending on which IIS version you have. On IIS 6.0 or Win2003 your
request for non-existent image *.jpg will never make it to .NET runtime.
Hence your handler will not work

Thanks again

Yes, I discovered shortly after posting that this wouldn't work on a shared
hosting site - so I'm now taking the custom 404 error page route.

I set up a custom 404 which gets displayed whenver a non-existant page is
requested.
In the page_load I check if the referring url was a jpeg I can construct and
if so I stream it out to the response - otherwise I display the standard
aspx page.

Now, this again works fine on my local system, but on the shared hosting
site my attempt to stream out the image is producing a 404 which produces my
standard 404 page.
What is the correct place to send the write to the response - in the
page_load or somewhere else?

Thanks
Jeff
 
I did not understand this phrase
"my attempt to stream out the image is producing a 404 which produces my
standard 404 page."

I actually would check if you got your custom page 404 page working.
Just output simple "hello" and check that if you request something like
http://www.mysite.com/nopage
You get your "hello".


You might want to add folowing lines to your 404 page to clear out 404 error
code.
Response.Clear();
Response.CacheControl = "no-cache";
Response.Expires = -1;
Response.StatusCode = 200;
Response.Status = "200 Ok";

Also do not forget to set ContentType = "image"; to indicate that it's image
being produced.

George
 
George said:
I did not understand this phrase
"my attempt to stream out the image is producing a 404 which produces my
standard 404 page."

I actually would check if you got your custom page 404 page working.
Just output simple "hello" and check that if you request something like
http://www.mysite.com/nopage
You get your "hello".


You might want to add folowing lines to your 404 page to clear out 404
error code.
Response.Clear();
Response.CacheControl = "no-cache";
Response.Expires = -1;
Response.StatusCode = 200;
Response.Status = "200 Ok";

Also do not forget to set ContentType = "image"; to indicate that it's
image being produced.

George

Once again, many thanks for your help.

I checked that my custom error page was writing stuff out ok.

What seems to be causing the problem is that my code for getting the
referring URL works ok on my local machine but apparently not on the remote
machine.
To get the referring page I put the following

string requestedPage = Request.QueryString["aspxerrorpath"];
if (!requestedPage.EndsWith("jpg"))
return;

Writing out <%=requestedPage%> on my local machine produces the expected
result, writing on the shared host produces an empty string.

Thanks

Jeff
 
Ok, I talked with the support people at my hosting Company.
It seems like there are two ways in which my custom 404 error page is
called:

Non existant urls with an extension assocuiated with asp.net are routed
through the asp.net environment
Other non existant urls are routed through the regular IIS(?) environment.

Unfortunately the latter Urls do not have a referring url in the query
string :-(

Looks like a complete gotcha.

Thanks for all the help.
Jeff
 
Back
Top