Chris Pratt
Chris Pratt

Reputation: 239290

Serving static files in development with IIS Express and Visual Studio 2012

This seems like a pretty common use case, but I can't for the life of me find any information on how to set this up.

I've got an "Admin" site where I upload images and then my normal website (separate project) where I need to display those images. Both projects need to be able to display the images (so I can't just simply store them in the website's project directory and call it a day).

In production, this is a no-brainer. I just set up a virtual directory in IIS, but developing in Visual Studio, there's no direct support for this. I've seen things online that tell you do go into the IIS Express config files and add the virtual directory manually, but there's two huge problems with this: 1) I'm in a multi-developer environment and making every developer do this in a consistent way manually is a no-go and 2) there's no automated way to reference this static directory in my code, so that's going to entail a lot of hard-coding paths to image references, which is a hugely bad idea.

I'm aware, also, that I can create links in my project, but I'm dealing with uploads, not static resources that I know about at design time, so creating links for every new upload manually in my project (especially when this is only for testing in development) is insane.

So what is the best way to dynamically share resources between projects just for the purposes of development in Visual Studio?

For what it's worth, these are both ASP.NET MVC 4 projects and I'm using Visual Studio 2012 on a Windows 7 box.

EDIT I should specify that upon uploading the image, a relative path to that image is stored in a database table. I suppose my real issue is in resolving that partial path into a real accessible URL from an entirely different project. If I was in the same project, I would simply to something like Url.Content(Model.ImageUrl), but this obviously won't resolve if the image is not actually in the same project.

Upvotes: 0

Views: 2285

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

It was pointed out to me that I'm over-thinking this.

In production, the files would of course be served through some virtual directory, but in development, it doesn't matter where they're served from. So, all I really need to do is add an app setting for the static URL prefix to prepend to the relative image URL.

For development it would just be the localhost + port of my admin site. Then, in my release web.config transform, I would just change it to the production URL, i.e. something like "http://static.mydomain.com". In my app code, I just look up this app setting and prepend it to my relative image URL and I'm golden.

If I run into any problems with this approach, I'll note them here with appropriate solutions, once I get a chance to actually implement it.

Upvotes: 1

Related Questions