2016-07-29 164 views
0

雖然我在項目目錄中有需要的文件,但我收到文件未找到錯誤。沒有編譯錯誤。未找到文件異常Visual Studio 2015

這裏是我startup.cs類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.Http; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Microsoft.Extensions.Configuration; 

namespace OdeToFood 
{ 
public class Startup 
{ 
    public Startup() 
    { 
     var builder = new ConfigurationBuilder() 
      .AddJsonFile("config.json"); 
     Configuration = builder.Build(); 
    } 

    public IConfiguration Configuration { get; set; } 

    // 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) 
    { 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
     } 

     app.Run(async (context) => 
     { 
      var message = Configuration["greeting"]; 
      await context.Response.WriteAsync(message); 
     }); 
    } 
} 
} 

這是錯誤消息時,我建我得到它。

System.IO.FileNotFoundException: The configuration file 'config.json' was not found and is not optional. 
    at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) 
    at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) 
    at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 
    at OdeToFood.Startup..ctor() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) 
    at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProvider services, Type startupType, String environmentName) 
    at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>c__DisplayClass1_0.b__1(IServiceProvider sp) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup() 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 
    at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 

屏幕快照,顯示當前文件

json file

我應該如何糾正呢?

感謝

回答

0

我想你忘記使用Server.MapPath功能。 您可以編輯Constructor與下面的代碼:

public Startup() 
    { 
     var builder = new ConfigurationBuilder() 
      .AddJsonFile(HttpContext.Current.Server.MapPath("~/config.json")); 
     Configuration = builder.Build(); 
    } 

希望它可以幫助你:)

+0

我得到「的HttpContext」不包含「內容」的錯誤的定義 – re3el

+0

試試這個:'系統。 Web.HttpContext.Current.Server.MapPath(「〜/ config.json」)' –

+0

和你在哪裏使用'Content'? –