0

I take NullReferenceException when I want to add addJsonFile. I made exaclty what lynda(http://www.lynda.com/ASP-NET-tutorials/Dynamically-control-behavior-custom-configuration/368051/431234-4.html) said. (screenShot)

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);
    }
}
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Cembora
  • 244
  • 2
  • 15
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – René Vogt Feb 11 '16 at 09:40
  • Try research your Exception with the link that Rene gave to you. I you can't find it, please post your full stacktrace here. The complete StackTrace is essential to find the error. – etalon11 Feb 11 '16 at 09:42

1 Answers1

0

Try the following code.

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; }
}
dudu
  • 39
  • 1
  • 14