Saturday, 19 June 2010

How to add a web project item to a class library

This is an old trick but I've found myself hunting for it repeatedly as of late…

If your current visual studio project doesn't provide the option to add a web user control, web page, etc (perhaps because the project is a class library or a WSPBuilder project), you can easily tweak the project file enable support for these templates.

To do so:

  • Unload the project (right-click the project in Solution Explorer and select Unload Project from the context menu)
  • Edit the .csproj file (right-click the project in Solution Explorer and select Edit MyProj.csproj from the context menu)
  • Locate the ProjectTypeGuids element and add a magic project type GUID to beginning of the list: {349c5851-65df-11da-9384-00065b846f21};
  • Save your changes and reload the project (right-click and Reload Project)

When you're done editing the .csproj file it should look like this:

<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Now you can add user controls and whatnot to your heart's content!

Update Feb 2011: When I last had to this in VS2010, things were a bit different. The class library contained a ProjectGuid element but no ProjectTypeGuids. According to the post I link to above, a ProjectTypeGuids element can be added below the ProjectGuid so I pasted in the above. Although VS "helped out" by rejigging the XML for me and modifying the GUID when I subsequently inspected it, the project allows me to add web items.

Each class library seems to get a different (unique) ProjectGuid value so I'm not sure how adding the child ProjectTypeGuids element (which was subsequently removed by VS) actually worked… but it did ;-)

0 comments:

Post a Comment