【发布时间】:2017-05-04 12:27:31
【问题描述】:
我正在使用 ASP.NET Core 1.1.0 开发 Web 应用程序。我的应用程序使用 IIS Express。但是当我将应用程序部署到 IIS 时,出现错误“找不到包'Microsoft.AspNetCore.Antiforgery'的编译库位置”。
我已从 project.json 中删除 preserveCompilationContext,但我得到“缺少一个或多个编译引用。可能的原因包括应用程序的“buildOptions”下缺少“preserveCompilationContext”属性project.json.”错误消息。
静态文件(如 .html)运行良好。
我该如何解决这个问题?
project.json
{
"dependencies": {
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0",
"Microsoft.AspNetCore.Diagnostics": "1.1.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0",
"Microsoft.AspNetCore.Mvc": "1.1.0",
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Routing": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.1.0",
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.NETCore.App": "1.1.0"
},
"tools": {
},
"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"nowarn": [],
"copyToOutput": [ "appsettings.json", "appsettings.staging.json" ]
},
"runtimes": {
"win10-x64": {}
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"appsettings.staging.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ],
"precompile": [ "C:\\Windows\\System32\\inetsrv\\appcmd recycle apppool /apppool.name:local.com" ]
}
}
Startup.cs
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Web.Management
{
public class Startup
{
public Startup(IHostingEnvironment 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 IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options =>
{
});
// Add framework services.
services
.AddMvc(options =>
{
options.Filters.Add(new ExceptionFilter());
})
.AddViewOptions(options =>
{
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
// 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(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseLogger();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider(),
new AcceptLanguageHeaderRequestCultureProvider()
}
};
app.UseRequestLocalization(localizationOptions);
HttpContextMiddleware.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
//app.UseStatusCodePages();
app.UseStaticFiles();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "AuthCookie",
AutomaticAuthenticate = true,
AutomaticChallenge = false,
LoginPath = new PathString("/login"),
CookieSecure = CookieSecurePolicy.SameAsRequest
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
【问题讨论】:
-
从 project.json 中删除
preserveCompilationContext之前出现什么错误? -
我收到错误在删除preserveCompilationContext之前找不到包'Microsoft.AspNetCore.Antiforgery'的编译库位置。
-
你试过命令行
dotnet restore吗? -
我现在试过了,但没有任何改变。
-
你遇到了一个问题,然后改变了一些东西,又遇到了另一个问题。你向哪一个寻求帮助?为什么要在一个问题中混合两个问题?
标签: c# asp.net-core-mvc .net-core