1. 程式人生 > >【ASP.NET】website轉webapplication

【ASP.NET】website轉webapplication

*以下操作都以VS2013為參考;

#新建兩種web專案

1、新增webapplication專案;

2、新增website專案;

 

#比較兩種web專案新建的webform頁面的不同點:

1、檔案目錄結構:

  從圖中可以看出webapplication專案中的webform頁面多了*.aspx.designer.cs檔案;

  *.aspx.designer.cs檔案是用來初始化頁面的服務端控制元件的;

1 <body>
2     <form id="form1" runat="server">
3     <div>
4 <input id="testinput" runat="server" /> 5 </div> 6 </form> 7 </body>
 1 namespace WebApplication1 {
 2     
 3     
 4     public partial class App_Default {
 5         
 6         /// <summary>
 7         /// form1 控制元件。
 8         /// </summary>
9 /// <remarks> 10 /// 自動生成的欄位。 11 /// 若要進行修改,請將欄位宣告從設計器檔案移到程式碼隱藏檔案。 12 /// </remarks> 13 protected global::System.Web.UI.HtmlControls.HtmlForm form1; 14 15 /// <summary> 16 /// testinput 控制元件。 17 /// </summary> 18 ///
<remarks> 19 /// 自動生成的欄位。 20 /// 若要進行修改,請將欄位宣告從設計器檔案移到程式碼隱藏檔案。 21 /// </remarks> 22 protected global::System.Web.UI.HtmlControls.HtmlInputText testinput; 23 } 24 }

2、aspx頁面不同點

  website的頁面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Site_Default.aspx.cs" Inherits="Site_Default" %>

  webapplication的頁面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="App_Default.aspx.cs" Inherits="WebApplication1.App_Default" %>

  不同點:site的為codefile,application為codebehind;

3、aspx.cs檔案的不同點

  website的頁面:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class Site_Default : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12 
13     }
14 }

  webapplication的頁面:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace WebApplication1
 9 {
10     public partial class App_Default : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14             this.testinput.Visible = true;
15         }
16     }
17 }

  不同點:webapp的頁面都有名稱空間,而website的頁面沒有;

#將website專案轉換成webapp專案

方法一:

  將website專案的webform頁面拷貝到webapp專案中,

  1)新增.aspx.designer.cs檔案;

  2)在.cs檔案中加入名稱空間;

  3)修改aspx頁面中的codefile為codebehind;

方法二:

  選中webapp專案,選擇選單欄中的“專案”,選擇“轉換為web應用程式”;