Serving files from App_Data #1 - the naive approach.

by Matt 5. October 2006 03:52

Don't worry, I've not gone crazy.

Asp.net 2 doesn't let you serve files from the App_Data folder, and for good reason - security. It's a folder your web app has write access to (a standard place on all installations without having to get your ISP involved - halellujah!) so in goes your databases and various other data files. You don't want anyone to be able to just download these, right? Right. Well, most of the time, anyway.

If you're dealing with a site that has mostly user-generated content, you're going to have to write a lot of data. The place of least friction for this data is of course App_Data. Think of a blog site (cough, SingleUserBlog, cough); all of the blog posts are going to be stored in the App_Data folder, perhaps as flat files or a database. And this is fine, because you don't serve those files directly - they're content to go in a page. But what about uploads? How can I include images in my blog? Uploading them is no problem, just chuck them in App_Data. But I can't serve them.

This is exactly the kind of solution a custom http handler is intended to solve. And the naive approach is to simply call HttpResponse.WriteFile and congratulate myself on a job well done.

So what's wrong with this?

Firstly, WriteFile reads the whole file into memory before chucking it to the client. For large files, it can fail. This isn't the best. But it can be worked around. I can use HttpResponse.TransmitFile to stream direct to the client. Or I could easily roll your own (this Microsoft KB article shows how).

Secondly, I'd have to add my own logic to setup the mime types. Boring, but still not reason enough to call it naive.

The third reason is the biggie. It's the single biggest mistake you see with custom http handler implementations, and that's forgetting caching and conditional gets. Bandwidth isn't free, and yet most http handlers will just serve the file. Again and again and again. To do things properly, I'd need to create and check etag values, and handle the various combination of the http headers (If-Unmodified-Since, Unless-Modified-Since, etc).

I could make this point into a blog post by itself. Fortunately, Kent Sharkey has already done it. Go read. Please.

Wouldn't it be nice if I could get someone else to do all this heavy lifting for me? (And I still haven't forgotten security.)

Tags:

asp.net

Comments (57) -

suv review
suv review
7/20/2011 10:20:30 PM #

Per il tuo bambino scegli Moncler. Una scelta di capi, estivi ed invernali, eccezionali. Tuo figlio sarà sempre alla moda e potrà muoversi in totale comodità.

Reply

best hybrid cars 2011
best hybrid cars 2011
7/22/2011 4:46:32 AM #

it ti permette di orientarti con semplicità nel pulviscolare niverso di Moncler.Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua felpe moncler, pantaloni e abbigliamento sportivo.

Reply

iphone os 4
iphone os 4
7/23/2011 7:51:07 PM #

it ti permette di orientarti con semplicità nel pulviscolare niverso di Moncler.Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua felpe moncler, pantaloni e abbigliamento sportivo.

Reply

beats by dr dre pro
beats by dr dre pro
10/30/2011 10:40:55 PM #

Catch one's heart,never be apart.

Reply

Bruce
Bruce
11/6/2011 4:46:09 PM #

Interesting article, not like the others!

Reply

chiropractor madison wi
chiropractor madison wi
11/8/2011 4:38:59 PM #

What a fantastic blog post. Thank you very much for writing this important information.

Reply

Jen
Jen
11/23/2011 11:52:50 AM #

Hello,
I just want to give two thumbs up for the good data youve got here on this site.
Thanks a lot   - -

Johna Rimmer Photography
937 Villaire Avenue
Windsor ON, N8S 2J5
(519) 567-3082

Reply

iPad games reviews
iPad games reviews
12/1/2011 5:02:58 AM #

Good job  , amazing Post !!!

Reply

philix
philix United States
11/21/2015 1:00:25 PM #

great site

Reply

Luvenia Colier
Luvenia Colier United States
1/15/2016 3:48:09 AM #

the style of keeping you connected with so many things of different types is awesome and you find it interesting everytime.

Reply

Darnell Drape
Darnell Drape United States
1/15/2016 4:15:29 AM #

getting laid AWSOME

Reply

Robin Eisiminger
Robin Eisiminger United States
1/15/2016 4:33:50 AM #

Printing out an essay or paper, reading the first line and realizing there aren't any mistakes.<br />AWESOME!

Reply

Leonel Marturano
Leonel Marturano United States
1/15/2016 4:51:29 AM #

the style of keeping you connected with so many things of different types is awesome and you find it interesting everytime.

Reply

Nathanael Mcmullen
Nathanael Mcmullen United States
1/15/2016 5:07:44 AM #

Printing out an essay or paper, reading the first line and realizing there aren't any mistakes.<br />AWESOME!

Reply

Jamison Gerraro
Jamison Gerraro United States
1/15/2016 5:44:24 AM #

Well good for you! I have a lot but one I just thought of was when you lick a crease of paper so that it'll rip in a straight line and it actually tears in a frayed straight line rather than rips awkwardly. I love that bleachy taste and that bleachy satisfaction.

Reply

Natividad Sluder
Natividad Sluder United States
1/15/2016 5:45:30 AM #

Congrats. And all of the items featured so far should be in the book.

Reply

Isreal Payor
Isreal Payor United States
1/15/2016 6:39:23 AM #

I can't wait to read this masterpiece. Ordering off the menu at fast food restaurants and old dangerous playground equipment have to be in the book!

Reply

Jinny Mccleese
Jinny Mccleese United States
1/15/2016 6:58:50 AM #

Well good for you! I have a lot but one I just thought of was when you lick a crease of paper so that it'll rip in a straight line and it actually tears in a frayed straight line rather than rips awkwardly. I love that bleachy taste and that bleachy satisfaction.

Reply

Isreal Payor
Isreal Payor United States
1/15/2016 7:14:41 AM #

yeah, except the "dolphin saving you" one

Reply

Robin Eisiminger
Robin Eisiminger United States
1/15/2016 7:44:53 AM #

getting the last pair of shoes in your size AWESOME!

Reply

Jinny Mccleese
Jinny Mccleese United States
1/15/2016 8:20:28 AM #

Well good for you! I have a lot but one I just thought of was when you lick a crease of paper so that it'll rip in a straight line and it actually tears in a frayed straight line rather than rips awkwardly. I love that bleachy taste and that bleachy satisfaction.

Reply

Jinny Mccleese
Jinny Mccleese United States
1/15/2016 8:27:47 AM #

getting laid AWSOME

Reply

Wai Freifeld
Wai Freifeld United States
1/19/2016 10:16:23 PM #

Congrats. And all of the items featured so far should be in the book.

Reply

Ralph Okonek
Ralph Okonek United States
2/28/2016 3:54:30 PM #

An interesting posting there friend ! Due for report .

Reply

Alva Ostergren
Alva Ostergren United States
3/10/2016 3:29:29 AM #

Comprise your self strategy around incorporating some movies to the post? I consider it may well enhance my understanding.

Reply

concrete water tanks
concrete water tanks United States
4/24/2016 4:10:55 AM #

Fabulous post!!! This webpage simply keeps me coming back. I enjoy the labor that you put into it and cherish browsing all of the posts. Hi and thanks a great deal and also keep the wonderful work up!

Reply

Oliva Merrihew
Oliva Merrihew United States
5/12/2016 4:54:52 AM #

This is so great post! Very well written and very informative! I will recommend your site to my friends!

Reply

Shannon Messenger
Shannon Messenger United States
5/19/2016 3:40:42 AM #

I am interested in advertising a new social networking site.  I have never wrote a Blog before.  How should I write this? . And where do I go to write a Blog.  This is all new to me and I want to start rocking.. Thanks..

Reply

Sheridan Agnelli
Sheridan Agnelli United States
6/21/2016 2:56:05 PM #

Woah this blog is magnificent i really like reading your posts. Keep up the good paintings! You realize, many individuals are searching round for this information, you can help them greatly.

Reply

pożyczka
pożyczka United States
6/23/2016 9:59:10 AM #

Pożyczka gotówkowa

Reply

Rejestracja Domen
Rejestracja Domen United States
6/25/2016 8:21:00 AM #

Transfer domen

Reply

rejestracja domen
rejestracja domen United States
6/25/2016 8:27:48 AM #

rejestracja domen

Reply

Rachael Milles
Rachael Milles United States
7/17/2016 5:50:05 PM #

In today's technological advances, we sure would like to to read another informative post like this about the smart house architectural layouts

Reply

Scott Weldeen
Scott Weldeen United States
7/20/2016 7:43:28 AM #

You have an accurate point of view, thanks!

Reply

Carri Thayne
Carri Thayne United States
7/20/2016 3:12:48 PM #

Hi there! I could have sworn IÕve been to your blog before but after browsing through a few of the articles I realized itÕs new to me. Anyhow, IÕm certainly delighted I stumbled upon it and IÕll be book-marking it and checking back regularly!

Reply

James Fly
James Fly United States
7/28/2016 7:45:08 PM #

This is such a great site come check out our new free Dating web site would wide fined your true love at http://youllfindlove.com/

Reply

Ima Gerberich
Ima Gerberich United States
7/29/2016 9:24:56 AM #

My Firefox stopped working saying the server wasn't found but Internet Explorers still works. I used to use firefox all the time but only recently it stopped working. I already checked the firewall and it allows firefox so i don't know what's wrong..

Reply

Roselee Braun
Roselee Braun United States
7/29/2016 6:05:19 PM #

I recently did a system restore on my computer and have yet to reload my antivirus software.  Apparently somewhere along the process something called Antivi took over my computer and I can't do anything, not even get to my task manager to end the program.  Any advice?.

Reply

Kathie Digian
Kathie Digian United States
8/25/2016 4:21:36 AM #

Great post but I was wanting to know if you could write a litte more on this subject? I’d be very appreciative if you could detail it a little bit further. Thank You!

Reply

nutra forskolin
nutra forskolin United States
9/7/2016 2:31:52 PM #

How to get your customized blogspot to appear on google search?

Reply

potty training tips
potty training tips United States
9/9/2016 4:53:25 PM #

How to become a tutor in creative writing for Further education and or teach English as a second language?

Reply

James Fly
James Fly United States
9/25/2016 1:18:32 PM #

Это такой хороший сайт с большим количеством полезной информации . Приходите присоединиться к нашему новый бесплатный социальный сайт, как Facebook . У нас есть группы друзей , объявления, сайт знакомств играть в игры бесплатно для всех на http://mysocialspot.ca/

Reply

John Smith
John Smith United States
11/4/2016 7:10:10 PM #

This is such a nice great website with lots of good information. Please come join our new free social site like Facebook. We have groups, friends, classifieds, dating site play games for free all at http://mysocialspot.ca/

Reply

Dorethea Brennan
Dorethea Brennan United States
3/3/2017 6:29:38 AM #

"Great Blogpost! Hello mates, nice post and good arguments commented here, I am in fact_ enjoying by these."

Reply

Ivory Krenke
Ivory Krenke United States
3/14/2017 8:31:38 PM #

This is a awesome site with some great information

Reply

Dale Hesketh
Dale Hesketh United States
3/27/2017 2:51:34 AM #

This is a awesome website with some awesome information

Reply

Bari Kenaga
Bari Kenaga United States
4/3/2017 3:19:45 AM #

please continue to write and work on your website

Reply

download app from google play
download app from google play United States
4/6/2017 1:18:29 AM #

I got another website may grab apk from Google Play: http://www.apksmart.com

Reply

Ludie Plunket
Ludie Plunket United States
5/5/2017 4:29:32 AM #

great issues altogether, you just won a new reader. What could you suggest about your post that you made a few days in the past? Any positive?

Reply

Jeanelle Harens
Jeanelle Harens United States
5/18/2017 11:28:17 PM #

Today's Anniversaries

Reply

American Bully
American Bully United States
5/28/2017 3:00:36 PM #

I took a look at this link sense i kept seeing it and i really did like the information that i found keep up the good work <a href="https://www.bonafideseo.com">amrican bully</a>

Reply

Welcome.  Today I  want to tell you about present  financial   standing in  the United Kingdom of Great Britain.  from a personal perspective that Great Britain did well being member of the EU. in case of Great Britain withdraws from the EU  the future doesn’t seem to be so optimistic.There are some perspectives that we are to realize:control over immigration together with single European market is impossible.  let’s continue with the next point I believe that fiscal system has become fragile. We are to pay more for the bills. I express no will for  check my political and financial outlooks about financial and political news, it is unpleasant”unaccepted  for me. What do you know about payday loans direct lender?

Reply

Jacqulyn Soltis
Jacqulyn Soltis United States
6/9/2017 7:30:10 AM #

Simply desire to say your article is as astounding. The clarity in your post is simply nice and i can assume you’re an expert on this subject. Fine with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the rewarding work.

Reply

昊奇音樂
昊奇音樂 United States
6/16/2017 4:36:42 AM #

☆☆☆☆☆ Logic Pro X 中文教程视频 ☆☆☆☆☆『超过30小时且持续增加的教程内容,从基本操作丶效果器详细解说丶合成器讲解丶混响後制,到House音乐制作。』★一次付费,无限期下载★您获得的内容将远超过於您的付出

Reply

como quitar las espinillas
como quitar las espinillas United States
10/10/2017 4:11:02 PM #

I'm really enjoying the theme/design of your blog. Do you ever run into any web browser compatibility issues? A few of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Opera. Do you have any suggestions to help fix this problem?

Reply

acn&#195;&#169;
acné United States
10/15/2017 7:47:46 AM #

Some really   fantastic   information,  Sword lily I found  this. "Ours is a world where people don't know what they want and are willing to go through hell to get it." by Donald Robert Perry Marquis.

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS