【问题标题】:File Not Found Exception Visual Studio 2015找不到文件异常 Visual Studio 2015
【发布时间】:2016-07-29 13:39:44
【问题描述】:

虽然我在项目目录中有所需的文件,但我收到文件未找到错误。没有编译错误。

这是我的 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 visual-studio-2015 asp.net-core-mvc


    【解决方案1】:

    我想你忘了使用Server.MapPath 函数。 您可以使用以下代码编辑您的Constructor

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

    希望对你有帮助:)

    【讨论】:

    • 我收到“HTTPContext”不包含“内容”错误的定义
    • 试试这个:System.Web.HttpContext.Current.Server.MapPath("~/config.json")
    • 你在哪里使用Content
    • the type or namespace web doesn't exist in the namespace "System" - 我现在收到了。 Content 是什么?
    • 您在 Visual Studio 中使用的语言是 C# 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多