IIS 中 ASP.NET Core 应用程序内的虚拟目录 [英] Virtual directory inside of ASP.NET Core app in IIS

查看:120
本文介绍了IIS 中 ASP.NET Core 应用程序内的虚拟目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用 ASP.NET Core 1.0 RC1 并托管在 IIS 上的应用程序.它工作正常.现在我们有了静态内容,可以在文件共享中使用,并且应该可以从应用程序访问.

在 ASP.NET 5 之前,我们在 IIS 中添加了一个虚拟目录,可以轻松访问共享内容.不幸的是,对于我们托管的 ASP.NET 5 应用程序,这似乎不起作用.尝试访问静态内容时,我们只会返回 404.

我们的应用程序正在使用 app.UseIISPlatformHandler()app.UseStaticFiles(),但这不起作用.我们发现我们可以使用带有自定义 FileServerOptionsapp.UseFileServer() 来获得所需的行为,但我们很好奇是否也可以使用正常的旧"方式在 IIS 中添加虚拟目录.

解决方案

我发现了一个我认为肯定是 OP 写的博客.

结果根本不是在 IIS 中使用虚拟目录,而是将您在 Startup.cs 中的路径映射到物理服务器目录.我希望 OP 不介意我粘贴了下面的博客,但它在我今天第一次遇到这个问题时帮助了我.

来源:https://www.jauernig-it.de/asp-net-coreiis-serving-content-from-a-file-share/

<块引用>

在某些情况下,当您想为应用程序提供静态内容时,这不是它的一部分,例如因为它存在于公共文件共享中.由业务部门管理的网站内容可能就是这样一个用例.在 Core 之前的 ASP.NET 中,这在 IIS 中没有问题:只需在 IIS 网站中创建一个虚拟目录并将其指向文件共享.

不幸的是,对于 ASP.NET Core,这种方法不再适用.如果在 IIS 中向 ASP.NET Core 应用程序添加虚拟目录,则无法识别该目录并返回 404.这是因为 DNX/Kestrel 在 IIS 下运行(使用 HttpPlatformHandler 模块)并且 IIS 仅向其代理请求.Kestrel 不知道来自 IIS 的任何虚拟目录.而且因为 ASP.NET Core 应用程序独立于 IIS,也可以在没有它的情况下运行(例如,独立运行 Kestrel),这应该被认为是一件好事.

但是现在我们需要另一个解决方案来解决我们的问题……幸运的是,ASP.NET Core 为我们提供了一个编程接口,可以从任何地方提供文件.只需将以下代码添加到您的 Startup.cs Configure() 方法中:

app.UseFileServer(new FileServerOptions{FileProvider = new PhysicalFileProvider(@"\serverpath"),RequestPath = new PathString("/MyPath"),EnableDirectoryBrowsing = false});

<块引用>

这实际上是将文件服务器添加到物理服务器路径,然后在某个请求路径上可用,在这种情况下禁用目录浏览.您还可以使用 new PhysicalFileProvider(env.WebRootPath + "path")(给定 env 类型为 IHostingEnvironment 作为 Configure() 的参数)从相对于您的应用程序的路径提供服务.瞧,就是这样.无需在 IIS 中添加虚拟目录",这些东西已被弃用,已成为过去.对我来说,这是一件好事,因为我们更加独立于整个 IIS……

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.

解决方案

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(@"\serverpath"),
    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…

这篇关于IIS 中 ASP.NET Core 应用程序内的虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆