VS 2015. 为 ASP.NET 5 web 项目设置正确的目标框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31671851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:41:50  来源:igfitidea点击:

VS 2015. Setting right target framework for ASP.NET 5 web project

.netvisual-studio-2015asp.net-core

提问by Sergiy

I've created new ASP.NET 5 MVC project in Visual Studio 2015. On project creation wizard I've chosen to show the templates for .NET framework 4.6 and then on the next screen selected "Web Application" from ASP.NET 5 Preview Templates.

我在 Visual Studio 2015 中创建了新的 ASP.NET 5 MVC 项目。在项目创建向导中,我选择显示 .NET framework 4.6 的模板,然后在下一个屏幕上从 ASP.NET 5 Preview 中选择“Web Application”模板。

However, after creation it appears that the project listed only "dnx451" and "dnxcore50" in "frameworks" section of project.json:

但是,在创建后,该项目似乎只在 project.json 的“frameworks”部分中列出了“dnx451”和“dnxcore50”:

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

I've tried to change "dnx451" to "dnx46". The project has been built well but when I tried to run it, I got the following error message:

我试图将“dnx451”更改为“dnx46”。该项目已经构建得很好,但是当我尝试运行它时,我收到以下错误消息:

The current runtime target framework is not compatible with '...'.

Current runtime Target Framework: 'DNX,Version=v4.5.1 (dnx451)' Type: CLR Architecture: x86 Version: 1.0.0-beta5

当前运行时目标框架与“...”不兼容。

当前运行时目标框架:'DNX,Version=v4.5.1 (dnx451)' 类型:CLR 架构:x86 版本:1.0.0-beta5

How is it possible to change that "target framework" for ASP.NET 5 project?

如何更改 ASP.NET 5 项目的“目标框架”?

Here is my global.json now:

这是我的 global.json 现在:

{
  "projects": [
    "src",
    "test",
    "wrap"
  ],
  "sdk": {
    "version": "1.0.0-beta5",
    "runtime": "clr",
    "architecture": "x86"
  }
}

but it does not really matter what is listed there. I've already tried different builds of 1.0.0-beta5 and the latest 1.0.0-beta6

但那里列出的内容并不重要。我已经尝试过不同版本的 1.0.0-beta5 和最新的 1.0.0-beta6

回答by Andrew Stanton-Nurse

The problem is that DNX is aware that your project is targeting dnx46but IIS is not :). So, IIS is starting as .NET 4.5.1 (by default) and trying to load your project which isn't compatible with 4.5.1.

问题是 DNX 知道您的项目是针对的,dnx46但 IIS 不是 :)。因此,IIS 以 .NET 4.5.1(默认情况下)开始并尝试加载与 4.5.1 不兼容的项目。

In a future version of the VS Tooling, we're going to do a better job of detecting that but until then, things are a little rough for Web Applications that want to target .NET versions above4.5.1. For now, I suggest just using dnx451until then (if you aren't depending on .NET 4.5.2/4.6 functionality), but I'll provide some instructions below on how to make this work today if you need to use 4.5.2 or higher.

在 VS Tooling 的未来版本中,我们将在检测方面做得更好,但在那之前,对于希望以4.5.1以上的.NET 版本为目标的 Web 应用程序来说,事情有点困难。目前,我建议只使用dnx451到那时(如果您不依赖于 .NET 4.5.2/4.6 功能),但如果您需要使用 4.5.2,我将在下面提供一些有关如何使此工作的说明或更高。

Note: dnx452and dnx46are fullysupported in Console Applications and when using servers other than IIS (such as Kestrel or WebListener). Basically anything you can launch using the native dnx.exebootstrapper works with dnx451and dnx46but the other hosting environments (IIS, IIS Express) need additional hints to launch the correct framework.

注:dnx452dnx46完全在控制台应用程序的支持,并使用IIS以外的服务器(如红隼或WebListener)时。基本上,您可以使用本机dnx.exe引导程序启动的任何内容都可以使用dnx451dnx46但其他托管环境(IIS、IIS Express)需要额外的提示来启动正确的框架。

To make this work today, you need to set an environment variable DNX_IIS_RUNTIME_FRAMEWORKto the target framework you want to use. You can do this in the Project Properties page for the web application, on the Debug tab. Just choose the IIS Express profile and add the environment variable as below:

为了使这项工作在今天起作用,您需要为DNX_IIS_RUNTIME_FRAMEWORK要使用的目标框架设置一个环境变量。您可以在 Web 应用程序的“项目属性”页面的“调试”选项卡上执行此操作。只需选择 IIS Express 配置文件并添加如下环境变量:

Launch Profile

启动配置文件

We're going to get this added to the Known Issues, because it looks like we missed it, and as I said above, we're going to improve the default experience in VS so this isn't necessary :).

我们将把它添加到已知问题中,因为看起来我们错过了它,正如我上面所说的,我们将改进 VS 中的默认体验,所以这不是必需的 :)。

回答by arcaner

"DNX_IIS_RUNTIME_FRAMEWORK" : "DNX46"

The value is case sensitive

该值区分大小写