visual-studio 如何将Web应用程序项目转换为类库项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/546979/
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
How to convert Web application project to Class library project
提问by Marco Bettiolo
I need to convert a project started as a Web Application to a Class Libray, is this possible?
我需要将作为 Web 应用程序启动的项目转换为类库,这可能吗?
Thanks
谢谢
回答by JarrettV
The correct answer is yes. Just edit the csproj (msbuild) file and change the ProjectGuid and remove the ProjectTypeGuids:
正确答案是肯定的。只需编辑 csproj (msbuild) 文件并更改 ProjectGuid 并删除 ProjectTypeGuids:
<ProjectGuid>{9845066A-3C9E-4F51-8F5F-8F513E8D03C1}</ProjectGuid>
It really is that simple.
真的就是这么简单。
回答by Sam
If you want to make it exactly the same as a class library project, here's how to do it for a Visual Studio 2010 project:
如果你想让它与类库项目完全一样,下面是一个 Visual Studio 2010 项目的方法:
- Edit the csproj file - Under PropertyGroup- Remove ProjectTypeGuids
- Remove UseIISExpress
- Add <FileAlignment>512</FileAlignment>
 
- Remove 
- Change <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />to<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- Remove <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
- Remove <ProjectExtensions>
- Under <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">- Change OutputPathtobin\Debug\
 
- Change 
- Under <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">- Change OutputPathtobin\Release\
 
- Change 
 
- Under 
- Open the project in Visual Studio - Remove any of the following references if they are unused
- System.Configuration
- System.Drawing
- System.EnterpriseServices
- System.Web
- System.Web.ApplicationServices
- System.Web.DynamicData
- System.Web.Entity
- System.Web.Extensions
- System.Web.Services
 
- Delete any of the following files/folders if they are unwanted/unused
- App_Data
- *.aspx
- Web.config
- Scripts
- Styles
- Global.asax
- Site.Master
 
 
- Remove any of the following references if they are unused
- 编辑 csproj 文件 - 在下面 PropertyGroup- 消除 ProjectTypeGuids
- 消除 UseIISExpress
- 添加 <FileAlignment>512</FileAlignment>
 
- 消除 
- 更改<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />为<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- 消除 <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
- 消除 <ProjectExtensions>
- 在下面 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">- 更改OutputPath为bin\Debug\
 
- 更改
- 在下面 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">- 更改OutputPath为bin\Release\
 
- 更改
 
- 在下面 
- 在 Visual Studio 中打开项目 - 如果未使用,请删除以下任何引用
- 系统配置
- 系统图
- 系统.企业服务
- 系统.Web
- 系统.Web.应用服务
- 系统.Web.动态数据
- 系统.Web.实体
- 系统.Web.扩展
- 系统.网络.服务
 
- 如果不需要/未使用,请删除以下任何文件/文件夹
- 应用程序数据
- *.aspx
- 网页配置
- 脚本
- 样式
- 全球.asax
- 站点管理员
 
 
- 如果未使用,请删除以下任何引用
回答by David Basarab
No. Your best bet is to create a Class.Library and copy the .cs files into your new project.
不。最好的办法是创建一个 Class.Library 并将 .cs 文件复制到您的新项目中。
A Class Library won't do anything with .aspx pages, it will see those as files in the solution.
类库不会对 .aspx 页面执行任何操作,它会将这些页面视为解决方案中的文件。
回答by Valamas
I came here to have a class library with mvc menu items. This is, right click on views folder to add view or controller to add controller within a classlibrary.
我来这里是为了有一个带有 mvc 菜单项的类库。也就是说,右键单击视图文件夹以添加视图或控制器以在类库中添加控制器。
I was able to achieve this by editing my mvc.web.app.csproj, copy the <ProjectTypeGuids />to my class.lib.csproj project.
我能够通过编辑我的 mvc.web.app.csproj 来实现这一点,将其复制<ProjectTypeGuids />到我的 class.lib.csproj 项目中。
For more context on what I am doing, see: How to reuse Areas, Controllers, Views, Models, Routes in multiple apps or websites.
有关我在做什么的更多上下文,请参阅:如何在多个应用程序或网站中重用区域、控制器、视图、模型、路由。

