Web 应用程序项目与网站项目比较分析

      在 Visual Studio 中,我们可以创建“Web 应用程序项目”或“网站项目”。每种项目类型各有优缺点,所以我们要选择可以满足需要的最佳项目类型,应了解各项目类型之间的差异。今天将深入的学习这两种项目之间的异同。
 项目文件结构
     Web 应用程序项目使用 Visual Studio 项目文件(.csproj 或 .vbproj)来跟踪有关项目的信息。除其他任务以外,这还使得指定项目中要包含或排除哪些文件,以及因此在生成期间编译哪些文件成为可能。

     web应用程序项目文件结构

     

      web应用程序物理文件结构

      

      web应用程序项目文件

     

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  
<!--应用程序相关信息配置-->
  
<PropertyGroup>
    
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    
<ProductVersion>
    
</ProductVersion>
    
<SchemaVersion>2.0</SchemaVersion>
    
<ProjectGuid>{0ED50071-6E4F-4951-ABC4-603577589CAB}</ProjectGuid>
    
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    
<OutputType>Library</OutputType>
    
<AppDesignerFolder>Properties</AppDesignerFolder>
    
<RootNamespace>MyWebApp</RootNamespace>
    
<AssemblyName>MyWebApp</AssemblyName>
    
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  
</PropertyGroup>
  
<!--应用程序构建配置-->
  
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    
<DebugSymbols>true</DebugSymbols>
    
<DebugType>full</DebugType>
    
<Optimize>false</Optimize>
    
<OutputPath>bin\</OutputPath>
    
<DefineConstants>DEBUG;TRACE</DefineConstants>
    
<ErrorReport>prompt</ErrorReport>
    
<WarningLevel>4</WarningLevel>
  
</PropertyGroup>
  
<!--应用程序构建配置-->
  
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    
<DebugType>pdbonly</DebugType>
    
<Optimize>true</Optimize>
    
<OutputPath>bin\</OutputPath>
    
<DefineConstants>TRACE</DefineConstants>
    
<ErrorReport>prompt</ErrorReport>
    
<WarningLevel>4</WarningLevel>
  
</PropertyGroup>
  
<!--应用程序引用的程序集-->
  
<ItemGroup>
    
<Reference Include="Microsoft.CSharp" />
    
<Reference Include="System" />
    
<Reference Include="System.Data" />
    
<Reference Include="System.Core" />
    
<Reference Include="System.Data.DataSetExtensions" />
    
<Reference Include="System.Web.Extensions" />
    
<Reference Include="System.Xml.Linq" />
    
<Reference Include="System.Drawing" />
    
<Reference Include="System.Web" />
    
<Reference Include="System.Xml" />
    
<Reference Include="System.Configuration" />
    
<Reference Include="System.Web.Services" />
    
<Reference Include="System.EnterpriseServices" />
    
<Reference Include="System.Web.DynamicData" />
    
<Reference Include="System.Web.Entity" />
    
<Reference Include="System.Web.ApplicationServices" />
  
</ItemGroup>
  
<!--应用程序所包含的文件-->
  
<ItemGroup>
    
<Content Include="About.aspx" />
    
<Content Include="Account\ChangePassword.aspx" />
    
<Content Include="Account\ChangePasswordSuccess.aspx" />
    
<Content Include="Account\Login.aspx" />
    
<Content Include="Account\Register.aspx" />
    
<Content Include="Styles\Site.css" />
    
<Content Include="Default.aspx" />
    
<Content Include="Global.asax" />
    
<Content Include="Scripts\jquery-1.4.1-vsdoc.js" />
    
<Content Include="Scripts\jquery-1.4.1.js" />
    
<Content Include="Scripts\jquery-1.4.1.min.js" />
    
<Content Include="Web.config" />
    
<Content Include="Web.Debug.config">
      
<DependentUpon>Web.config</DependentUpon>
    
</Content>
    
<Content Include="Web.Release.config">
      
<DependentUpon>Web.config</DependentUpon>
    
</Content>
  
</ItemGroup>
  
<!--前台页面和后台代码文件依赖关系-->
  
<ItemGroup>
    
<Compile Include="About.aspx.cs">
      
<DependentUpon>About.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="About.aspx.designer.cs">
      
<DependentUpon>About.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Account\ChangePassword.aspx.cs">
      
<DependentUpon>ChangePassword.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Account\ChangePassword.aspx.designer.cs">
      
<DependentUpon>ChangePassword.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Account\ChangePasswordSuccess.aspx.cs">
      
<DependentUpon>ChangePasswordSuccess.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Account\ChangePasswordSuccess.aspx.designer.cs">
      
<DependentUpon>ChangePasswordSuccess.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Account\Login.aspx.cs">
      
<DependentUpon>Login.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Account\Login.aspx.designer.cs">
      
<DependentUpon>Login.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Account\Register.aspx.cs">
      
<DependentUpon>Register.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Account\Register.aspx.designer.cs">
      
<DependentUpon>Register.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Default.aspx.cs">
      
<DependentUpon>Default.aspx</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Default.aspx.designer.cs">
      
<DependentUpon>Default.aspx</DependentUpon>
    
</Compile>
    
<Compile Include="Global.asax.cs">
      
<DependentUpon>Global.asax</DependentUpon>
    
</Compile>
    
<Compile Include="Properties\AssemblyInfo.cs" />
    
<Compile Include="Site.Master.cs">
      
<DependentUpon>Site.Master</DependentUpon>
      
<SubType>ASPXCodeBehind</SubType>
    
</Compile>
    
<Compile Include="Site.Master.designer.cs">
      
<DependentUpon>Site.Master</DependentUpon>
    
</Compile>
  
</ItemGroup>
  
<ItemGroup>
    
<Folder Include="App_Data\" />
  
</ItemGroup>
  
<ItemGroup>
    
<Content Include="Account\Web.config" />
    
<Content Include="Site.Master" />
  
</ItemGroup>
  
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
  
<!--应用程序扩展配置-->
  
<ProjectExtensions>
    
<VisualStudio>
      
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
        
<WebProjectProperties>
          
<UseIIS>False</UseIIS>
          
<AutoAssignPort>True</AutoAssignPort>
          
<DevelopmentServerPort>50898</DevelopmentServerPort>
          
<DevelopmentServerVPath>/</DevelopmentServerVPath>
          
<IISUrl>
          
</IISUrl>
          
<NTLMAuthentication>False</NTLMAuthentication>
          
<UseCustomServer>False</UseCustomServer>
          
<CustomServerUrl>
          
</CustomServerUrl>
          
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
        
</WebProjectProperties>
      
</FlavorProperties>
    
</VisualStudio>
  
</ProjectExtensions>
  
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  
-->
</Project>


      Web应用程序项目使您易于暂时从站点移除文件,但仍确保不会失去对它们的跟踪,因为这些文件保留在文件夹结构中。例如,如果页面没有为部署准备就绪,您可以暂时从生成中排除它,而无需从文件夹结构中将其删除。您可以部署编译的程序集,然后再次将文件包括在项目中。这在使用源代码管理储存库时尤为重要。
      排除about.aspx文件后的web应用程序项目结构图

     
     排除文件about.aspx后的web应用程序物理文件图

    
     排除文件about.aspx后的web应用程序项目文件代码

    

       对于网站项目,文件夹结构中的所有文件会被自动视为包含在网站中。如果您希望从编译中排除某些文件,必须从网站项目文件夹中移除文件或将其文件扩展名更改为不由 IIS 编译和提供的扩展名。
       网站项目文件结构图

      

       网站项目物理文件结构图

      
      网站项目使您不必专门在 Visual Studio 中管理项目的结构。例如,您可以通过使用 Windows 资源管理器将文件复制到项目中或从项目中删除文件。

      排除about.aspx文件后的网站项目目录

           

      排除about.aspx文件后的网站项目物理文件目录

     

 编译
      对于 Web 应用程序项目,通常在 Visual Studio 中生成项目,或通过使用不是生产 IIS 服务器的计算机上的 ASP.NET 批处理编译器生成项目。项目中的所有代码隐藏类文件和独立类文件都编译为一个程序集,然后将其放在 Web 应用程序项目的 Bin 文件夹中。(.aspx 和 .ascx 文件以与网站项目类似的方式进行动态编译。)

      web应用程序编译后的dll内容

      

      web应用程序项目编译后产生的文件

      
     web应用程序项目指定程序集特性(如名称和版本)非常简单。

    
     事先编译可确保在生产服务器上编译站点期间用户无须等待。
     您可以完全控制项目文件夹结构中放置代码文件的位置,以及项目中的类互相引用的方式。(动态编译要求在整个站点中使用的所有类的源代码必须位于 App_Code 文件夹中。您不能从 App_Code 中的类引用页面或用户控件类。)

      新增加的单个类MySingleClass

     

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyWebApp 
{
    
public class SingleClass
    {
        
public static string MySingleClassGreet()
        {
            
return "hello ,this is MySingleClass";
        }
    }
}

 

      web项目增加MySingleClass.cs

      

       web项目页面代码文件调用MySingleClass类

      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebApp
{
    
public partial class _Default : System.Web.UI.Page
    {
        
public static  string greet = "hello ,this is my webapplication";
        
protected void Page_Load(object sender, EventArgs e)
        {
           Response.Write( MySingleClass.MySingleClassGreet());
        }


        
public static string Greet()
        {
            
return greet;
        }
    }
}

      网站项目增加MySingleClass.cs

     

      网站项目调用MySingleClass类

     

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(MyWebApp.SingleClass.MySingleClassGreet());
        
        
////Response.Write(MyWebApp._Default.Greet());
    }
}

 

      
      对于网站项目,您不必手动编译项目。网站项目通常由 ASP.NET(在开发计算机和生产 IIS 服务器上)进行动态编译。您可以在批处理编译模式(通常为每个文件夹生成一个程序集)和固定编译模式(通常为每个页面或用户控件生成一个程序集)之间选择。

      
       

       网站项目使您可以测试特定的页面,而不管其他页面的状态。这是因为运行单个页面不需要成功编译整个站点,仅需编译该页面和该页面所依赖的任何组件,如 App_Code 文件夹或 Global.asax 文件中的代码。(在 Web 应用程序项目中,如果站点中任何位置存在编译错误,则不能创建程序集,因此不能进行测试,甚至连测试所编译站点的部分内容也不行。)
       在生产中更新网站非常容易。可以更新生产服务器上的各个源代码文件,而无需以显式方式重新编译站点。即使由于编译错误其他文件未准备就绪,也可以更新各个为部署准备就绪的文件。还可以直接在 Visual Studio 中打开生产 IIS 服务器上的网站,并实时更新该网站。
       在某些情况下,预编译为多个程序集具有性能优势。一个典型示例是具有许多页面,并为这些页面编写了大量代码的站点。其中大多数页面极少被请求,只有某些页面经常受到使用。如果将这样的站点编译成多个程序集,生产服务器就可以仅加载当前请求所需的程序集。如果未请求某个页面,则不会加载其对应的程序集。
部署
      若要部署 Web 应用程序项目,需将通过编译该项目创建的程序集复制到 IIS 服务器。相反,若要部署网站项目,通常是将项目源文件复制到 IIS 服务器。
      Web 应用程序项目部署策略的优点包括:
       可以避免将源代码部署到 IIS 服务器。在某些情况下,例如在共享承载环境中,您可能会关注对 IIS 服务器上源代码进行的未经授权的访问。(对于网站项目,可能通过在开发计算机上进行预编译并部署所生成的程序集而不是源代码来避免此风险。不过,在这种情况下,会失去轻松更新站点的某些好处。)
      除将程序集或代码复制到服务器之外,部署通常还涉及其他任务。例如,数据库脚本可能必须在生产中运行,Web.config 文件中的连接字符串可能需要针对生产服务器进行更改。Visual Studio 提供了一些处理 Web 应用程序项目以自动执行其中许多任务的工具,如一键式发布。这些工具对于网站项目不可用。
      网站项目部署策略的优点包括:
       如果对 Web 应用程序进行少量更改,则不必重新部署整个应用程序。而是可以只将更改过的文件复制到生产 IIS 服务器。另外,还可在生产服务器上直接编辑文件。(因为 Web 应用程序项目的代码文件将被编译成单个程序集文件,所以即使进行少量更改,也必须部署整个站点,除非更改只是针对 .aspx 或 .ascx 文件。)

 

posted @ 2011-07-08 21:27  无风听海  阅读(2392)  评论(0编辑  收藏  举报