We have an application using ASP.NET Core 1.0 RC1 and hosted on IIS. It works fine. Now we have static content, that is available on a file share and should be accessible from the application.
Before ASP.NET 5, we added a virtual directory in IIS and could access the shared content easily. With our hosted ASP.NET 5 application, this unfortunately doesn't seem to work. We just get a 404
back when trying to access the static content.
Our application is using app.UseIISPlatformHandler()
and app.UseStaticFiles()
, but this doesn't work. We discovered that we could use app.UseFileServer()
with custom FileServerOptions
to get the desired behavior, but we are curious if it's also possible with the normal "old" way of adding a virtual directory in IIS.
You can use Internet Information Services Manager to create a virtual directory for an ASP.NET Web application that is hosted in IIS. The virtual directory name becomes part of the application's URL.
To host an ASP.NET Core app as a sub-app under another ASP.NET Core app: Establish an app pool for the sub-app. Add the root site in IIS Manager with the sub-app in a folder under the root site. Right-click the sub-app folder in IIS Manager and select Convert to Application.
If you add a virtual directory to your ASP.NET Core application in IIS, it isn’t recognized and a 404 is returned. It’s because of DNX/Kestrel, which is running beneath IIS (using the HttpPlatformHandler module) and to which IIS only brokers the requests. Kestrel doesn’t know anything of virtual directories from IIS.
On the hosting system, create a folder to contain the app's published folders and files. In a following step, the folder's path is provided to IIS as the physical path to the app. For more information on an app's deployment folder and file layout, see ASP.NET Core directory structure.
I have found a blog which I think must have been written by the OP.
The upshot is not to use a virtual directory in IIS at all but rather map your path in Startup.cs to the physical server directory. I hope the OP does not mind that I have pasted the blog below, but it helped me when I first encountered this problem today.
Source: https://www.jauernig-it.de/asp-net-coreiis-serving-content-from-a-file-share/
There are situations, when you want to serve static content with your application, that is not part of it, e.g. because it exists on a common file share. Website content, that is managed by a business division, could be such a use case. In ASP.NET before Core, this was no problem in IIS: just create a virtual directory within your IIS website and point it to the file share.
Unfortunately, with ASP.NET Core, this approach isn’t working any longer. If you add a virtual directory to your ASP.NET Core application in IIS, it isn’t recognized and a 404 is returned. It’s because of DNX/Kestrel, which is running beneath IIS (using the HttpPlatformHandler module) and to which IIS only brokers the requests. Kestrel doesn’t know anything of virtual directories from IIS. And because ASP.NET Core applications are independent from IIS and could also be run without it (e.g. running Kestrel standalone), that should be considered as a good thing.
But now we need another solution for our problem… fortunately, ASP.NET Core gives us a programmatic interface to serve files from anywhere. Just add the following code to your Startup.cs Configure() method:
app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider(@"\\server\path"), RequestPath = new PathString("/MyPath"), EnableDirectoryBrowsing = false });
What this essentially does, is adding a file server to a physical server path, that is then available on a certain request path, in this case with directory browsing disabled. You are also able to serve from a path relative to your application, using new PhysicalFileProvider(env.WebRootPath + "\path") (given env is of type IHostingEnvironment as parameter of Configure()). Voila, that’s it. There is no need to add a „virtual directory“ in IIS, this stuff is deprecated and a thing of the past. For me, this is a good thing, because we get more independent of the whole IIS…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With