Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which code component in an ASP.NET (WebForms or MVC) application serves website resources such as images?

When you have an image tag in your application like so:

<img src = @Url.Content("~/foo/bar.jpg") />

or like so:

<asp:image src = "~/foo/bar.jpg" />

or like so:

<input type = "image" ...>

and if this happens to be an ASP.NET application (Web Forms or MVC is irrelevant to the question, I assume, as the code serving images must be the same), which specific component serves requests for images?

Is it the Web server (IIS or Visual Studio's internal web server) or is it an ASP.NET HttpHandler that Microsoft wrote.

I remember there used to be a handler for serving resource files in ASP.NET 2.0. Is that the one that serves images, too? What's it called?

Why am I asking this question / What I am trying to do

I have an ASP.NET MVC application and I want to write my own custom HttpHandler or a Controller to serve static assets like images so that they get cached.

What I am planning to do it something almost exactly similar to http://blog.hompus.nl/2011/03/11/make-your-browser-cache-the-output-of-an-httphandler/ and http://archive.msdn.microsoft.com/ResourceCache

I want to make sure that if there's one already written by Microsoft, I might need to take some precautions while sending back Http headers in the response just so that the headers Microsoft's in-built httpHandler sets are not in contradition to the ones I set.

like image 527
Water Cooler v2 Avatar asked Mar 12 '13 11:03

Water Cooler v2


2 Answers

By default, all requests for ASPX pages (including static resources therein, like images) are served by the default HttpHandler, which is an instance of the PageHandlerFactory class:

Instances are created dynamically to handle requests for ASP.NET files. The PageHandlerFactory class is the default handler factory implementation for ASP.NET pages.

As you can see from the MSDN article on HttpHandlers and HttpModules, the primary "built in" handlers are those for

  • standard ASP.NET files (.aspx),
  • web services (.asmx),
  • generic, non-UI handlers (.ashx), and
  • one for handling the trace (trace.axd).

There is not one that is specifically handling image requests. So you should be good to go =)

like image 78
Josh Darnell Avatar answered Sep 22 '22 11:09

Josh Darnell


Okay, it appears to me that ASP.NET doesn't care much for any static resources and therefore, it doesn't register an ISAPI filter with IIS for any of those resources.

That means, as a developer who wants to write a specific handler for static resources, you're pretty much free to do whatever you want.

like image 27
Water Cooler v2 Avatar answered Sep 21 '22 11:09

Water Cooler v2