C# 如何从asp.net core中的外部Json文件访问数据?

C# 如何从asp.net core中的外部Json文件访问数据?,c#,json,asp.net-core,C#,Json,Asp.net Core,我想访问文件目录中json文件中的数据 我尝试过使用StreamReader,但它没有反序列化数据 string json = r.ReadToEnd(); WorkFlowConfiguration items = JsonConvert.DeserializeObject<WorkFlowConfiguration>(json); Debug.WriteLine(items.WorkFlowAction); Console.WriteLine(items.WorkF

我想访问文件目录中json文件中的数据

我尝试过使用
StreamReader
,但它没有反序列化数据

string json = r.ReadToEnd();

WorkFlowConfiguration items = 
    JsonConvert.DeserializeObject<WorkFlowConfiguration>(json);

Debug.WriteLine(items.WorkFlowAction);
Console.WriteLine(items.WorkFlowAction);

items.WorkFlowAction.ForEach(el => Console.WriteLine(el.ToString()));
string json=r.ReadToEnd();
WorkFlowConfiguration项=
反序列化对象(json);
Debug.WriteLine(items.WorkFlowAction);
Console.WriteLine(items.WorkFlowAction);
items.WorkFlowAction.ForEach(el=>Console.WriteLine(el.ToString());

如果要从启动类访问

"MySettings": {  
    "DbConnection": "abc",  
    "Email": "abc@domain.com",  
    "SMTPPort": "5605"  
  }  


public Startup(IConfiguration configuration)
{   
     var dbConnection= configuration["MySettings:DbConnection"];
}
或者,如果您想在控制器中使用IConfiguration,则必须在构造函数中插入IConfiguration

public UserController(IConfiguration configuration)  
{  
   configuration = configuration["MySettings:DbConnection"]; 
}  

获取外部json文件有两种方法,如下所示:

1.型号:

public class Test
{
    public string status { get; set; }
    public int code { get; set; }
    public string message { get; set; }
}
2.Json文件:

  {
    "status": "succeess",
    "code": 1,
    "message": "login succeeded!"
  }
3.第一种方式(使用
StreamReader
):


在代码片段的第1行之后的
string json
变量中,您能看到文件内容吗?在这段代码中您有没有发现任何错误?什么错误?你现在得到了什么结果?你期待什么?@rena非常感谢你的合作。我的问题解决了。现在它正在工作。问题在于访问语法。
using (StreamReader r = new StreamReader("C:\\test.json"))
    {
         string json = r.ReadToEnd();
         Test item =JsonConvert.DeserializeObject<Test>(json);
    }
var jsonString = System.IO.File.ReadAllText("C:\\test.json");
Test items =JsonConvert.DeserializeObject<Test>(jsonString);