23.09.2015 Views

ASP.NET

精采試讀

精采試讀

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

packages<br />

3-31<br />

PART<br />

<br />

01<br />

02<br />

03-0303<br />

3-31<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

03-03 <strong>ASP</strong>.<strong>NET</strong> MVC<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

Entity<br />

Framework Code First<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

2<br />

TIPS<br />

Visual Studio 2010NuGetEntity<br />

Framework5.0<br />

<br />

CHAPTER 03 59


❙ 3-3-1 <strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

Visual Studio 20122<br />

3-32<br />

Web<strong>ASP</strong>.<strong>NET</strong> MVC 4 Web MvcGuestbook<br />

3-33<strong>ASP</strong>.<strong>NET</strong> MVC 4 Web <br />

60<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

TIPS<br />

Visual Studio 2012<strong>ASP</strong>.<strong>NET</strong> MVC 2<strong>ASP</strong>.<strong>NET</strong> MVC 1.0<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 2.0Visual Studio 2010<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

3 Application Upgrader<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 3 Application plication Upgrader<strong>ASP</strong>.<strong>NET</strong> MVC<br />

2.0<strong>ASP</strong>.<strong>NET</strong> MVC 3.0<strong>ASP</strong>.<strong>NET</strong> MVC 4.0<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 3 Application plication Upgrader<br />

http://aspnet.codeplex.com/releases/view/59008<br />

plex.com/releases/v<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 3 Application plication Upgrader <br />

http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-<br />

3-project-upgrade-<br />

tool.aspx<br />

<br />

<br />

3-34<br />

<br />

01<br />

02<br />

03-0303<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

3-34<strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

CHAPTER 03 61


( <br />

F5) <br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

3-35<strong>ASP</strong>.<strong>NET</strong> MVC<br />

3<br />

(Layout Page)<strong>ASP</strong>.<strong>NET</strong>Membership<br />

<br />

<br />

3-36<strong>ASP</strong>.<strong>NET</strong> P MVC 4<br />

62<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


<strong>ASP</strong>.<strong>NET</strong> MVC 4 <br />

App_Data (*.mdf, *.ldf)<br />

aspnet--.mdf3-37<br />

PART<br />

<br />

01<br />

02<br />

03-0303<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

3-37<strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

TIPS<br />

Visual Studio 2012App_<br />

Data<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4<br />

3-38<br />

CHAPTER 03 63


CSS/JS<br />

Action Filter <br />

(Routing) <br />

<br />

CSS<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<strong>ASP</strong>.<strong>NET</strong> MVCModels<br />

EDMXDBML <br />

JavaScriptVBScript<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

HomeController<br />

<br />

LayoutPartial View<br />

<br />

3-38<br />

TIPS<br />

2<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<strong>ASP</strong>.<strong>NET</strong> Web Forms<br />

<strong>ASP</strong>.<strong>NET</strong> <strong>ASP</strong>.<strong>NET</strong> Web Forms<strong>ASP</strong>.<strong>NET</strong><br />

MVC<br />

64<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> Web<br />

/Index.aspx<br />

http://localhost/<br />

Forms<br />

/Index.aspx.cs<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

http://localhost/<br />

/Controller/HomeController.cs<br />

/Views/Home/Index.aspx<br />

<strong>ASP</strong>.<strong>NET</strong> Web http://localhost/<br />

/About.aspx<br />

()<br />

Forms<br />

About.aspx /About.aspx.cs<br />

()<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

http://localhost/<br />

/Controller/HomeController.cs ()<br />

Home/About<br />

/Views/Home/About.aspx ()<br />

3-1<strong>ASP</strong>.<strong>NET</strong> Web Forms<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<strong>ASP</strong>.<strong>NET</strong> Web Forms<br />

<br />

01<br />

02<br />

03-0303<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

(Routing)App_Start\<br />

RouteCong.csRegisterRoutes<br />

es<br />

public static void RegisterRoutes(RouteCollection s(RouteCollection routes)<br />

{<br />

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");<br />

e}.axd/{*pathInfo}<br />

routes.MapRoute(<br />

name: "Default",<br />

url: "{controller}/{action}/{id}",<br />

on}/{id}",<br />

defaults: new { controller = "Home", action = "Index", id =<br />

UrlParameter.Optional }<br />

);<br />

}<br />

(Routing)<br />

IgnoreRoute<br />

*.axd <strong>ASP</strong>.<strong>NET</strong> MVC<br />

<strong>ASP</strong>.<strong>NET</strong>Trace.axdHttpHandler<strong>ASP</strong>.<br />

<strong>NET</strong> MVC<strong>ASP</strong>.<strong>NET</strong> MVC<strong>ASP</strong>.<strong>NET</strong> Web Form<br />

<br />

CHAPTER 03 65


MapRoute<br />

MapRoute<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

1. name<br />

2. url<br />

3. defaults {controller}{action}{id} 3<br />

MapRoute http://localhost/Home/About<br />

Routing Home/About<br />

{controller} <br />

Home {action} <br />

About<strong>ASP</strong>.<strong>NET</strong> MVC<br />

ControllersHome (HomeController.cs)<br />

About (Public Method)MVC<br />

(Action)<br />

<br />

http://localhost/Routing<br />

MapRoute3 <br />

(defaults) <br />

ControllersHome<br />

Index<strong>ASP</strong>.<strong>NET</strong> MVC<br />

TIPS<br />

Routing { } <br />

<br />

<br />

{controller} {action} <strong>ASP</strong>.<strong>NET</strong>T MVCController<br />

Actiondefaults<br />

<br />

HomeController<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Web;<br />

using System.Web.Mvc;<br />

66<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

namespace MvcGuestbook.Controllers<br />

{<br />

public class HomeController : Controller<br />

{<br />

public ActionResult Index()<br />

{<br />

ViewBag.Message = " <strong>ASP</strong>.<strong>NET</strong> MVC ";<br />

return View();<br />

}<br />

public ActionResult About()<br />

{<br />

ViewBag.Message = "";<br />

return View();<br />

}<br />

01<br />

02<br />

03-0303<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

public ActionResult Contact()<br />

{<br />

ViewBag.Message = "";<br />

";<br />

}<br />

}<br />

}<br />

return View();<br />

(Controller) <br />

ControllerGuestbookControllerGuestbook<br />

erGuestbook<br />

<br />

Controller (IController)<br />

ActionResult<strong>ASP</strong>.<strong>NET</strong><br />

N<br />

MVC (Action)<br />

IndexViewBagdynamic<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

View<br />

ViewBag.Message = " <strong>ASP</strong>.<strong>NET</strong> MVC ";<br />

CHAPTER 03 67


TIPS<br />

(dynamic)<br />

C# 4.0<strong>ASP</strong>.<strong>NET</strong> MVC 4.0 .<strong>NET</strong><br />

Framework 4.0<br />

return View(); Controller (Helper<br />

Method)ViewResultViewResultActionResult<br />

<strong>ASP</strong>.<strong>NET</strong> MVC (View)<br />

<strong>ASP</strong>.<strong>NET</strong> MVCHomeIndex<br />

View()<strong>ASP</strong>.<strong>NET</strong> MVCViews\Home\<br />

Index.aspx<br />

Visual Studio<br />

<br />

3-39<br />

@<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 3Razor ()<strong>ASP</strong>.<strong>NET</strong><br />

MVC 2.0<strong>ASP</strong>.<strong>NET</strong> Web Form<strong>ASP</strong>XRazor<br />

View<strong>ASP</strong>.<strong>NET</strong> MVC 4<strong>ASP</strong>.<strong>NET</strong><br />

MVCWebForm ViewRazorView<br />

View<br />

68<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

01<br />

02<br />

03-0303<br />

3-40 (Action) (View)<br />

Index.cshtmlHTML<br />

HTM<br />

<strong>ASP</strong>.<strong>NET</strong> Web Form<strong>ASP</strong>.<strong>NET</strong> MVC 2.0MasterPage<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4.0<br />

Views<br />

_ViewStart.cshtml<br />

View<br />

View<br />

(Layout Page)<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

_ViewStart.cshtml<br />

3Layout<br />

~/Views/Shared/_Layout.<br />

cshtmlViews<br />

s<br />

(View) <br />

<br />

3-41Views\<br />

_ViewStart.cshtml<br />

3-42Views\_ViewStart.cshtml<br />

CHAPTER 03 69


~/Views/Shared/_Layout.cshtml<br />

HTML3-43<br />

3-43Views\Shared\_Layout.cshtml<br />

ControllerViewBag.MessageIndex.<br />

rViewBag essage cshtml (View) <br />

@ViewBag.Message<br />

Index.cshtmlViewBag.Title<br />

View_Layout.cshtml<br />

3-44Views\Home\Index.cshtml<br />

70<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

01<br />

02<br />

03-0303<br />

3-45Views\Shared\_Layout.cshtml<br />

❙ 3-3-2 <br />

Step01Models<br />

<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

3-46<br />

CHAPTER 03 71


Step02 Guestbook.cs<br />

3-47<br />

Step03 <br />

namespace MvcGuestbook.Modelsok.Models<br />

{<br />

public class Guestbook<br />

{<br />

public int Id { get; set; }<br />

public string { get; set; }<br />

public string Email { get; set; }<br />

public string { get; set; }<br />

}<br />

}<br />

Step04 <br />

3-48<br />

72<br />

<strong>ASP</strong>.<strong>NET</strong> MVC 4


PART<br />

<br />

TIPS<br />

<strong>ASP</strong>.<strong>NET</strong> MVC (Model) <br />

ADO.<strong>NET</strong>, , LINQ to SQL, NHibernateEntity FrameworkModels<br />

<br />

Models<br />

Models<br />

❙ 3-3-3 <br />

Step01Controllers<br />

<br />

01<br />

02<br />

03-0303<br />

<br />

<br />

<br />

<br />

<br />

<strong>ASP</strong>.<strong>NET</strong> MVC<br />

<br />

<br />

04<br />

05<br />

06<br />

3-49Controllers<br />

CHAPTER 03 73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!