【问题标题】:Can not find compilation library location for package 'Microsoft.AspNetCore.Antiforgery'找不到包“Microsoft.AspNetCore.Antiforgery”的编译库位置
【发布时间】: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


【解决方案1】:

返回preserveCompilationContext,也许吧?

【讨论】:

  • 糟糕。我们有二合一的问题,我有点困惑我应该回答哪一个:)
【解决方案2】:

看起来您的 project.json 不包括您项目的所有依赖项,这可能解释了为什么在您的部署目标上找不到这些库 - 您是如何部署到 IIS 的,您能否确认所有是否包含所需的 DLL?

Microsoft.AspNetCore.Mvc 取决于:
Microsoft.AspNetCore.Mvc.Razor 取决于:
Microsoft.AspNetCore.Mvc.ViewFeatures 取决于:
Microsoft.AspNetCore.Antiforgery

preserveCompilationContext 用于编译您的 MVC 视图,它还可能引用 AntiForgery 库的内容,以在您网站上的任何表单上生成所需的令牌和 cookie。

【讨论】:

  • 您是如何确定这种依赖关系的 - ViewFeatures 依赖于 Microsoft.AspNetCore.Antiforgery?
  • 我用谷歌找到了任何指向 Antiforgery nuget 包的链接,其中列出了 ViewFeatures 包,从那里很容易找到其中的链接 - 每个包都列出了它的直接依赖项,所以你可以关注他们下来。
  • 我在 project.json 中添加了 Microsoft.AspNetCore.Mvc.Razor、Microsoft.AspNetCore.Mvc.ViewFeatures、Microsoft.AspNetCore.Antiforgery 依赖项,但没有运行。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2018-12-18
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多