Tải bản đầy đủ - 0 (trang)
Lesson 1: Creating an ASP.NET Web Application Project

Lesson 1: Creating an ASP.NET Web Application Project

Tải bản đầy đủ - 0trang

VisualStudio.NETcreatesaprojectfile;anew,blankWebform;andother

supportingfilesusedbyyourapplication.

TocreateaWebapplicationprojectfromVisualStudio.NET,followthesesteps:

1. OntheVisualStudio.NETStartPage,clickNewProject.Visual

Studio.NETdisplaystheNewProjectdialogbox,asshowninFigure

2-1.VisualStudio.NETputsWebapplicationsinthelocalhostvirtual

directory.



Figure2-1.TheNewProjectdialogbox

2. IntheNewProjectdialogbox,selecttheASP.NETWebApplication

template,typethenameofthenewprojectintheLocationtextbox,

andclickOK.

WhenyoucreateaWebapplicationprojectinVisualStudio.NET,the

programmingenvironmentcreatesanewfolderandgeneratesasetoffilesfor

theproject.VisualStudio.NETgivesthefolderthesamenameastheproject

andputsthefolderintherootfolderofthedefaultWebsitethatIIShostson

yourcomputer.ThislocationisshownintheLocationboxoftheNewProject

dialogboxashttp://localhost/projectname.



OrganizingYourProjectswithIIS



It’simportanttorealizethatWebapplicationscanexistonlyinalocationthat

hasbeenpublishedbyIISasavirtualfolder.Avirtualfolderisashared

resourceidentifiedbyanaliasthatrepresentsaphysicallocationonaserver.If

youtrytoselectaphysicalfolderfromtheNewProjectdialogbox,suchas

C:\MyFiles,VisualStudio.NETdisablestheOKbutton.It’snotOK—youcan’t

createaWebapplicationthere!

Thevirtualfoldernamed//localhostistheWebrootfolderonyourcomputer.IIS

determinesthephysicallocationofyourWebrootfolder.Bydefault,IISinstalls

thefolderonyourbootdriveat\Inetpub\wwwroot.Insteadofusingthe

//localhostdefaultandlettingitgetclutteredwithsampleprojects,production

code,andotherstuff,organizeyourprojectsbycreatingseparatefoldersfor

samples,tests,andproductioncode.ThensharethosefolderswiththeWeb

beforeyoucreatenewprojects.RememberthatVisualStudio.NETcreatesa

newfolderforeachnewproject,socreateonlythefoldersyouneedtoorganize

thetypesofprojectsyoucreate.



CreatingVirtualFolderstoOrganizeWebApplications

UseIIStocreatenewvirtualfoldersandtomanageWebsiteshostedonyour

computer.CreatingavirtualfolderforusewithVisualStudio.NETrequirestwo

majortasks:

Creatingthevirtualfolder.

VirtualfoldersspecifywhereyourWebapplicationprojectsare

physicallystored,soyouusethemtohelporganizeyourprojectsduring

development.

AddingtheFrontPageServerExtensionstothevirtualfoldertocreatea

subweb.

AsubwebissimplyavirtualfolderthatcontainsaWebsite.Addingthe

FrontPageServerExtensionstoavirtualfolderenablesVisualStudio

.NETtocreateandmaintainWebapplicationsinthatfolder.



CreatingaVirtualFolder

TocreateanewvirtualfolderinIIS,followthesesteps:



1. OpentheIISmanagementconsolebyclickingStart,pointingtoAll

Programs,AdministrativeTools,andclickingInternetInformation

Services.

2. Intheconsoletree,expandthelocalcomputerandWebsites.RightclickDefaultWebSite,pointtoNew,andclickVirtualDirectoryon

theshortcutmenu,asshowninFigure2-2.TheVirtualDirectory

CreationWizardopens.



Figure2-2.Creatinganewvirtualfolder

3. IISstartstheVirtualDirectoryCreationWizardtowalkyouthrough

creatinganewvirtualfolder.ClickNextonthewizardtitlepageto

displaytheVirtualDirectoryAliaspage,asshowninFigure2-3.



Figure2-3.TheVirtualDirectoryAliaspage

4. Typeanaliasforthefolder.Thealiasisthenameyouwilluseto

identifytheresourceinthisfolder.InVisualStudio.NET,thisisthe

nameyouwillusetospecifythelocationofyourproject.ClickNext.

ThewizarddisplaystheWebSiteContentDirectorypage,asshown

inFigure2-4.



Figure2-4.TheWebSiteContentDirectorypage

5. IntheDirectorybox,typethepathspecificationofthephysicalfolder

toassociatewiththevirtualfolder.Thisisthebasefolderwhereyour

projectfolderswillbestored.ClickNext.Thewizarddisplaysthe

AccessPermissionspage,asshowninFigure2-5.



Figure2-5.TheAccessPermissionspage

6. KeepthedefaultpermissionsshowninFigure2-5.ClickNextand

thenclickFinishtocreatethevirtualfolderandcompletethewizard.



CreatingaSubweb

ToaddtheFrontPageServerExtensionstoavirtualfolder,followthesesteps:

1. Right-clicktheDefaultWebSiteiconintheIISmanagementconsole,

pointtoNew,andclickServerExtensionsWebontheshortcutmenu.

2. IISstartstheNewSubwebWizardtowalkyouthroughaddingthe

FrontPageServerExtensionstoyourvirtualfolder.ClickNextonthe

wizardtitlepagetodisplaytheSubwebNamepage,asshownin

Figure2-6.



Figure2-6.TheSubwebNamepage

3. TypethenameofthevirtualfolderintheDirectoryNamebox.This

namecorrespondstothealiasyouenteredinstep4oftheprevious

procedure.TypeadescriptionofthefolderintheTitleboxorleaveit

blank.ClickNext.ThewizarddisplaystheAccessControlpage,as

showninFigure2-7.



Figure2-7.TheAccessControlpage

4. AcceptthedefaultaccesscontrolsettingsbyclickingNext,andthen

clickFinishtocreatethesubweb.



CreatingaNewProjectintheVirtualFolder

Afteryoucreateanewvirtualfolderandaddtheserverextensionstoit,youcan

useitwithinVisualStudio.NETtocreatenewprojects.TocreateaWeb

applicationprojectinyournewvirtualfolder,chooseNewProjectfromthe

VisualStudio.NETFilemenu,andspecifythenameofthevirtualfolderinthe

NewProjectdialogbox,asshowninFigure2-8.



Figure2-8.CreateaProjectinthenewvirtualfolder

ThelocationyouspecifyintheNewProjectdialogboxtakestheform

http://servername/virtualfolder.Theservernamelocalhostindicatesthatthe

serverisrunningonyourdevelopmentmachine.Thenamevirtualfolderisthe

aliasyoucreatedforthevirtualfolderintheprecedingtasks.



AddingControlsandWritingCode

WhenVisualStudio.NETcreatesaWebapplication,itdisplaysanewWebform

inthecenterwindow,asshowninFigure2-9.DragcontrolsfromtheToolboxto

theWebformasyouwouldwithaWindowsform.



Figure2-9.AnewWebform

ToaddcodetorespondtoeventsontheWebform,double-clickthecontrol.

VisualStudio.NETdisplaystheCodewindow.Addyourcodetotheevent

procedureprovidedforthecontrol.Forexample,thefollowingcodedisplays

Hello,Web!inInternetExplorerwhenyouclicktheButton1commandbutton:



VisualBasic.NET



PrivateSubButton1_Click(ByValsenderAsSystem.Objec

ByValeAsSystem.EventArgs)HandlesButton1.Click

Response.Write("Hello,Web!
")

EndSub



VisualC#



privatevoidButton1_Click(objectsender,System.Event

{

Response.Write("Hello,Web!
");

}

VisualStudioautomaticallycreatestheeventproceduresforanobject’sdefault

eventwhenyoudouble-clickthecontrolintheDesignwindow.The

Button1_Clickeventprocedureshownaboveresponds,orhandles,theClick

eventfortheButtoncontrol.InMicrosoftVisualBasic.NET,theHandlesclause

attheendoftheproceduredeclarationmakestheconnectionbetweentheClick

eventandthiseventprocedure.

InMicrosoftVisualC#,thisconnectionishiddeninthecodegeneratedbythe

WebFormsDesigner.Toviewthiscode,clicktheplussignnexttotheWeb

FormDesignerGeneratedCoderegionintheCodewindow.Thefollowingcode

showsthegeneratedcodeconnectingtheFormLoadeventandButtonClick

eventstoeventprocedures.



VisualC#



#regionWebFormDesignergeneratedcode

overrideprotectedvoidOnInit(EventArgse)

{

//

//CODEGEN:ThiscallisrequiredbytheASP.NETW

//

InitializeComponent();

base.OnInit(e);

}

///



///RequiredmethodforDesignersupport-donotmodi

///thecontentsofthismethodwiththecodeeditor.

///


privatevoidInitializeComponent()

{

this.Button1.Click+=newSystem.EventHandler(this

this.Load+=newSystem.EventHandler(this.Page_Loa

}

#endregion

Creatingthisconnectionbetweenanobject’seventandtheeventprocedurethat

respondstotheeventiscalledwiringtheevent.VisualBasic.NET

automaticallycreatesthisconnectionthroughtheHandlesclause,butVisualC#

requiresyoutowiretheeventmanuallybyaddingcodetothe

InitializeComponentprocedureforanyofthenondefaulteventsyouwanttouse.

Toruntheapplication,pressF5.VisualStudio.NETbuildstheapplication,starts

thebrowser,andthendisplaysthepageinthebrowser.WhenyouclickButton1,

thebrowserdisplaysHello,Web!asshowninFigure2-10.Closingthebrowser

endstheWebapplication.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Lesson 1: Creating an ASP.NET Web Application Project

Tải bản đầy đủ ngay(0 tr)

×