首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Configuration.AddJsonFile("config.json") NullReferenceException

Configuration.AddJsonFile("config.json") NullReferenceException
EN

Stack Overflow用户
提问于 2016-02-11 17:39:45
回答 1查看 381关注 0票数 0

当我想要添加addJsonFile时,我会使用NullReferenceException。我把琳达(http://www.lynda.com/ASP-NET-tutorials/Dynamically-control-behavior-custom-configuration/368051/431234-4.html)说的话都说对了。(screenShot)

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNet.Diagnostics;
using Microsoft.Framework.Internal;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.ConfigurationModel.Json;

namespace PortalDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            var config = new Configuration();

            config.AddEnvironmentVariables();
            config.AddJsonFile("config.json");//Here

            if (config.Get("debug") == "True")
            {
                app.UseRuntimeInfoPage();
                app.UseDeveloperExceptionPage();

            }
            app.UseExceptionHandler("/home/errorPage");

            app.UseMvc(routes=> 
            routes.MapRoute("Default","{controller=Home}/{action=Index}/{id?}"));

            app.UseStaticFiles();  
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-10 18:24:27

尝试以下代码。

代码语言:javascript
复制
public class Startup
{
    public Startup(IApplicationEnvironment appEnv)
    {
        IConfigurationBuilder builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json", false);
        Configuration = builder.Build();           
    }

    public IConfiguration Configuration { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35335573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档