GOTCHA 18 Creating Web apps can be painful


GOTCHA #18 Creating Web apps can be painful

It is better to create a blank solution and create projects in it rather than starting out creating a project. The advantages of this are:

  • You can add other projects to the solution as desired (and in a typical application you will want to).

  • In a large application you can create different solutions with a subset of projects for different purposes. A project may belong to more than one solution at a time.

Say you have created a blank solution named MyApp in the C:\projects directory. Also assume you have created projects like a class library named MyLib from within this solution. By default, the MyLib project is placed in the C:\projects\MyApp\MyLib directory. Now say you want to create a Web project within the same solution, either an ASP.NET Web Application or an ASP.NET Web Service. If you specify the location of the service as http://localhost/MyWebApp, then the project for MyWebApp is unfortunately created in the C:\inetpub\wwwroot directory (or wherever IIS is installed). This is undesirable, as you would like to keep all files related to your solution together, or at least in your own preferred location. The problem is worse if you move an existing solution to a new machine by bringing it over from a source code control system.

In this gotcha I discuss two things:

  • how to create a solution with a Web project

  • how to open a solution that contains a Web project and is stored in Visual Source Safe.

Before creating a Web project, create a virtual directory. The easiest way to do that is in Windows Explorer.

In the following example, I assume that you have already created a blank solution named CreatingWebApp. In that solution you want to create an ASP.NET Web Application. If you click on Add New Project in Solution Explorer, a project will be created under the CreatingWebApp directory.

First you use Windows Explorer to create MyWebApp as a subdirectory of CreatingWebApp. Then you right click on it (in Windows Explorer) and select Properties. In the Properties window, you go to the Web Sharing tab as shown in Figure 2-15.

Figure 2-15. Creating a Virtual Directory before creating a Web Application


In this tab select the "Share this folder" radio button. In the dialog that pops up, accept the defaults and click OK. This process tells IIS that your new directory is the location of the virtual directory MyWebApp. Now in Visual Studio you can create the Web application simply by providing the location as http://localhost/MyWebApp, as shown in Figure 2-16.

Figure 2-16. Creating a Web Application


The project-related files will now be created under the CreatingWebApp\MyWebApp directory.

You should take a similar approach before opening any solution containing a Web App project for the first time. First make the directory a virtual directory, then open the solution in Visual Studio.

Things get a bit more complicated when you open projects that are stored in Visual Source Safe (VSS). Suppose you have a solution in VSS containing one or more Web projects. You want to open it on a new machine belonging to a new developer on your team. I have wasted significant time on this whenever I forgot to follow the correct sequence of steps. When you open a solution in Visual Studio, if that solution is in VSS, Visual Studio tries to get the latest version. For non-Web applications, this is not much of a problem. However, if the project is a Web App, you'll be presented with the Set Project Location - CreatingWebApp dialog shown in Figure 2-17.

Figure 2-17. Error opening a Web App


Let's examine the problem scenario. First, you placed the solution created above in VSS. Then you removed the directory from your local hard drive and got the latest version of the solution from VSS. You made sure that MyWebApp is a virtual directory referring to the physical location of the MyWebApp directory. Then you double-clicked on the CreatingWebApp.sln solution file.

That is when you got the error message shown in Figure 2-17. What's the problem?

When you open the Web App, Visual Studio looks in the virtual directory and finds the project-related files. Overwriting the files in a Web application may not be the best thing to do. So, as a precaution, Visual Studio asks you to enter a different working directory. How do you avoid this problem? Here are the steps to follow:

  1. Fetch the source code and related files from VSS.

  2. Create the virtual directory for your Web project.

  3. Leave the Web project directory in place, but remove all its contents.

  4. Double-click on the .sln file and open the solution with Web projects in it.

This will automatically pull your Web project files from VSS.

The above steps are only needed the first time you bring the files over, or if you change the location where you keep your source code locally.

IN A NUTSHELL

It is better to start by creating a blank solution and then creating projects in it. Also, set up a virtual directory before creating a Web App on localhost. Further, if the solution has Web Apps and you have it checked into Visual Source Safe, opening the solution the first time on a new machine requires following a specific set of steps:

  1. Fetch the files.

  2. Create a virtual directory.

  3. Delete the contents of the virtual directory.

  4. Open the solution.

SEE ALSO

Gotcha #19, "Naming XML documentation for IntelliSense support isn't intuitive."



    .NET Gotachas
    .NET Gotachas
    ISBN: N/A
    EAN: N/A
    Year: 2005
    Pages: 126

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net