asp.net-core-mvc - 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?}");
});
}
}
}
解决方案
我猜这行代码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 本身添加兼容性。
推荐阅读
- php - 从时区用户而不是时区数据库中获取查询结果
- angular - Angular 2:我正在创建便笺板。因此,单击创建便笺按钮时,我想每次都添加便笺
- reactjs - 组织查询 React Native 的更好方法
- hyperledger-fabric - 在 Hyperledger Fabric 区块链网络中使用图像
- javascript - 反应子回调绑定 this 而不覆盖原始 this
- java - AWS Api Gateway - Java SDK 生成“生成 SDK 时发生内部错误”
- pycharm - 无法添加自定义后缀完成模板
- python - tf.estimator.LinearClassifier 输出权重解释
- c - 不理解 printf() 的奇怪行为
- imagick - Imagick php 无法正确打印印地语 utf8 字符