首页 > 解决方案 > ASP.NET Core 2.1:当项目不在 Windows 硬盘上时出现参数 null 异常

问题描述

提前抱歉我的英语不好。我是一名新手开发人员,今天刚开始学习 ASP.NET Core。简而言之,我在创建项目后遇到了问题。我没有添加任何东西,它只是不会启动。经过几个小时的尝试了解问题后,我在桌面文件夹中创建了新项目(带有文件夹),而不是我的默认项目文件夹(位于另一个硬盘上),它现在可以工作。你能帮我理解为什么会这样吗?先感谢您。 这是例外

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.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace proj1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

//THE EXCEPTION OCCURS HERE ↓
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

标签: asp.net-core-mvcasp.net-core-2.0asp.net-core-mvc-2.0

解决方案


我猜这行代码services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);是用来为低版本的代码添加向后兼容性,如果你已经迁移到更高版本的话。

此链接https://blogs.msdn.microsoft.com/webdev/2018/02/27/introducing-compatibility-version-in-mvc/澄清了它。

该异常告诉我们它在您的 bin(执行程序集的路径)文件夹中找不到相关的 dll。

最后,由于您已经在使用 2.1 版本,我觉得没有必要为 2.1 本身添加兼容性。


推荐阅读