【发布时间】:2018-04-10 17:49:42
【问题描述】:
很快: 我的创业课程看起来像这样:
public Startup(IHostingEnvironment env)
{
this.env = env;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.RespectBrowserAcceptHeader = true;
options.FormatterMappings.SetMediaTypeMappingForFormat(
"xml", MediaTypeHeaderValue.Parse("text/xml"));
options.FormatterMappings.SetMediaTypeMappingForFormat(
"json", MediaTypeHeaderValue.Parse("application/json"));
})
.AddXmlSerializerFormatters();
var dbConfig = Configuration.GetSection(nameof(DbOptions)).Get<DbOptions>();
services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(dbConfig.ConnectionString));
}
并且: 虽然我在本地主机上一切正常,但是当我发布到网络时,应用程序会在每个请求上抛出 Status: 500 Internal server error。我想知道如果我更换
var dbConfig = Configuration.GetSection(nameof(DbOptions)).Get<DbOptions>();
services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(dbConfig.ConnectionString));
与
services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(@"connectionstring"));
它在本地和外部都像魅力一样工作,所以我猜问题在于 appsettings.js 的解析,但它有什么问题? appsettings.Development.json 基本上只是 appsettings.json 的复制粘贴
【问题讨论】:
-
你能在连接字符串密码之后分享你的 appSettings 吗?
-
您收到什么错误?
-
@Rabur 要获得确切的错误,您可以在应用程序设置中添加这两个设置
ASPNETCORE_DETAILEDERRORS= true 和ASPNETCORE_ENVIRONMENT=Development。当您使用 Postman 访问 APi 时,这将为您提供详细的错误信息。等 -
@ShaswatRungta@HenkMollema 错误说:ArgumentNullException 参数名称:connectionString Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName) Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName) Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action
sqlServerOptionsAction) GamesBoard.Startupc__DisplayClass9_0. b__1(DbContextOptionsBuilder opt) in Startup.cs -
@AdemCatamak 它基本上看起来像这样:{ ... "DbOptions": { "ConnectionString": "Server=mssql.SERWERPATH;Database=youknow;Uid=myUsername;Password=SecretStuff;", "DefaultSchema": "gb" }, ... }
标签: c# asp.net-core .net-core