简体   繁体   English

从 ChatBot 中的项目文件夹中读取 JSON 文件

[英]Read JSON file from project folder in ChatBot

I would like to read json files to present an adaptive card, but I am missing how to get the path to the files.我想读取 json 文件以呈现自适应卡,但我想知道如何获取文件的路径。 Like I have a card_a.json file in my wwwwroot folder.就像我的 wwwwroot 文件夹中有一个 card_a.json 文件。

public async Task<string> GetCardText(string cardName)
{
    var path = $"/wwwroot/{cardName}.json";
    if (!File.Exists(path))
        return string.Empty;

    using (var f = File.OpenText(path))
    {
        return await f.ReadToEndAsync();
    }
}

I tried $"/wwwroot/{cardName}.json" , $"/{cardName}.json" and $"{cardName}.json" , none of them worked.我试过$"/wwwroot/{cardName}.json" , $"/{cardName}.json"$"{cardName}.json" ,它们都没有工作。
I've read this article, but didn't help, the sample fails here:我已经阅读了这篇文章,但没有帮助,示例在此处失败:

var path = HostingEnvironment.MapPath($"/Cards/{cardName}.json");

With an error: HostingEnvironment does not contain a definition for MapPath.出现错误:HostingEnvironment 不包含 MapPath 的定义。 Adding using System.Web didn't solve this issue.使用 System.Web 添加并没有解决这个问题。 VS says that adding this is unnecessary. VS 说添加这个是不必要的。

Does the cardname.json file need to reside in the wwwroot folder? cardname.json 文件是否需要驻留在 wwwroot 文件夹中? You could save a lot of permission and path problems by storing them in the application root, or a subfolder of that.通过将它们存储在应用程序根目录或其子文件夹中,您可以节省大量权限和路径问题。

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

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