简体   繁体   English

wwwroot中的vNext / .NET5 config.json

[英]vNext / .NET5 config.json in wwwroot

So far as i am thinking, it is now preferred to use config.json files to store configuration settings for you application, which is great. 到目前为止,我现在更喜欢使用config.json文件来存储应用程序的配置设置,这很棒。 Love json format and find it easier to read. 喜欢json格式,并且更容易阅读。

Upon publishing my first vNext application though, i notice that the config.json file seems to publish into 在发布我的第一个vNext应用程序时,我注意到config.json文件似乎发布到

approot/src/my_project/config.json

and my website are in a folder called wwwroot 我的网站位于名为wwwroot的文件夹中

My web server has multiple websites setup and each one used to have its own "app.config" file, which i could put in the website directory with settings specific to that website being run. 我的网络服务器有多个网站设置,每个网站都有自己的“app.config”文件,我可以把它放在网站目录中,并运行特定于该网站的设置。 But now i have 2 problems, 但现在我有两个问题,

  1. config.json is populated into the wrong directory config.json填充到错误的目录中

  2. If i do put the config.json file into the wwwroot folder, then it can be accessed by the client via the browser. 如果我确实将config.json文件放入wwwroot文件夹,那么客户端可以通过浏览器访问它。 This is very dangerous. 这非常危险。

Do i have to setup my web server to ignore requests for config.json if its in wwwroot, or am i missing something completely? 我是否必须设置我的Web服务器以忽略对config.json的请求,如果它在wwwroot中,或者我完全错过了什么?

You should have a separate folder for each project. 每个项目都应该有一个单独的文件夹。 And never place a folder in wwwroot that you don't want served out. 永远不要在wwwroot中放置一个你不希望服务的文件夹。 It could end up looking something like this: 它可能最终看起来像这样:

/root
/root/Project1
/root/Project1/config.json
/root/Project1/wwwroot
/root/Project2
/root/Project2/config.json
/root/Project2/wwwwroot

Obviously, your structure doesn't have to exactly match. 显然,您的结构不必完全匹配。 In general though, if you use separate directories to separate your projects, and don't put your config in your wwwroot, you'll be fine. 一般来说,如果你使用单独的目录来分隔你的项目,并且不把你的配置放在你的wwwroot中,你会没事的。

By the way, you may want to avoid having anything secret in the config.json file in the first place. 顺便说一句,您可能希望首先避免在config.json文件中有任何秘密。 You should be using source code control software, and you should avoid a situation where secret stuff gets checked in. Perhaps environment variables or the secrets API . 应该使用源代码控制软件,并且应该避免检查秘密内容的情况。可能是环境变量或秘密API

Thanks to mason for his answer about structuring websites on your webserver with regards to vNext, but there is also another option worth mentioning here, that is why i have add another answer. 感谢mason关于在网络服务器上构建关于vNext的网站的答案,但是还有另外一个值得一提的选项,这就是为什么我要添加另一个答案。

As stated above, i have the config.json file in my project for developing, and to get the file i was using this in my Startup.cs file 如上所述,我在我的项目中有config.json文件用于开发,并获取我在Startup.cs文件中使用它的文件

public Startup(IApplicationEnvironment appEnv)
{
    var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath.)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = builder.Build();
}

This includes the file in the approot when publishing. 这包括发布时的批准文件。 Including it in the wwwroot is not an option either. 将其包含在wwwroot中也不是一种选择。 So using the file structure, where you would have a folder containing both the approot and wwwroot folders, i changed the code to this, which works for development and publishing. 因此,使用文件结构,您将拥有包含approot和wwwroot文件夹的文件夹,我将代码更改为此,这适用于开发和发布。

public Startup(IHostingEnvironment env)
{
    // configuration is in the folder above the wwwroot
    var builder = new ConfigurationBuilder(env.MapPath("..\\"))
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = builder.Build();
}

However, when you publish the code/site, the config.json file is still put in the approot folder. 但是,当您发布代码/站点时,config.json文件仍然放在approot文件夹中。 To get round this, you must can it to the project.json file, like so 为了解决这个问题,你必须将它放到project.json文件中,就像这样

"publishExclude": [
    "node_modules",
    "bower_components",
    "config.json",
    "**.xproj",
    "**.user",
    "**.vspscc"
],
"exclude": [
    "wwwroot",
    "config.json",
    "node_modules",
    "bower_components"
]

Now the file can be in the project root so you can save it too source control, it works when using IIS Express for developing and it is not published either. 现在该文件可以位于项目根目录中,因此您可以将其保存为源代码控制,它在使用IIS Express进行开发时也可以正常工作,也不会发布。

Hope this help someone else 希望这有助于其他人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM