【问题标题】:hostfile.json webroot property not serving static fileshostfile.json webroot 属性不提供静态文件
【发布时间】:2016-06-02 04:37:18
【问题描述】:

疯了:-)

我已经安装了最新的 ASP.NET Core (RC2)。

我希望能够创建一个带有多个*.csproj*.sln:一个用于服务器端开发,一个用于客户端开发。

我们将它们分开的原因是,我们可以选择将客户端*.csproj 提供给具有更好的 UI 技能的外部开发人员进行工作,而无需对服务器端代码了解太多。他们可以使用 Visual Studio Code 或其他轻量级 IDE 在客户端 html/js 上工作,而不需要 Visual Studio 参与。

在客户端 *.csproj 中,我想为一个角度项目提供静态文件 (html/js/css),来自根目录,而不是来自 wwwroot em> 目录,因此 gulpfile.js 相对路径等与在没有 Visual Studio 的情况下设置 Angular 项目的方式相同。

据我了解,现在的规则是: * 如果hosting.json 文件存在,则使用hosting.json 中的webroot 设置。 * 否则,使用 wwwroot。 * 如果缺少,请使用 root。 * 见:https://github.com/aspnet/Hosting/issues/450

首先,检查我是否设置了静态页面路由。创建了一个 wwwroot/index.html 页面。多田!有效。

现在,将目录重命名为 app/ 并更新 hosting.json 以指向它。重新加载项目后,app/ 文件夹更改了图标..好...运行...不高兴。与它战斗一段时间。没有成功...

然后将app/ 文件夹和hosting.json 文件一并删除。最终肯定想扔东西......

我获取静态文件的唯一方法是该文件夹名为wwwroot。我是否有hosting.json 文件。

这与文档相反:https://github.com/aspnet/Hosting/issues/450

还有其他人成功摆脱 wwwroot 文件夹吗?如果是这样……怎么办?!?!?

谢谢!

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    尽管您可以打开 ASP.NET Core 应用程序的根以提供静态文件,但这不是一个好主意,因为一旦您这样做了,就没有什么可以阻止某人导航到 project.json 或根目录中的任何其他文件。

    话虽如此,这就是您在 wwwroot 之外提供静态文件的方法。

    首先,创建一个返回 IApplicationBuilder 的静态类。在这里,您将定义可访问的物理路径以及对该路径的可选 URL 重写(参见代码中的 cmets):

    using System.IO;
    
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting.Internal;
    using Microsoft.Extensions.FileProviders;
    
    public static class ApplicationBuilderExtensions
    {
        public static IApplicationBuilder UseRootResources(this IApplicationBuilder app, HostingEnvironment env)
        {
            //var path = Path.Combine(env.ContentRootPath); // WARNING - this opens up the root of the app
            var path = Path.Combine(env.ContentRootPath, "static"); // this would allow for serving up contents in a physical path folder named 'static'
            var provider = new PhysicalFileProvider(path);
    
            var options = new StaticFileOptions();
            // the below line re-writes the path name of the physical path, it can have the string value of anything you want to call it
            options.RequestPath = ""; //  in this example, an empty string will give the *appearance* of it being served up from the root
            //options.RequestPath = "/static"; // this will use the URL path named static, but could be any made-up name you want
            options.FileProvider = provider;
    
            app.UseStaticFiles(options);
            return app;
        }
    }
    

    接下来,在 Startup.cs 中,从 Configure 方法调用该函数:

    app.UseRootResources((HostingEnvironment)env);
    

    现在您可以在 wwwroot 之外提供静态文件了!在 HTML 中引用静态文件将使用您在 Options.RequestPath 中定义的路径,该路径在 ApplicationBuilderExtensions 类中设置。假设您将 RequestPath 保留为空字符串以模拟根目录,然后由于 URL 重写的魔力,您可以调用资源就像它存在于那里一样(即使它确实存在于“静态”文件夹中):

    <img src="bus.jpg"/>
    

    【讨论】:

    猜你喜欢
    • 2012-06-15
    • 2018-10-23
    • 2021-07-08
    • 2013-06-02
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多