找不到包的编译库位置' Microsoft.AspNetCore.Antiforgery'

时间:2016-12-19 12:41:11

标签: c# asp.net-core-mvc .net-core

我正在使用ASP.NET Core 1.1.0开发Web应用程序。我的应用程序适用于IIS Express。但是当我将应用程序部署到IIS时,我收到了错误&#34; 无法找到包的编译库位置&#39; Microsoft.AspNetCore.Antiforgery&#39; &#34;。< / p>

我从project.json中删除了 preserveCompilationContext ,但我得到了#34; 缺少一个或多个编译引用。可能的原因包括缺少&#39; preserveCompilationContext&#39;属于&#39; buildOptions&#39;在应用程序的project.json。&#34;错误信息。

静态文件(如.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?}");
            });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

可能会回来preserveCompilationContext吗?