ChatGPT解决这个技术问题 Extra ChatGPT

Creating virtual directories in IIS express

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?

For Visual Studio 2015, mikedice417.wordpress.com/2015/09/13/… worked. The applicationHost.config file is under the project root: ${PROJECT}\.vs\config\applicationHost.config.
Here there is an answer that could help you: https://stackoverflow.com/a/46260607/2472664

B
Be.St.

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

Users\\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:

Application tag for a Site

Virtual Directory tag


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"
In the bindings section you see the url and port used to access the root of your site. So: localhost:1132 is the url for the web application installed in c:\temp\website1 localhost:1132/OffSiteStuff is the url for the web application installed in d:\temp\SubFolderApp.
Anyone know if/how I can configure settings separately for each project (so I can check in the config)? Thanks
@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.
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.
R
Rob

@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\\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


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?
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.
So many non-answers flooding google results for this question in relation to VS 2012. This works!!!!
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 ?
u
user2765861

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


This doesnt let you configure the physical path.
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.
B
Bryan B

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.


L
Lex Li

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.


T
TheJoe

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.


A
AngryDaz

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.


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 ?
i
immirza

Visual Studio Studio Professional 2017

Go to the /Solution/.vs directory

Open applicationHost.config file Expand node and look for the correct child node (based on running port) `

Add `


N
NicoJuicy

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 :)


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 :-)