为什么要创建 ASP.NET 5类库项目?

我试图弄清楚“ ASP.NET 5类库”(vNext) C # 项目与 ASP.NET 有什么关系。为什么要用这个模板创建一个项目,而不仅仅是一个普通的 C # “类库”项目呢?

我喜欢这些新特性,比如 project.json 文件,而不是。Csproj 文件等,但是当项目与 ASP.NET 或 IIS 等无关时,创建一个“ ASP.NET”类库似乎是不对的。这只是业务逻辑层的一个项目。一个新的 WebApi ASP.NET 网站最终将会引用这个项目,但是在这一点上这是不相关的。

只是名字不好吗?它应该被称为“ vNext Class Library”而不是使用一个看起来像 web 应用程序的图标吗?

29691 次浏览

From what I understand one benefit is that the end product of ASP.NET 5 Class Library project is a NuGet package (rather than just the .dll assembly).

You can generate the NuGet package by right clicking the project in Visual Studio 2015 and choosing the "Publish..." option. Or you can use "KPM pack" at the command line.

Also, you have the ability to use the ASP.NET 5 Core runtime so that your library can run cross-platform .

This is an interesting observation, the current template will generate a class library compatible with ASP.NET 5 runtime. You don't get that from the normal C# class library.

I filed the following issue for tracking this design question - https://github.com/aspnet/Home/issues/281

Why create an ASP.NET 5 Class Library project?

There are a number of benefits of ASP.NET 5 Class Library projects (.kproj) over Class Library projects (.csproj):

  1. ASP.NET 5 class libraries easily support cross-compiling projects to multiple targets, such as aspnet50, aspnetcore50, net45, and various other portable class library variations. This includes rich Visual Studio support for Intellisense to notify you which APIs are available for which targets.
  2. NuGet packages are automatically created, which is an extremely common thing to do with class libraries.
  3. Better productivity when it comes to things like automatically refreshing Solution Explorer when the file system changes. Fewer conflicts in source control when trying to merge conflicting changes in the *.csproj file.
  4. Can be compiled cross-platform (in part because it doesn't depend on MSBuild)

You can reference a *.csproj project from a *.kproj project (this was just made a lot easier with the new preview of Visual Studio 2015), but it was always possible with some manual steps.

Why does the name have "ASP.NET" in it?

As far as the names goes, it's a relic of history that will soon be addressed. The new project type is useful far beyond ASP.NET 5 applications. Expect to see new names in a future preview of Visual Studio:

  • .NET Console Application (Cross-platform)
  • .NET Class Library (Cross-platform)

Update 5/13/2015

With the release of Visual Studio 2015 RC you can see the updated project template names:

  • Class Library (Package)
  • Console Application (Package)

These use the project.json file and the .NET Execution Environment (DNX) to build, run, and package (into a NuGet package) the project.

These project templates continue to show up in the New Project dialog under the "Web" node, but now also show up in the main "Visual C#" node as well.

VS2015 RC Class Lib / Console App templates