36

I need to convert a project started as a Web Application to a Class Libray, is this possible?

Thanks

Marco Bettiolo
  • 5,071
  • 6
  • 30
  • 35

4 Answers4

42

The correct answer is yes. Just edit the csproj (msbuild) file and change the ProjectGuid and remove the ProjectTypeGuids:

<ProjectGuid>{9845066A-3C9E-4F51-8F5F-8F513E8D03C1}</ProjectGuid>

It really is that simple.

JarrettV
  • 18,845
  • 14
  • 46
  • 43
  • 1
    I have VS2010 and this Guid didn't work for me,I've tried this one: `{2CAAF0E8-A840-4ABF-BF2A-BCD4338D80B5}` – Ala' Alnajjar Apr 25 '13 at 11:09
  • 2
    What's the point of changing `ProjectGuid`? Doesn't that just uniquely identify the project? To me, it seems that a new one is generated for each project. – Sam Aug 25 '13 at 23:45
  • As an alternative **change the output type of the project to class library, you can find that in your project properties under the tab Application**, see http://stackoverflow.com/a/11536160/313113 – Alex Bitek Dec 31 '13 at 08:52
  • 1
    my project was ALREADY a class library but still showing the web icon. the project type guid of `349c5851...` means web app and `fae04ec0...` means a C# project (see this https://www.mztools.com/articles/2008/mz2008017.aspx). I completely removed the `ProjectTypeGuids` line and changed the `ProjectGuid` to any new GUID (I just changed a 6 to a 7) so when you reload it the IDE won't get confused and thinking it's still a web app – Simon_Weaver Apr 20 '17 at 08:55
  • There is no need to change the `ProjectGuid` for this, but if you do (for some reason) remember to edit your .sln file to reflect the new `ProjectGuid`. Alternatively remove the project from the solution before changing the `ProjectGuid` then add it again afterwards. – Caltor Sep 10 '20 at 11:25
  • @bitek WebAPI projects already have the output type set to class library. You need to remove the `ProjectTypeGuid`s as per this answer to fix it. – Caltor Sep 10 '20 at 11:30
39

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:

  1. Edit the csproj file

    • Under PropertyGroup
      • Remove ProjectTypeGuids
      • Remove UseIISExpress
      • Add <FileAlignment>512</FileAlignment>
    • 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 OutputPath to bin\Debug\
    • Under <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
      • Change OutputPath to bin\Release\
  2. 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
Sam
  • 40,644
  • 36
  • 176
  • 219
2

No. Your best bet is to create a Class.Library and copy the .cs files into your new project.

A Class Library won't do anything with .aspx pages, it will see those as files in the solution.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • I have a main Web Application that loads Controls from another Web Application. It should load them from a Class Library, because right now I have two web.config files and this is boring. – Marco Bettiolo Feb 13 '09 at 19:57
  • I prefer it this way, as it is the cleaner way. Though, you'll need to do some renaming in the project properties, physical folder, and .sln file, if you want the new project to use the same project/folder name as the old one. – malnosna Aug 02 '18 at 17:34
  • Incorrect. It can be changed as per this answer https://stackoverflow.com/a/3971982/470014 – Caltor Sep 10 '20 at 11:28
1

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.

I was able to achieve this by editing my mvc.web.app.csproj, copy the <ProjectTypeGuids /> to my class.lib.csproj project.

For more context on what I am doing, see: How to reuse Areas, Controllers, Views, Models, Routes in multiple apps or websites.

Community
  • 1
  • 1
Valamas
  • 24,169
  • 25
  • 107
  • 177