액션 필더 이해하기

  

이 장에서 목표는 액션 필더를 설명하는 것이다. 액션 필터는 여러분이 컨트롤러 액션을 응용할 수 있는 속성이다.

ASP.NET MVC 프레임웍은 여러 액션 필터를 포함한다.

l   OutputCache – 이 액션 필터는 특정한 시간을 위한 컨트롤러 액션의 결과를 저장한다.

l   HandleError – 이 액션 필터는 컨트롤러 액션이 실행될 때 발생하는 에러를 잡아낸다.

l   Authorize – 이 액션 필터는 개개인의 사용자 또는 룰을 통해 접근을 제한할 수 있다.

 

여기서는 OutputCache 액션 필터에 대해서만 알아보겠다.

액션 필터는 attribute이다. 각각의 컨트롤러 또는 전체 컨트롤러에서 모두 액션 필터를 적용할 수 있다.

 

다음 예제는  Index() 액션의 결과를 10초동안 저장하는 예제이다.

 

//[OutputCache(Duration = 10)] 사이트소스ㅡㅡ)+ 에러발생아놔~

[OutputCache(Duration = 10, VaryByParam="none")]

public string Index()

{

    return DateTime.Now.ToString("T");

}

 

주소창에 URL /Data/Index입력하고 리플래쉬를 하면 10초동안 동일한 시간을 보게 될 것이다.

 OutputCache 액션 필터를 10초동안 Index() 페이지에 적용시켰다.

 

 

다른 타입의 필터

 

ASP.NET MVC 프레임웍은 4개의 다른 타입의 필터를 제공한다.

1.      Authorization Filters – IauthorizationFilter  속성을 구현

2.      Action Filter - IActionFilter 속성을 구현

3.      Result Filter – IresultFilter 속성을 구현

4.      Exception Filters – IexceptionFilter 속성 구현

 

필터는 위에 나열된 순서대로 실행이 된다.

 

 

Base ActionFilterAttribute Class

 

이 장에서는 여러분이 쉽게 커스텀 액션 필터, ASP.NET framework에 포함된 기본 ActionFilterAttribute 클래스로 구현을 쉽게 만들 것이다. 

이 클래스는 필터 클래스로부터 상속 받는 IActionFilter IResultFilter 인터페이스 둘다 구현을 한다.

 

base ActionFilterAttribute 클래스는 다음과 같은 메서드를 override 할 수 있다.

l   OnActionExecuting – 이 메소드는 컨트롤 액션 전에 실행이 된다.

l   OnActionExecuted – 이 메소드는 컨트롤 액션 후에 실행이 된다.

l   OnResultExecuting – 이 메소드는 컨트롤 액션 결과 전에 실행된다.

l   OnResultExecuted – 이 메소드는 컨트롤 액션 결과 후에 실행된다.

 

 

Creating a Log Action Filter

 

이 장에서는 커스텀 액션 필터를 어떻게 만들지에 대해 설명한다.

우리는 Visual Studio Output 윈도우에 컨트롤 액션의 상태 단계 로그의 커스텀 액션 필터를 만들 것이다.

LogActionFilter Listing2에 포함되어 있다.

 

using System;

using System.Diagnostics;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace MvcApplication1.ActionFilters

{

    public class LogActionFilter : ActionFilterAttribute

    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)

        {

            Log("OnActionExecuting", filterContext.RouteData);

        }

 

        public override void OnActionExecuted(ActionExecutedContext filterContext)

        {

            Log("OnActionExecuted", filterContext.RouteData);

        }

 

        public override void OnResultExecuting(ResultExecutingContext filterContext)

        {

            Log("OnResultExecuting", filterContext.RouteData);

        }

 

        public override void OnResultExecuted(ResultExecutedContext filterContext)

        {

            Log("OnResultExecuted", filterContext.RouteData);

        }

 

        private void Log(string methodName, RouteData routeData)

        {

            var controllerName = routeData.Values["controller"];

            var actionName = routeData.Values["action"];

            var message = String.Format("{0} controller:{1} action:{2}", methodName,

                                controllerName, actionName);

            Debug.WriteLine(message, "Action Filter Log");

        }

 

    }

}

 

Listing2에서 OnActionExcuting(), OnActionExcuted(), OnResultExecuting(), OnResultExecuted() 메소드를 Log()메소드에서 모두 호출한다.

메소드명과 정확한 라우트 데이터는 Log() 메소드를 통과한다.

Log() 메소드는 Visual Studio Output 윈도우에 메시지를 출력한다. ( 디버그 모드로 )

 

 

 

Home컨트롤러의 Index() 액션이든 아니든 Home컨트롤러에의 모든 액션에 대해서 동작을 한다.

그 동작된 내용을 Output창에 출력이 된다.

 

namespace ExamActionFilter.Controllers

{

    //[HandleError]

    [LogActionFilter]

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }

 

        public ActionResult About()

        {

            return View();

        }

    }

}

 

여기까지 간단한 액션 필터를 구현하는 것을 배웠다.

 

마스터 페이지

 

이 단원에서는 공통 페이지 레이아웃을 만드는 방법에 대해 배울 것이다.

마스터 페이지의 강점을 배우고 사이트에 로고나 메뉴 링크 그리고 배너 광고를 마스터 페이지 내에 배치하는 것을 배울 것이다.

 

마스터 페이지 만들기

 

 

하나의 어플리케이션 안에 하나 이상의 마스터 페이지를 만들 수 있으며 각각의 마스터 페이지는 각각 다른 레이아웃을 정의 할 수 있다.

 

컨텐츠 페이지 만들기

마스터 페이지를 만든 이후 마스터 페이지를 기본으로 하나 이상의 컨텐츠 페이지를 만들 수 있다.

 

 

  

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

    브라우저 타이틀 변경

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

 

    <h2>Contents Page</h2>

 

 

</asp:Content>

 

 

추가한 index 페이지에서 타이틀을 변경한 내용이다. 마스터 페이지의 타이틀 부분에 컨텐츠를 만들어 놓으므로써

각 컨텐츠 페이지의 타이틀을 넣을 수 있다.

 

 

마스터 페이지로 데이터 전달

 

이 단원의 목표는 마스터 페이지에 데이터 전달할 수 있는가를 설명하고 있다.

영화 데이터베이스를 구축하고 영화 카테고리 테이블과 영화 목록 테이블 2개를 만들 것이다.

카테고리를 뿌려주고 그 뿌려준 카테고리에 속한 영화 리스트를 보여줄 것이다.

 

 

데이터베이스 Movies를 만들자. 만들어진 데이터베이스를 더블 클릭하면 서버 탐색기로 이동하여 해당 데이터베이스의 정보를 볼 수 있다.

 

 

 

모델 폴더에 해당 데이터베이스 관련된 LINQ to SQL을 생성한다

 

 컨트롤러 페이지

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcApplication1.Models;

 

namespace MvcApplication1.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        private MoviesDataContext _dataContext = new MoviesDataContext();

 

        public ActionResult Index()

        {

            ViewData["categories"] = from c in _dataContext.MovieCategory

                                     select c;

            ViewData["movies"] = from m in _dataContext.Movie

                                 select m;

            return View();

        }

 

        public ActionResult Details(int id)

        {

            ViewData["categories"] = from c in _dataContext.MovieCategory

                                     select c;

            ViewData["movies"] = from m in _dataContext.Movie

                                 where m.CategoryId == id

                                 select m;

            return View();

        }

    }

}

 

 디테일 페이지

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import! Namespace="MvcApplication1.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

        Details

</asp:Content>

 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Details</h2>

    <% foreach (var m in (IEnumerable<Movie>)ViewData["movies"])

         { %>

         <li><%= m.title %></li>

 

    <% } %>

</asp:Content>

 

 마스터 페이지

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<%@ Import! Namespace="MvcApplication1.Models" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

</head>

<body>

    <div>

        <h1>My Web Site</h1>

       

          <% foreach (var c in (IEnumerable<MovieCategory>)ViewData["categories"])

             {%>

               <%= Html.ActionLink(c.name, "Details", new {id=c.id} ) %>

 

          <% } %>

   

        <asp:ContentPlaceHolder ID="MainContent" runat="server">

       

        </asp:ContentPlaceHolder>

    </div>

</body>

</html>

 

위의 소스는 마스터 페이지에서 영화 카테고리를 클릭하면 해당 카테고리에 속한 영화 타이틀이 출력되는 예제이다.

 

 위의 소스는 마스터 페이지에서 영화 카테고리를 클릭하면 해당 카테고리에 속한 영화 타이틀이 출력되는 예제이다.

 

 

Good Solution

위의 예제는 심플하게 데이터를 뿌려주고 있다. 하지만 컨트롤러 소스를 보면 영화 카테고리 정보를 중복되게 사용하는 것을 보게 될 것이다.

왜 중복되게 보내는 것일까? 그 이유는 마스터에서 사용하는 카테고리 ViewData 때문이다.

이 단원에서 배울 것은 위의 예제를 좀 더 개선하여 좋을 프로그램으로 만드는 것이 목적이다.

 

우리는 컨트롤러에 추상화로 구현한 클래스를 하나 만들 것이다.

이 클래스의 역할은 Movie데이터를 사용할 수 있게 속성(프로퍼티)을 하나 제공하며 생성자에서 카테고리 정보를 ViewData로 만들어 주는 것을 한다.

그리고 잊으면 안되는 것이 이 클래스는 컨트롤러를 상속 받는 것이다.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;

using MvcApplication1.Models;

 

namespace MvcApplication1.Controllers

{

    public abstract class ApplicationController : Controller

    {

        private MoviesDataContext _dataContext = new MoviesDataContext();

 

        public MoviesDataContext DataContext

        {

            get { return _dataContext; }

        }

 

        public ApplicationController()

        {

            ViewData["categories"] = from c in DataContext.MovieCategory

                                     select c;

        }

    }

}

 

해당 추상 클래스를 사용할 Movies컨트롤을 만들겠다.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;

using MvcApplication1.Models;

 

namespace MvcApplication1.Controllers

{

    public class MoviesController : ApplicationController

    {

        /// <summary>

        /// Show list of all movies

        /// </summary>

        public ActionResult Index()

        {

            ViewData["movies"] = from m in DataContext.Movie

                                 select m;

            return View();

        }

 

        /// <summary>

        /// Show list of movies in a category

        /// </summary>

        public ActionResult Details(int id)

        {

            ViewData["movies"] = from m in DataContext.Movie

                                 where m.CategoryId == id

                                 select m;

            return View();

        }

    }

}

 

소스를 보면 알듯이 ApplicationController를 상속 받아 Movie데이터를 사용하는 것을 볼 수 있다.

그리고 달라진 것이 마스터에 보내야 하는 정보를 이미 추상 클래스에서 해결을 했으므로 Movies컨트롤러에서는 할 필요가 없어진 것이다.

 

Controller를 상속 받은 클래스로 인해 추상 클래스를 상속 받은 모든 클래스는 어느 액션이든 호출될 때 마다 자동으로 영화 카테고리 정보를 ViewData에 포함하게 된다.

'Web Platform' 카테고리의 다른 글

에러) System.Web.HttpException: 최대 요청 길이를 초과했습니다.  (0) 2009.06.29
ASP.NET MVC 액션 필터  (0) 2009.06.29
ASP.NET MVC TagBuilder  (0) 2009.06.29
ASP.NET MVC View  (0) 2009.06.29
ASP.NET MVC Routing 기술  (0) 2009.06.29

HTML Helper 만들고 TagBuilder 사용하기

 

ASP.NET MVC 프레임웍은 HTML Helper를 만들때 TagBuilder라는 utility 클래스를 사용한다.

TagBulider 클래스는 손쉽게 HTML 태그를 만드는 것을 가능하게 하고 생각하네 하는 클래스이다.

 

TagBulider클래스는 System.Web.Mvc 네임스페이스에서 제공되고 5개의 Method를 가지고 있다.

 

AddCssClass()

새로운 Class=”” 속성을 추가

GenerateId()

Id 속성 추가

MergeAttribute()

속성을 추가. 다수의 overload 제공

SetInnerText()

Inner text 추가.

ToString()

기본적인 tag, “<”, “>”, “< />”

 

 

Image HTML Helper 만들기

tag명을 넣어 TagBuilder 생성자를 통해 TagBuilder Class의 인스턴스를 만든다.

다음으로 태그의 속성을 수정하기 위해 AddCssClass MergeAttribute() 메소드를 호출한다.

마지막으로 ToString() 메소를 호출하여 태그를 제공한다.

 

/Helpers/ImageHelper.cs

 

using System.Web.Mvc;

using System.Web.Routing;

 

namespace MvcMyApplication4.Helpers

{

    public static class ImageHelper

    {

        public static string Image(this HtmlHelper helper, string id, string url, string alternateText)

        {

            return Image(helper, id, url, alternateText, null);

        }

 

        public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)

        {

            // Create tag builder

            var builder = new TagBuilder("img");

 

            // Create valid id

            builder.GenerateId(id);

 

            // Add attributes

            builder.MergeAttribute("src", url);

            builder.MergeAttribute("alt", alternateText);

            builder.MergeAttributes(new RouteVal!ueDictionary(htmlAttributes));

 

            // Render tag

            return builder.ToString(TagRenderMode.SelfClosing);

        }

    }

}

 

/Home/Index.aspx

 

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import! Namespace="MvcMyApplication4.Helpers" %>

 

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">

    Home Page

</asp:Content>

 

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

 

    <!-- Calling helper without HTML attributes -->

    <%= Html.Image("img1", ResolveUrl("~/Content/XBox.png"), "XBox Console") %>

 

    <!-- Calling helper with HTML attributes -->

    <%= Html.Image("img1", ResolveUrl("~/Content/XBox.png"), "XBox Console", new {border="4px"})%>

 

</asp:Content>

 

 

 

 

<%@ Import! Namespace="MvcMyApplication4.Helpers" %> index.aspx view의 상단에 반드시 import! 해야 한다.

'Web Platform' 카테고리의 다른 글

ASP.NET MVC 액션 필터  (0) 2009.06.29
ASP.NET MVC 마스터 페이지  (0) 2009.06.29
ASP.NET MVC View  (0) 2009.06.29
ASP.NET MVC Routing 기술  (0) 2009.06.29
ASP.NET MVC 개발 환경 만들기  (0) 2009.06.29

데이터베이스 데이터 테이블 출력하기

HTML Table에 데이터베이스 레코드를 구성하는 2개의 Method를 보자

 

Database 만들기

프로젝트에 App_Data폴더 오른쪽 버튼을 클릭하여 추가에 새 항목을 선택하면 다음과 같은 화면이 나온다.

 

 데이터 -> SQL Server 데이터베이스 선택 후 이름은 Movies.mdf 로 한 후 추가 버튼을 클릭한다.

 

 

Model Class 만들기

Movies 데이터베이스 테이블의 레코드 구성을 보여주는 것부터 출발한다. Movies 데이터베이스 테이블은 다음과 같은 컬럼으로 구성되어 있다.

 

Table : tblMovie

Column Name

Data Type

Allow Nulls

Id

Int

False

Title

Nvarchar(200)

False

Director

NVarchar(50)

False

DateReleased

DateTime

False

 

Movies 데이터베이스 테이블 나타내는 순서로는 LINQ to SQL 데이터 접근 기술을 사용할 것이다.

다른 단어로는 MVC model classes by using LINQ to SQL

 

프로젝트에 보면 Models폴더가 있는데 Models폴더 오른쪽 버튼을 클릭하여 추가에 새 항목을 선택하면 다음과 같은 화면이 나온다.

데이터 -> LINQ to SQL 클래스 선택 후 이름은 Movie.dbml로 한 후 추가버튼을 클릭한다.

 

 

서버탐색기에서 만든 테이블을 Movie.dbml에 끌어다 놓으면 다음과 같이 출력된다.

 (테이블의 컬럼은 알아서 만들어 보자 )

여기까지 데이터베이스 생성 및 Models LINQ to SQL 파일 생성이 모두 완료되었다.

 

 

Controller Action LINQ to SQL 사용하기

Controller에서 LINQ to SQL을 사용하려면

Using System.Linq; Using 프로젝트명.Models; 를 선언해야 한다.

 

using System.Linq; // 추가

using System.Web.Mvc;

using MvcMyApplication3.Models; //추가

 

namespace MvcMyApplication3.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            // 사용할 ModelsLINQ to SQL Class

            var dataContext = new MovieDataContext();  

            var movies = from m in dataContext.tblMovie // 테이블명

                         select m;

            return View(movies);

        }

    }

}

 

View에 데이터 출력하기

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import! Namespace="MvcMyApplication3.Models" %>

 

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">

    Home Page

</asp:Content>

 

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

 

<table>

<tr>

     <th>Id</th><th>Title</th><th>Release Date</th>

</tr>

<% foreach (tblMovie m in (IEnumerable)ViewData.Model)

{ %>

<tr>

     <td><%= m.id %></td>

     <td><%= Html.Encode(m.Title) %></td>

     <td><%= m.DateReleased %></td>

</tr>

<% } %>

</table>

 

</asp:Content>

 

 여기까지 View page에 데이터 출력하는 것이 완료 되었다.

MVC
에서는 Routing Controller Method를 통해 View page를 호출을 한다

using System.Web.Mvc;

 

namespace MvcMyApplication2.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();

        }

 

        public ActionResult About()

        {

            return View();

        }

 

        public ActionResult Detail()

        {

            return RedirectToAction("Index");

        }

     }

}

HomeController 에는 3개의 Index(), About(), Detail() action Method가 있다.

 

action은 다음과 같이 주소표시줄에 표기된다.

Index()

/Home/Index

About()

/Home/About

Detail()

/Home/Detail

 

Index() action view를 리턴한다. 어떤 action은 다른 type action 결과를 리턴할 수 있다.

예를 들어 Detail() action처럼 Index() action을 요청하는 RedirectToActionResult를 리턴할 수 있다.

 

Index() action View();

반드시 웹서버에 \Views\Home\Index.aspx가 있어야 한다.

 

만약 View(“Fred”); 를 리턴하면

\Views\Home\Fred.aspx 실행된다.

 

View에 내용추가

View는 다양한 script내용을 포함할 수 있는 html문서이다.

예를 들어 날짜를 출력하는 view를 보겠다.

  

Add.aspx

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Add</title>

</head>

<body>

    <div>

       

    The current date and time is<br />

    <% Response.Write(DateTime.Now); %>

 

    </div>

</body>

</html>

이 구분기호 <% %> C#에서 쓰여진 script이다.

Response.Write() Method는 브라우저 내용에 현재 날짜를 보여준다.

이 구분기호 <% %>은 하나 또는 더 많은 세크먼트를 실행하는데 사용된다.

 

Result

The current date and time is
2009-04-18
오후 10:45:43

Add2.aspx

<%@ Page Title=”” Language=”C#” Inherits=”System.Web.Mvc.ViewPage” %>

 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

 

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

    <title>Add</title>

</head>

<body>

    <div>

 

    The current date and time is<br />

    <%=DateTime.Now %>

 

    </div>

</body>

</html>

<%=%> 구분기호는 Response.Write() 를 간략하게 쓰는 기호이다.

Result

The current date and time is
2009-04-18
오후 10:45:43

 

 

View에서 HTML Helper 사용하기

HTML HelperTextbox, Link, Dropdown List, List Box등의 표준 HTML 요소를 사용할 수 있게 한다.

 

Login.aspx

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Login Form</title>

</head>

<body>

    <div>

   

    <% using (Html.BeginForm())

       { %>

        

        <label for="UserName">User Name:</label>

        <br />

        <%= Html.TextBox("UserName") %>

       

        <br /><br />

           

        <label for="Password">Password:</label>

        <br />

        <%= Html.Password("Password") %>

       

        <br /><br />

 

        <input type="submit" value="Log in" />       

   

    <% } %>

   

    </div>

</body>

</html>

HTML Source

<form action="/Home/Login" method="post">

 

        <label for="UserName">User Name:</label>

        <br />

        <input id="UserName" name="UserName" type="text" value="" />

       

        <br /><br />

           

        <label for="Password">Password:</label>

        <br />

        <input id="Password" name="Password" type="password" />

       

        <br /><br />

 

        <input type="submit" value="Log in" />       

   

    </form>

 

위에서 보듯이 HTML Helper는 각 HTML 요소로 변경된다. 기존의 서버컨트롤과는 다른 string만 변경하여 리턴을 해준다. ( 랜더링 작업이 없다. )

 

 

View View Data 사용하기

Controller 에서 ViewData["Message"] = "Welcome to ASP.NET MVC!"; 설정하면

ViewData["Message"]View Page에서 사용할 수 있다.

<%= Html.Encode(ViewData["Message"]) %> 하면 View Page Welcome to ASP.NET MVC! 라고 출력이 된다.

 

 

Custom HTML Helper 만들기

ASP.NET MVC HTML Helper의 종류는 다음과 같다.

  • Html.ActionLink()
  • Html.BeginForm()
  • Html.CheckBox()
  • Html.DropDownList()
  • Html.EndForm()
  • Html.Hidden()
  • Html.ListBox()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextArea()
  • Html.TextBox()

<label>관련된 static Method를 만들려고 한다.

우리가 가끔 string 관련 함수를 만들어 return 하는 방식과 같다.

Helpers/LabelHelper.cs

using System;

 

namespace MvcMyApplication2.Helpers

{

    public class LabelHelper

    {

        public static string Label(string target, string text)

        {

            return String.Format("<label for='{0}'>{1}</label>", target, text);

        }

    }

}

HTML 요소의 특성에 맞게 파라메터를 받고 스트링 문자열을 만들어 return 한다.

이렇게 만든 클래스를 사용해보자

 

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import! Namespace="MvcMyApplication2.Helpers " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

     <title>Index2</title>

</head>

<body>

     <div>

          <% using (Html.BeginForm())

          { %>

               <%= LabelHelper.Label("firstName", "First Name:") %>

               <br />

               <%= Html.TextBox("firstName")%>

               <br /><br />

               <%= LabelHelper.Label("lastName", "Last Name:") %>

               <br />

               <%= Html.TextBox("lastName")%>

               <br /><br />

               <input type="submit" value="Register" />

          <% } %>

     </div>

</body>

</html>

 

Result

 

 

HTML Helpers Extension Method 만들기

HtmlHelper 클래스에 Label() 확장 메소드를 추가하기

1.     클래스는 Static class

2.     Static classExtension Method를 정의 해야한다.

 

Helpers/LabelExtensions.cs

using System;

using System.Web.Mvc;

 

namespace MvcMyApplication2.Helpers

{

    public static class LabelExtensions

    {

        public static string Label(this HtmlHelper helper, string target, string text)

        {

            return String.Format("<label for='{0}'>{1}</label>", target, text);

        }

    }

}

 

작성 후 View Page Import! 하면 아래 그림과 같이 출력이 된다.

 

 

 

'Web Platform' 카테고리의 다른 글

ASP.NET MVC 마스터 페이지  (0) 2009.06.29
ASP.NET MVC TagBuilder  (0) 2009.06.29
ASP.NET MVC Routing 기술  (0) 2009.06.29
ASP.NET MVC 개발 환경 만들기  (0) 2009.06.29
ASP.NET MVC(Model, View, Controller)란?  (0) 2009.06.29

ASP.NET MVC Routing

 

블로그나 카페등에서 사용되는 확장자 없는 URL을 자유롭게 설정해 주는 기능

Global.asax에서 routes.MapRoute() 메소드를 통해 매핑규칙을 추가하면

http://www.mnstime.com/microsoft/aspnet/mvc/routing 와 같은 URL이 가능하다는 것이다.

 

RoutingGlobal.asax에서 설정을 한다.

Global.asax의 소스를 보면 다음과 같다.

 

public class MvcApplication : System.Web.HttpApplication

    {

        public static void RegisterRoutes(RouteCollection routes)

        {

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

            routes.MapRoute(

                "Default",                                              // Route name

                "{controller}/{action}/{id}",                           // URL with parameters

                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

            );

        }

        protected void Application_Start()

        {

            RegisterRoutes(RouteTable.Routes);

        }

    }

 

프로젝트를 생성하면 Global.asax에 기본 routes.MapRoute() 이 설정되어 있다.

만약 URL /Home/Detail/10 로 요청이 들어오면 "{controller}/{action}/{id} 순으로

 

Controller

Home Controller

Action

Detail Method

Id

10 ( 5 Parameter )

이렇게 매핑이 된다.

 

이 매핑의 규칙은 Global.asax에서 routes.MapRoute() 통해 다양하게 설정할 수 있다.

URL 요청은 순서대로 다양하게 설정한 routes.MapRoute() 중에서 적용이 되는 제일 첫번째 설정으로 매핑이 된다.

 

Default Routing

MVC URL에 대한 Default Routing 다음과 같다.

 

Global.asax.cs

 

routes.MapRoute(

                "Default",                                              // Route name

                "{controller}/{action}/{id}",                           // URL with parameters

                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

            );

 

다음은 URL에 대한 요청 설명이다.

http://localhost/ 라면 Paramter defaults로 인해 Home Controller Index Method를 호출한다.

http://localhost/Shop/List/ 라면 Shop Controller List Method가 호출된다.

http://localhost/Shop/List/1 라면 Shop Controller List Method 1값이 파라메터로 전달이 된다.

 

Customer Routing

Routing은 기본적으로 {controller}/{action}/{id} 형태로 구성되지만 다른 형태로 구성 할 수 있다.

 

Global.asax.cs

 

routes.MapRoute(

                "Plan",                                                     // Route name

                "Plan/{action}/{id}",                                       // URL with parameters

                new { controller = "Plan", action = "List", id = "" }    // Parameter defaults

            );

http://localhost/Plan/View/2

PlanMgt Controller View Method 2값이 파라메터로 전달이 되며 /Plan View.aspx를 호출한다.

그리고 List.aspx 뷰가 만들어져야 http://localhost/Plan/ 를 요청했을 경우 List.aspx가 호출이 된다.

 

Global.asax.cs

 

routes.MapRoute(

                "ShopProduct",                                                     // Route name

                "Shop/{controller}/{action}/{id}",                                       // URL with parameters

                new { controller = "Product", action = "Index", id = "" },    // Parameter defaults

                new { controller = "(Product)|(Product2)" }

            );

 

※ 중요

Route를 여러개 지정하여 사용할 경우 위에서부터 차례대로 해석하는데 Default Route이 최상단으로 올리는 경우 Default Route에서 URL을 해석하여 페이지 에러가 발생하게 된다.

Customer Route를 생성했을 경우에는 먼저 Customer Route를 넣어준 후 Default Route를 넣어주는게 좋은 방법이다.

 

 

URLController 찾기

http://localhost/Shop

기본으로 Product Controller Index Method를 호출하여 /Product Index.aspx를 호출한다.

 

http://localhost/Shop/Product2/

Product2 Controller Index Method를 호출하여 /Product Index.aspx를 호출한다.

 

http://localhost/Shop/Product3/

Product3 Controller가 존재하지 않아 오류가 발생한다.

 

 

URLParameter 전달하기

http://localhost/Shop/Product/Index/Hello

Controller Index Method Hello파라메터값을 전달하여 Index.aspx를 호출한다.

값의 전달을 위해 소스를 아래와 같이 수정하였다.

 

ProductController.cs

 

        public ActionResult Index(string id)

        {

            ViewData["ProductParam"] = id;

            return View();

        }

 

Index.aspx

 

<%=Html.Encode(ViewData["ProductParam"]) %>

  

간단하게 하나의 파라메터 값을 전달하였다.

 

 

응용해서 2개의 파라메터를 전달하려면 어떻게 해야 할까?

http://localhost:7002/Shop/Product/Index/Hello/Hi

 

Global.asax.cs

 

            routes.MapRoute(

                "ShopProduct",                                                     // Route name

                "Shop/{controller}/{action}/{id}/{id2}",                                       // URL with parameters

                new { controller = "Product", action = "Index", id = "", id2 = "" },    // Parameter defaults

                new { controller = "(Product)|(Product2)" }

            );

 

ProductController.cs

 

        public ActionResult Index(string id , string id2)

        {

            ViewData["ProductParam"] = id;

 ViewData["ProductParam2"] = id2;

            return View();

        }

 

Index.aspx

 

<%=Html.Encode(ViewData["ProductParam"]) %> / <%=Html.Encode(ViewData["ProductParam2"]) %>

 

 

 

Views Win Form 추가하기

지금까지는 그림에서 처럼 Views/ProductView 항목을 통해 Page를 추가했다.

 

지금은 새 항목을 통하여 Web Form을 추가하는 것을 해보겠다.

 

 

새 항목을 클릭 후 Web Form 선택하여 Detail.aspx라는 Web Form을 만들자.

다음으로 Product Controller Detail Action을 만들어 연결을 해보자

Product Controller.cs

 

        public ActionResult Detail()

        {

            return View();

        }

 

 http://localhost:7002/Shop/Product/Detail URL을 입력하면 다음과 같은 에러가 출력된다.

Detail.aspx ViewPage의 상속을 받지 않아서 생기는 문제이다.

  

Detail.aspx.cs의 코드를 수정하자.

Detail.aspx.cs

 

using System.Web.Mvc; // 추가

 

namespace MvcMyApplication.Views.Product

{

    public partial class Detail : ViewPage //System.Web.UI.Page 를 변경

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

    }

}

 

'Web Platform' 카테고리의 다른 글

ASP.NET MVC TagBuilder  (0) 2009.06.29
ASP.NET MVC View  (0) 2009.06.29
ASP.NET MVC 개발 환경 만들기  (0) 2009.06.29
ASP.NET MVC(Model, View, Controller)란?  (0) 2009.06.29
날짜 관련 함수  (0) 2009.06.29

ASP.NET MVC Web Application 만들기

 

새로운 MVC 프로젝트를 만들기 위해서는 아래와 같은 환경이 준비되어야 한다.

1.      Microsoft Visual Studio2008

2.      .NET Framework 3.5 SP 1

3.      ASP.NET MVC 1.0

4.      IIS 7.0 (IIS 6.0 이상)

 

 

ASP.NET MVC 1.0http://www.asp.net/MVC/ 에서 다운 받을 수 있다.

버튼을 클릭하면 Web Platform Installer 2.0 Beta를 설치한다.

 

 

설치를 완료 하면 아래와 같은 화면이 나온다.

 

Web Platform -> Frameworks and Runtimes Customize를 선택하면

ASP.NET MVC 1.0 .NET Framework 3.5 SP1를 체크한 후 Install을 하면 설치가 된다.  

 

 

이제 환경이 준비 되었으니 새로운 MVC 프로젝트를 만들어보자.

 

 

Microsoft Visual Studio2008을 실행 한 후 새 프로젝트를 선택하면 아래와 같이 ASP.NET MVC Web Application이 생성된 것을 볼 수 있다.

 

 

 

MvcMyApplication 이름으로 새 프로젝트를 생성하자.

다음과 같은 화면이 나오는데 No, do not create a unit test project를 선택 -> OK

(Yest, create a unit test project를 하게 되면 단위테스트를 위한 프로젝트가 같이 생성이 된다.)

 

Ctrl + F5를 눌러 실행해보자.

 

이렇게 해서 하나의 MVC Application을 만들어보았다

 

 

'Web Platform' 카테고리의 다른 글

ASP.NET MVC View  (0) 2009.06.29
ASP.NET MVC Routing 기술  (0) 2009.06.29
ASP.NET MVC(Model, View, Controller)란?  (0) 2009.06.29
날짜 관련 함수  (0) 2009.06.29
JavaScript를 이용한 정규식 표현  (0) 2009.06.29

MVC란 객체지향언어인 Smalltalk에서 도입되었다.

 

MVC의 세부분의 구성은 다음과 같은 역활을 한다.

 

1. Model(Biz 로직

비즈니스를 처리한다. 데이터 부분의 클래스 및 논리적인 데이터 기반 구조를 표현

2. View(Biz의 프리젠테이션)

사용자가 보게 될 결과 화면을 담당한다.

3. Contoller(흐름제어)

Model View와의 통신을 담당한다.


MVC 어플리케이션을 만들기는 결정할 때

ASP.NET MVC framework 인지 혹은 ASP.NET Web Forms 모델 인지를 결정하는 것은 주의 깊게 고려해야 한다.

MVC framework ASP.NET Web Forms을 교체하지는 않는다.

결정하기 이전에 웹 어플리케이션에 주는 이점을 살펴야 한다

 

ASP.NET MVC 기반 웹 어플리케이션은 다음과 같은 이점을 제공한다.

* Model, View, Controller가 분리되어 있으므로 관리의 복잡성이 줄어든다.

* View State Server Based Control을 사용하지 않는다. 이것은 어플리케이션의 동작을 완전히 제어하려는 개발자들에게 이상적이다.

* 좀더 나은 Test-Driven Development의 지원

* 높은 수준의 제어가 필요한 개발자와 설계자들의 큰 팀을 지원하는 Web 어플리케이션에 적합

 

Web Forms 기반 웹 어플리케이션은 다음과 같은 이점을 제공한다.

* HTTP를 통해 상태를 저장하는 이벤트 모델을 제공한다. 서버컨트롤에서 지원하는 수만은 이벤트들을 지원한다.

* 상태정보를 쉽게 관리할 수 있는 View Status 또는 Server-based Forms를 제공한다.

* 많은 수의 컴포넌트를 빠르게 개발하려는 개발자 및 설계자들의 작은 팀에 적합.

* 일반적으로 컴포넌트가 긴밀하게 통합되어 있고 MVC 모델 보다 적은 코드가 필요하기 때문에 개발의 복잡도가 적다

 

MVC 어플리케이션에서는 더 이상 ViewState와 포스트백을 사용하지 않는다.

 

'Web Platform' 카테고리의 다른 글

ASP.NET MVC Routing 기술  (0) 2009.06.29
ASP.NET MVC 개발 환경 만들기  (0) 2009.06.29
날짜 관련 함수  (0) 2009.06.29
JavaScript를 이용한 정규식 표현  (0) 2009.06.29
location.href 와 location.replace의 차이  (0) 2009.06.29

+ Recent posts