135

Is there any way to create a virtual directory in IIS express? I know that Cassini can't do this and it would be nice to be able to do this without using a full version of IIS.

I've got it so far that I can browse to my application locally in IIS express like this:

http://localhost:1132/

What I would like to do is create a virtual directory called "OffSiteStuff" and point it to some location on my C drive, like "c:\offsitestuff" and then browse to items in that folder like this:

http://localhost:1132/OffSiteStuff/UserUploadedImage.jpg

I know I could do this with a folder within my site and still use IIS Express, or, for that matter plain old Cassini, but this folder will store images uploaded by users and I really don't want to have these images mixed up with application files.

The other, "go big" solution is to deploy the site onto a full blown Server 2008 IIS 7.5 instance every time I want to debug the features that use offsite content, but that is a bit cumbersome too.

Is there any way I can do this in the <System.WebServer /> Web config element?

John Hoge
  • 2,371
  • 2
  • 20
  • 24
  • 7
    For Visual Studio 2015, https://mikedice417.wordpress.com/2015/09/13/vs-2015-and-you-must-specify-localhost-for-the-server-name/ worked. The `applicationHost.config` file is under the project root: `${PROJECT}\.vs\config\applicationHost.config`. – Matt Jan 26 '16 at 04:29
  • Here there is an answer that could help you: [https://stackoverflow.com/a/46260607/2472664](https://stackoverflow.com/a/46260607/2472664) – Giovanny Farto M. Sep 17 '17 at 04:22
  • Related post - [IIS express deploy application](https://stackoverflow.com/q/5455434/465053) – RBT Sep 01 '21 at 12:48

9 Answers9

136

IIS express configuration is managed by applicationhost.config.
You can find it in

Users\<username>\Documents\IISExpress\config folder.

Inside you can find the sites section that hold a section for each IIS Express configured site.

Add (or modify) a site section like this:

<site name="WebSiteWithVirtualDirectory" id="20">
   <application path="/" applicationPool="Clr4IntegratedAppPool">
     <virtualDirectory path="/" physicalPath="c:\temp\website1" />
   </application>
   <application path="/OffSiteStuff" applicationPool="Clr4IntegratedAppPool">
     <virtualDirectory path="/" physicalPath="d:\temp\SubFolderApp" />
   </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:1132:localhost" />
   </bindings>
</site>

Practically you need to add a new application tag in your site for each virtual directory. You get a lot of flexibility because you can set different configuration for the virtual directory (for example a different .Net Framework version)

EDIT Thanks to Fevzi Apaydın to point to a more elegant solution.

You can achieve same result by adding one or more virtualDirectory tag to the Application tag:

<site name="WebSiteWithVirtualDirectory" id="20">
   <application path="/" applicationPool="Clr4IntegratedAppPool">
     <virtualDirectory path="/" physicalPath="c:\temp\website1" />
     <virtualDirectory path="/OffSiteStuff" physicalPath="d:\temp\SubFolderApp" />
   </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:1132:localhost" />
   </bindings>
</site>

Reference:

Be.St.
  • 4,101
  • 3
  • 25
  • 35
  • After adding each application tag for each Virtual directory i was not able to run "offsitestuff" application. Ex: iisexpress /site:WebSiteWithVirtualDirectory - run the first app from the path "C:\temp\website1". How can i run my the 2nd app that contains the path "d:\temp\SubFolderApp" – Velu Aug 29 '12 at 12:00
  • In the bindings section you see the url and port used to access the root of your site. So: http://localhost:1132 is the url for the web application installed in c:\temp\website1 http://localhost:1132/OffSiteStuff is the url for the web application installed in d:\temp\SubFolderApp. – Be.St. Sep 10 '12 at 09:40
  • Anyone know if/how I can configure settings separately for each project (so I can check in the config)? Thanks – Ian Grainger Apr 30 '13 at 19:30
  • @IanGrainger can you explain a bit more what are you trying to achieve? – Be.St. May 17 '13 at 07:51
  • 1
    @Be.St. I'd like things like the port number for an IIS site to be the same for all developers - so I'd like to check (some of) the config in. But I can't do that from it's current location. Was wondering if I could point it to another file in the style of UserAppSettings etc. – Ian Grainger May 17 '13 at 13:50
  • @IanGrainger in Visual Studio go to the web application properties. Choose "Web" tab. In the "Server" section select "Apply server settings to all users" to store them in the project file and share across all users. – Be.St. May 17 '13 at 15:47
  • @Be.St. When selecting "Apply server settings to all users" I can only seem to get the original port to be shared for all users - I can't seem to get e.g. `/OffSiteStuff` to be shared for all users. – Nathan Sep 30 '14 at 21:34
  • Hi @Nathan, yes behavior is correct. Virtual directory information is added to applicationhost.config which is a per-user file. You have to manually modify each applicationhost.config foreach user. Port instead is shared because is stored in .csproj file. – Be.St. Oct 08 '14 at 11:01
  • 30
    Note: While these instructions still apply to Visual Studio 2015, the location of `applicationhost.config` has changed. More sensibly than previous versions, it is now located in the `.vs\config` sub folder of your solution folder. – Richard Moss Aug 27 '15 at 00:03
  • Is it possible to add a separate application to the virtual directory in IIS express so that I could have two applications under one domain ? – Denys Alexieiev Feb 11 '20 at 15:02
  • I don't know if what I am doing is so different from the above, but this is not working for what I want to do. I have a solution with 2 website projects. On each one, I want to add a virtual directory (in my case, '/resources' to both, so they can share the same image files). I tried modifying my user's IISExpress config file, but vdir contents don't work in the running debuggers of either website. Am I missing something obvious? – Jay Imerman Feb 09 '21 at 01:10
93

@Be.St.'s aprroach is true, but incomplete. I'm just copying his explanation with correcting the incorrect part.

IIS express configuration is managed by applicationhost.config.
You can find it in

Users\<username>\Documents\IISExpress\config folder.

Inside you can find the sites section that hold a section for each IIS Express configured site.

Add (or modify) a site section like this:

<site name="WebSiteWithVirtualDirectory" id="20">
   <application path="/" applicationPool="Clr4IntegratedAppPool">
     <virtualDirectory path="/" physicalPath="c:\temp\website1" />
     <virtualDirectory path="/OffSiteStuff" physicalPath="d:\temp\SubFolderApp" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="*:1132:localhost" />
   </bindings>
</site>

Instead of adding a new application block, you should just add a new virtualDirectory element to the application parent element.

Edit - Visual Studio 2015

If you're looking for the applicationHost.config file and you're using VS2015 you'll find it in:

[solution_directory]/.vs/config/applicationHost.config

Rob
  • 10,004
  • 5
  • 61
  • 91
Fevzi Apaydın
  • 1,050
  • 7
  • 7
  • 4
    Is there anyway to apply this within the Visual Studio project? In a multi-developer environment, if someone else check's out the code on their machine, then their local IIS Express wouldn't be configured with the virtual directory and cause runtime errors wouldn't it? – Andrew Hillier Sep 03 '15 at 23:06
  • 4
    In Visual Studio 2015, the IIS config file is stored in the solution directory, in a folder called .vs/config, so you can check it into source control if you want. – Randy Gamage Nov 11 '15 at 18:19
  • So many non-answers flooding google results for this question in relation to VS 2012. This works!!!! – Tim Ogilvy May 06 '17 at 01:46
  • Is it possible to add a separate application to the virtual directory in IIS express so that I could have two applications under one domain ? – Denys Alexieiev Feb 11 '20 at 15:02
8

In VS2013 I did this in the following steps:

1.Right-click the web application project and hit Properties

2.View the "Web" tab of the Properties page

3.Under Servers, with "IIS Express" being the default choice of the dropdown, in the "Project Url" change the url using the port number to one that suits you. For example I deleted the port number and added "/MVCDemo4" after the localhost.

4.Click the "Create Virtual Directory" button.

5.Run your project and the new url will be used

user2765861
  • 163
  • 2
  • 5
  • 1
    This doesnt let you configure the physical path. – Victorio Berra Jul 28 '14 at 14:03
  • OK, with a little experimentation: if you change your webroot to have an application URI and click Create Virtual Directory, it creates an applicationhost.config under project path .vs\{projectname}\config. You can edit this .config file and change the physicalPath property of the virtual directory. I don't know if this gets checked into SCM yet, I will have to find out. – Jay Imerman Feb 09 '21 at 01:35
3

If you're using Visual Studio 2013 (may require Pro edition or higher), I was able to add a virtual directory to an IIS Express (file-based) website by right-clicking on the website in the Solution Explorer and clicking Add > New Virtual Directory. This added an entry to the applicationhost.config file as with the manual methods described here.

Bryan B
  • 720
  • 6
  • 16
2

A new option is Jexus Manager for IIS Express,

https://blog.lextudio.com/2014/10/jexus-manager-for-iis-express/

It is just the management tool you know how to use.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
1

I had to make the entry in the [project].vs\config\applicationhost.config file.

Prior to this, it worked from deployment but not from code.

TheJoe
  • 57
  • 10
1

In answer to the further question -

"is there anyway to apply this within the Visual Studio project? In a multi-developer environment, if someone else check's out the code on their machine, then their local IIS Express wouldn't be configured with the virtual directory and cause runtime errors wouldn't it?"

I never found a consistant answer to this anywhere but then figured out you could do it with a post build event using the XmlPoke task in the project file for the website -

<Target Name="AfterBuild">
    <!-- Get the local directory root (and strip off the website name) -->
    <PropertyGroup>
        <LocalTarget>$(ProjectDir.Replace('MyWebSite\', ''))</LocalTarget>
    </PropertyGroup>

    <!-- Now change the virtual directories as you need to -->
    <XmlPoke XmlInputPath="..\..\Source\Assemblies\MyWebSite\.vs\MyWebSite\config\applicationhost.config" 
        Value="$(LocalTarget)AnotherVirtual" 
        Query="/configuration/system.applicationHost/sites/site[@name='MyWebSite']/application[@path='/']/virtualDirectory[@path='/AnotherVirtual']/@physicalPath"/>
</Target>

You can use this technique to repoint anything in the file before IISExpress starts up. This would allow you to initially force an applicationHost.config file into GIT (assuming it is ignored by gitignore) then subsequently repoint all the paths at build time. GIT will ignore any changes to the file so it's now easy to share them around.

In answer to the futher question about adding other applications under one site:

You can create the site in your application hosts file just like the one on your server. For example:

  <site name="MyWebSite" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\GIT\MyWebSite\Main" />
      <virtualDirectory path="/SharedContent" physicalPath="C:\GIT\SharedContent" />
      <virtualDirectory path="/ServerResources" physicalPath="C:\GIT\ServerResources" />
    </application>
    <application path="/AppSubSite" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\GIT\AppSubSite\" />
      <virtualDirectory path="/SharedContent" physicalPath="C:\GIT\SharedContent" />
      <virtualDirectory path="/ServerResources" physicalPath="C:\GIT\ServerResources" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:4076:localhost" />
    </bindings>
  </site>

Then use the above technique to change the folder locations at build time.

AngryDaz
  • 71
  • 5
  • Is it possible to add a separate application to the virtual directory in IIS express so that I could have two applications under one domain ? – Denys Alexieiev Feb 11 '20 at 15:02
1

Visual Studio Studio Professional 2017

  • Go to the /Solution/.vs directory
  • Open applicationHost.config file Expand <Sites> node and look for the correct child node (based on running port) `
  • Add <virtualDirectory path="/MyVirtualDirectory" physicalPath="D:\MyVirtualDirectoryPhysicalPath" />`
TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81
0

I had something else, the files itself where inaccessible in a SBS envirenment.

Delete the files in the config folder (if you can't open them!) and replace them with a copy of the folder on your own local pc.

Fixed it for me :)

NicoJuicy
  • 3,435
  • 4
  • 40
  • 66
  • Files in Users\\Documents\IISExpress\config are per-user protected. Maybe you've tried to edit it with a different user. So to edit it you need to run Notepad (or your editor) in Administration mode. In this way you are able to modify it. Or delete and copy like you did :-) – Be.St. May 17 '13 at 15:52