1

My team recently moved an ASP.NET website project from VS 2010 to 2012. When creating a new page and code behind file in 2010, the file would be created with a namespace based on the folder the structure the file was created in. For example, a file named MyPage.aspx.cs created in MyProject -> Folder1 -> Folder2 would be generated looking like

namespace MyProject.Folder1.Folder2
{
    public partial class MyPage
    {

    }
}

In 2012, the same file is created without a namespace, and the folder structure is appended to the class name with underscores like

public partial class MyProject_Folder1_Folder2_MyPage
{

}

We prefer the look of the 2010 style as it seems much cleaner to us. How can we get new files to generate with namespaces in that manner? This seems like it should be a simple setting somewhere, but I cannot find any documentation on the issue.

jtrohde
  • 352
  • 1
  • 3
  • 13
  • Create the page ouside the folder and the drop it to the folder you want. – kostas ch. Oct 15 '13 at 14:18
  • 2
    Try looking in the project properties under `Application > Default namespace` and make sure it is set to something (such as MyProject). – Trevor Elliott Oct 15 '13 at 14:19
  • I should add, this is only affecting files created under the Web project folder. Files created in other projects in the solution are still being created with the namespaces. The Web project does not have Application > Default namespace. – jtrohde Oct 15 '13 at 14:27
  • @TrevorElliott - there is no `Application` setting, because the OP is using a website project not a web application project. – Karl Anderson Oct 15 '13 at 14:29
  • I also see that creating new files under the Web project in VS 2010 is generating the files without namespaces with the folder structure in the class names, so this is not a 2012 specific issue. I'm now wondering if the files I see under Web that do have namespaces had those namespaces added manually and the class names edited to remove the folder structure, or if this is still a settings issue. – jtrohde Oct 15 '13 at 14:34
  • 2
    This sounds like you are using a web site project for VS2012 but a web application project for VS2010. I strongly recommend never using File->New Web Site, ever. – John Saunders Oct 15 '13 at 14:37
  • The project is a Web Site rather than Web Application in both 2010 and 2012. For reasons I'm not going to go into here (partly because I don't know them myself), the project HAS to be a Web Site. – jtrohde Oct 15 '13 at 14:41

1 Answers1

3

This is happening because the project is configured as a Web Site instead of a Web Application. Try converting the to a web application and see if that addresses the behavior in question.

Regarding Web Site projects, MSDN states the following:

Explicit namespaces are not added to pages, controls, and classes by default, but you can add them manually.

Check out these link for details:

Community
  • 1
  • 1
James Johnson
  • 43,670
  • 6
  • 67
  • 106
  • Marked this as the answer because it's correct. However it seems I misunderstood the situation from the beginning. The project was and always has been a Web Site project (needs to be for reasons I won't go into here). We always manually created the namespaces ourselves, so there really was no out of ordinary behavior when I posted this question. – jtrohde Nov 25 '14 at 18:47