在 asp5 dnx 项目中获取 json 文件
Getting a json file in asp5 dnx project
我想使用 json 文件中的属性
[
{
"Name:"Foo",
"AnotherName":"Bar"
}
]
它位于我的 App_Data 根文件夹中
在class我只想return那个文件
public class FooRepo{
internal List<FooList> Get(){
var filePath = ????
}
}
我试过了
Server.MapPath("~/App_Data/foo.json");
, HttpContext.Current.Server.MapPath("~/App_Data/foo.json"")
,
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
我似乎无法弄清楚如何让这个文件使用它。这是有关主题的文档 https://docs.asp.net/en/latest/fundamentals/file-system.html
We are currently working on this topic.
在 ASP.NET5 应用程序中使用来自 json 文件的配置数据的最简单方法是在 IConfIigurationRoot
中创建类型为 IConfIigurationRoot
的静态 属性 =15=] class,例如
public static IConfigurationRoot Configuration
将以下代码添加到 Startup 的构造函数中 class
public Startup(IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
显然,您必须更改传递给 SetBasePath
方法的值,以确保您的应用程序可以找到 json 文件。
您可以通过静态 属性
从 class 访问配置数据
private Startup _st;
public FooRepo(Startup st)
{
_st = st;
}
var emailAddress = _st.Configuration["AppSettings:EmailAddress"];
这是我的 json 文件的布局
注意: 我的 json 文件名为 config.json 并已添加到我的项目的根文件夹中。
{
"AppSettings": {
"EmailAddress": "info@somedomain.com"
},
}
我想使用 json 文件中的属性
[
{
"Name:"Foo",
"AnotherName":"Bar"
}
]
它位于我的 App_Data 根文件夹中
在class我只想return那个文件
public class FooRepo{
internal List<FooList> Get(){
var filePath = ????
}
}
我试过了
Server.MapPath("~/App_Data/foo.json");
, HttpContext.Current.Server.MapPath("~/App_Data/foo.json"")
,
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
我似乎无法弄清楚如何让这个文件使用它。这是有关主题的文档 https://docs.asp.net/en/latest/fundamentals/file-system.html
We are currently working on this topic.
在 ASP.NET5 应用程序中使用来自 json 文件的配置数据的最简单方法是在 IConfIigurationRoot
中创建类型为 IConfIigurationRoot
的静态 属性 =15=] class,例如
public static IConfigurationRoot Configuration
将以下代码添加到 Startup 的构造函数中 class
public Startup(IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
显然,您必须更改传递给 SetBasePath
方法的值,以确保您的应用程序可以找到 json 文件。
您可以通过静态 属性
从 class 访问配置数据 private Startup _st;
public FooRepo(Startup st)
{
_st = st;
}
var emailAddress = _st.Configuration["AppSettings:EmailAddress"];
这是我的 json 文件的布局
注意: 我的 json 文件名为 config.json 并已添加到我的项目的根文件夹中。
{
"AppSettings": {
"EmailAddress": "info@somedomain.com"
},
}