• Creating a Tasklist Application with ASP.NET MVC
  • Understanding Models, Views, and Controllers
  • Understanding Controllers, Controller Actions, and Action Results
  • Understanding Views, View Data, and HTML Helpers
  • An Introduction to URL Routing
  • Preventing JavaScript Injection Attacks
  • Creating Unit Tests for ASP.NET MVC Applications
  • Using ASP.NET MVC with Different Versions of IIS
  • Creating Custom HTML Helpers
  • Creating Model Classes with LINQ to SQL
  • Displaying a Table of Database Data
  • Creating Page Layouts with View Master Pages
  • Passing Data to View Master Pages
  • Understanding Action Filters


    Unit Test는 한글 VS 적용이 안되는 점,
    IIS 쪽 부분은 다른 블로그 내용이 더 좋은 것 같아 다른 글을 다뤄 볼까 해요.
    아직은 어설픈 해석만 하고 있지만 튜토리얼 정리가 끝나고 미니 프로젝트를 다뤄볼까 합니다 :)


    Creating Custom HTML Helpers

     

    이 튜토리얼의 목적은 MVC views에서 사용할 수 있는 사용자 정의 HTML Helpers을 어떻게 만드는 지 설명하는 것입니다. HTML Helpers의 장점으로, HTML 태그의 코드 양을 감소할 수 있습니다.

     

    이 튜토리얼의 첫 파트에서, ASP.NET MVC 프레임워크에 내장된 HTML Helpers에 대해 알아 볼 것이고, 다음으로 사용자 정의 HTML Helpers을 만드는 두 가지 메소드를 설명할 것입니다.

     

    Understanding HTML Helpers

     

    HTML Helper은 단순한 string을 리턴 하는 메소드 입니다. String은 사용자가 원하는 여러 타입의 콘텐트를 나타냅니다. 예를 들어, HTML <input> <img> 태그와 같이 표준 HTML 태그를 렌더링 할 때 HTML Helpers을 사용할 수 있습니다. , 데이터베이스 데이터로부터 HTML table을 만드는 경우와 같이 복잡한 콘텐트를 렌더링 하기 위해서도 사용할 수 있습니다.

     

    ASP.NET MVC 프레임워크는 다음 표준 HTML Helpers을 포함하고 있습니다.

     

    l  Html.ActionLink()

    l  Html.BeginForm()

    l  Html.CheckBox()

    l  Html.DropDownList()

    l  Html.EndForm()

    l  Html.Hidden()

    l  Html.ListBox()

    l  Html.Password()

    l  Html.RadioButton()

    l  Html.TextArea()

    l  Html.TextBox()

     

    예를 들어, 아래의 폼을 생각해 봅시다. 이 폼은 두 개의 표준 HTML Helpers로 렌더링 되었습니다.



     

    <%@ Page Language="C#" AutoEventWireup="true"

    CodeBehind="Index.aspx.cs" Inherits="MvcApplication1.Views.Home.Index"%>

    <!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>Index</title>

    </head>

    <body>

          <div>

              <% using (Html.BeginForm())

              { %>

                   <label for="firstName">First Name:</label>

                   <br />

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

                   <br /><br />

                   <label for="lastName">Last Name:</label>

                   <br />

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

                   <br /><br />

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

              <% } %>

         1     </div>

    </body>

    </html>

     

    Html.BeginForm() Helper 메소드는 HTML <form> 태그의 시작과 끝을 만드는데 사용된다. Html.BeginForm() 메소드는 using 문안에서 호출 되어 진다는 것을 명심해야 합니다. Using 문은 <form> 태그가 using 블록의 끝에서 닫히게 되는 것을 보장해줍니다.

     

    Using 블록을 만드는 것 대신에, <form> 태그를 닫기 위해 Html.EndForm() Helper 메소드를 호출 할 수도 있습니다. 사용자가 편한 방법으로 <form> 태그를 닫고 여는 방법을 선택할 수 있습니다.

     

    위 코드에서 Html.TextBox() Helper 메소드는 Html <input> 태그를 렌더링 합니다. 웹 브라우저에서 소스 보기를 한다면 아래와 같은 코드를 볼 것입니다.

     

    중요! Html.TextBox() Html Helper <$ $> 태그가 아니라 <$= $> 태그 내에서 렌더링 됩니다. ‘=’ 기호가 포함되어 있지 않다면, 브라우저에서 렌더링 되지 않을 것입니다.

     

    <%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Index.aspx.cs"

     Inherits="MvcApplication1.Index" %>

    <!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>

         <title>Index</title>

    </head>

    <body>

         <div>

              <form action="http://localhost:33102/" method="post">

                   <label for="firstName">First Name:</label>

                   <br />

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

                   <br /><br />

                   <label for="lastName">Last Name:</label>

                   <br />

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

                   <br /><br />

                   <input id="btnRegister" name="btnRegister" type="submit" value="Register" />

              </form>

         </div>

    </body>

    </html>

     

    ASP.NET MVC 프레임워크의 helper은 소규모 이기에, 대부분 사용자들은 사용자 정의 HTML Helpers 로 확장하기를 원할 것입니다.

     

    Creating HTML Helpers with Static Methods

     

    새로운 HTML Helper을 만드는 가장 쉬운 방법은 문자를 리턴 하는 static 메소드를 만드는 것입니다. 예를 들어, HTML <label> 태그를 렌더링 하는 새로운 HTML Helper을 만든다고 생각해 봅시다. 사용자는 <label>을 렌더링 하기 위한 아래 코드의 클래스를 사용할 수 있습니다.

     

    using System;

    namespace MvcApplication1.Helpers

    {

              public class LabelHelper

              {

                   public static string Label(string target, string text)

                   {

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

                   }

              }

    }

     

     

    위 코드에서 클래스에 관해 특별한 것이 없습니다. Label() 메소드는 단지 문자를 리턴 합니다.

     

    아래 코드에서 수정된 Index view HTML <label> 태그를 렌더링 하기 위해 LabelHelper을 사용합니다.

    주의! <%@ Import Namespace="MvcApplication1.Helpers" %>

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index2.aspx.cs"

     Inherits="MvcApplication1.Views.Home.Index2"%>

    <%@ Import Namespace="MvcApplication1.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>

     

    Creating HTML Helpers with Extension Methods

     

    사용자가 확장 메소드를 만들 필요가 생길 때, ASP.NET MVC 프레임워크에 포함된 표준 HTML Helpers 처럼 동작하는 HTML Helpers을 만드 -----------

    확장 메소드는 존재하는 클래스에 새로운 메소드를 추가하여 활성화 할 수 있습니다. HTML Helper 메소드를 만들 때, view Html 프로퍼티를 통해 HtmlHelper 클래스에 새로운 메소드를 추가합니다.

     

    아래 코드의 클래스는 Label() 이라는 이름의 HtmlHelper 클래스에 확장 메소드를 추가합니다. 이 클래스에 관해 주의해야 할 사항이 있습니다. 첫 번째는 이 클래스는 static 클래스이기에 사용자는 static 클래스 내에서 확장 메소드를 정의 해야 합니다.

     

    두 번째로 Label() 메소드의 첫 번째 파라메터는 this 키워드에 의해 선행되어 짐을 알아야 합니다. 확장 메소드의 첫 번째 파라메터는 새로운 확장 메소드가 들어가는 클래스를 의미 합니다.

     

    using System;

    using System.Web.Mvc;

     

    namespace MvcApplication1.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);

              }

         }

    }

     

    사용자는 확장 메소드를 만들고 성공적으로 어플리케이션을 빌드 했으면, 확장 메소드는 아래와 같이 Visual Studio Intellisense 에 나타날 것입니다.

     


     

    아래 코드에서 수정된 Index view <label> 태그를 렌더링 할 Html.Label() 확장 메소드를 사용합니다.

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index3.aspx.cs" Inherits="MvcApplication1.Views.Home.Index3" %>

    <%@ Import Namespace="MvcApplication1.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>Index3</title>

    </head>

    <body>

         <div>

              <% using (Html.BeginForm())

              { %>

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

                   <br />

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

                   <br /><br />

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

                   <br />

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

                   <br /><br />

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

              <% } %>

         </div>

    </body>

    </html>



  • Posted by glycerine
    ,

    DevDays 2008

    Microsoft 2008. 12. 4. 11:37


    올해는 오프가 아닌 온라인으로 열린다고 하네요!
    닷넷 개발자라면 빼먹을수 없는 이슈!

    http://www.microsoft.com/korea/devdays2008/

    Posted by glycerine
    ,

    스캇씨의 유용한 링크 모음집!


     I'm flying out later today on a pretty intense business trip (22,000 miles, 5 countries, 3 continents, 1 week, no sleep... :-), so my blog activity over the next week and a half will be pretty light.  To keep you busy till I return, here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page and Silverlight Tutorials page for links to popular articles I've done myself in the past.


    ASP.NET

    • Geolocation/Geotargeting Reverse IP Lookup Code: Scott Hanselman has a cool sample that demonstrates how to perform IP address lookups on users visiting your site to determine where they are located on the globe (down to the latitude and longitude).  Pretty cool stuff.

    • Tracking User Activity: Scott Mitchell has a nice article that discusses how to track end-user activity when visiting an ASP.NET web site.

    • iTunes Data Grid Skin: Matt Berseth continues his cool series showing off cool new skins you can apply to ASP.NET controls (especially the GridView and DetailsView controls).  This post shows off a pretty sweet iTunes like skin.

    ASP.NET Dynamic Data

    • ASP.NET Dynamic Data Videos: Joe Stagner has 6 nice ASP.NET Dynamic Data "How Do I?" videos posted on www.asp.net that you can check out to learn about the new ASP.NET Dynamic Data feature in .NET 3.5 SP1.

    • ASP.NET Dynamic Data Routing: Rachel Appel has a nice post that talks about how to use the new ASP.NET routing features with ASP.NET Dynamic Data to enable customized URLs.

    ASP.NET AJAX

    ASP.NET MVC

    • How to Setup ASP.NET MVC on IIS6: Phil Haack has a great post that walks-through how to enable ASP.NET MVC on IIS6 servers (including how to enable it on a hosting server that you can't install anything on).

    • Fluent Route Testing in ASP.NET MVC: Ben Scheirman has a nice post where he blogs about new helper methods he is creating that make it easier to unit test ASP.NET MVC routes using a fluent API.

    Visual Studio

    WPF / Silverlight

    • XAML Power Toys - Instant Form Creation: Karl Shifflett has a great video that shows off his XAML Power Toys tool that integrates into Visual Studio and enables rapid forms creation for WPF and Silverlight.

    Hope this helps,

    Scott

    Posted by glycerine
    ,

    비주얼 스튜디오 2008 SP1을 설치하고 한글 인텔리센스가 영문으로 둔갑한 적이 있었습니다.
    물론 제 경우만 해당 될 수도 있겠지만.

    당시 SP1 설치 전 인텔리센스를 백업하고 덮어서 해결을 했었는데
    업데이트 파일이 있네요.

    http://code.msdn.microsoft.com/KB957507/Release/ProjectReleases.aspx?ReleaseId=1854
    Posted by glycerine
    ,
  • Creating a Tasklist Application with ASP.NET MVC
  • Understanding Models, Views, and Controllers
  • Understanding Controllers, Controller Actions, and Action Results
  • Understanding Views, View Data, and HTML Helpers
  • An Introduction to URL Routing
  • Preventing JavaScript Injection Attacks
  • Creating Unit Tests for ASP.NET MVC Applications
  • Using ASP.NET MVC with Different Versions of IIS
  • Creating Custom HTML Helpers
  • Creating Model Classes with LINQ to SQL
  • Displaying a Table of Database Data
  • Creating Page Layouts with View Master Pages
  • Passing Data to View Master Pages
  • Understanding Action Filters
  • 결론적으로 자바스크립트 인젝션을 막는 방법은 HTML.encode() 라는 겁니다!!
    오타나 틀린 내용은 꼭 지적해주세요!

    Preventing JavaScript Injection Attacks

     

    이 튜토리얼의 목적은 사용자가 ASP.NET MVC 어플리케이션에서 어떻게 자바스크립트 인젝션 공격을 막는가 설명하는 것입니다. 이 튜토리얼은 자바스크립트 인젝션 공격을 막는 두 가지 방법에 대해 토의할 것입니다. 화면에 보여줄 데이터를 인코딩 함으로써 어떻게 자바스크립트 인젝션 공격을 막는지 알게 될 것입니다.

     

    What is a JavaScript Injection Attack?

     

    사용자의 입력을 받고 입력에 의해 화면이 다시 보여질 때마다, 웹 사이트는 자바스크립트 인젝션 공격에 오픈 됩니다. 자바스크립트 인젝션 공격에 오픈 된 실제 어플리케이션을 시험해보겠습니다.

     

    아래 사진과 같은 사용자 입력 웹 사이트를 만들었다고 가정합니다. 고객은 사이트에 접속하고, 제품을 사용하고 느낀 경험에 대해 의견을 입력할 수 있습니다. 고객이 그들의 의견을 전송할 때, 의견은 의견 페이지로 다시 보여질 것입니다.

     


     

    고객 의견 웹 사이트는 아래 코드의 controller을 사용합니다. controller Index() Create() 라는 두 actions을 가지고 있습니다.

     

    using System;

    using System.Web.Mvc;

    using CustomerFeedback.Models;

     

    namespace CustomerFeedback.Controllers

    {

         [HandleError]

         public class HomeController : Controller

         {

              private FeedbackDataContext db = new FeedbackDataContext();

     

              public ActionResult Index()

              {

                   return View(db.Feedbacks);

              }

     

              public ActionResult Create(string message)

              {

                   // Add feedback

                   var newFeedback = new Feedback();

                   newFeedback.Message = message;

                   newFeedback.EntryDate = DateTime.Now;

                   db.Feedbacks.InsertOnSubmit(newFeedback);

                   db.SubmitChanges();

     

                   // Redirect

                   return RedirectToAction("Index");

              }

         }

    }

     

    Index() 메소드는 Index view을 보여줄 것입니다. 이 메소드는 데이터베이스로부터 의견을 가져와 Index view 로 고객 의견을 전달합니다.

     

    Create() 메소드는 새로운 Feedback 개체를 만들고 그것을 데이터베이스에 저장합니다. 고객이 폼에서 입력한 메시지는 message 파라메터로써 Create() 메소드에 전달됩니다. Feedback 개체가 생성되고, message은 생성된 feedback 개체의 Message 속성에 값이 입력됩니다. Feedback 개체는 DataContext.SubmitChanges() 호출을 통해 데이터베이스에 전송됩니다. 마지막으로 모든 의견이 보여지는 Index view 페이지로 리다이렉트 됩니다.

     

    Index view은 다음 코드를 가지고 있습니다.

     

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"

       CodeBehind="Index.aspx.cs" Inherits="CustomerFeedback.Views.Home.Index"%>

     

    <%@ Import Namespace="CustomerFeedback.Models" %>

     

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

         <h1>Customer Feedback</h1>

         <p>

              Please use the following form to enter feedback about our product.

         </p>

     

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

              <label for="message">Message:</label>

              <br />

              <textarea name="message" cols="50" rows="2"></textarea>

              <br /><br />

              <input type="submit" value="Submit Feedback" />

         </form>

     

         <% foreach (Feedback feedback in ViewData.Model)

         {%>

              <p>

              <%=feedback.EntryDate.ToShortTimeString()%>

              --

              <%=feedback.Message%>

              </p>

         <% }%>

    </asp:Content>

     

    Index view은 두 가지 영역으로 나뉩니다. 상단은 고객 의견 폼이고, 하단은 이전 저장된 고객 의견 아이템을 보여줄 For..Each 루프 영역입니다.

     

    고객 의견 웹 사이트는 간단한 사이트로, 불행히도 자바스크립트 인젝션 공격에 노출되어 있습니다.

     

    고객 의견 폼에서 다음과 같은 문자를 입력한다고 생각해보세요.

    <script>alert(“Boo!”)</script>

     

    이 문자는 경고 메시지 박스를 띄우는 자바스크립트입니다. 누군가 의견 폼에 이 스크립트를 입력하고 전송을 누르면 앞으로 누구든지 이 사이트에 방문하게 되면 메시지 박스 Boo! 가 나타날 것입니다.




    자바스크립트 인젝션 공격에 대한 당신의 초기 반응은 무관심일지도 모릅니다. 아마 자바스크립트 인젝션 공격은 화면을 공격하는 단순한 유형으로 생각할 것입니다. 자바스크립트 인젝션 공격으로 실제로 어떠한 것도 할 수 없다고 생각해보세요.

     

    불행이 해커는 실제로 웹 사이트에 자바스크립트 인젝션으로 사이트를 마비시킬 수 있습니다. Cross-Site Scripting (XSS) 공격을 하기 위해 자바스크립트 인젝션을 사용할 수 있습니다. Cross-Site Scripting 공격으로, 사용자의 정보를 훔칠 수 있고, 다른 웹 사이트로 정보를 보낼 수도 있습니다.

     

    예를 들어, 해커는 다른 유저로부터 브라우저 쿠키 값을 훔칠 수도 있습니다. 비밀번호, 주민등록번호, 또는 신용 카드 번호와 같은 중요한 정보가 쿠키에 저장되어 있다면, 해커들은 그 정보를 알아낼 수 있습니다. 사용자가 자바스크립트 공격에 노출된 페이지에 있는 입력 폼에 정보를 입력한다면, 해커는 자바스크립트 인젝션 공격으로 정보를 가로채 다른 웹 사이트로 보낼 것입니다.

     

    자바스크립트 인젝션 공격은 신중히 처리해야 하고, 사용자의 중요한 정보는 보호해야 할 것입니다. 다음 두 영역에서, 당신이 자바스크립트 인젝션 공격으로부터 ASP.NET MVC 어플리케이션을 보호할 수 있는 두 기술을 이야기 할 것입니다.

     

    Approach #1: HTML Encode in the View

     

    자바스크립트 인젝션 공격을 막는 쉬운 방법은 사용자가 입력한 데이터가 view에 보여질 때, 입력한 데이터를 HTML 인코딩 하는 것입니다. 다음 코드의 수정된 Index view을 보세요..

     

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="CustomerFeedback.Views.Home.Index"%>

     

    <%@ Import Namespace="CustomerFeedback.Models" %>

     

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

         <h1>Customer Feedback</h1>

         <p>

              Please use the following form to enter feedback about our product.

         </p>

     

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

              <label for="message">Message:</label>

              <br />

              <textarea name="message" cols="50" rows="2"></textarea>

              <br /><br />

              <input type="submit" value="Submit Feedback" />

         </form>

     

         <% foreach (Feedback feedback in ViewData.Model)

         {%>

              <p>

              <%=feedback.EntryDate.ToShortTimeString()%>

              --

              <%=Html.Encode(feedback.Message)%>

              </p>

         <% }%>

    </asp:Content>

     

    Feedback.Message의 값은 아래와 같은 코드로 값이 화면에 출력되기 전에 HTML 로 인코딩 되었습니다.

     

    <%=Html.Encode(feedback.Message)%>

     

    HTML을 문자로 인코딩한다는 것은 무엇을 의미하는 것일까? HTML을 문자로 인코딩 할 때, 위험한 문자는, 예를 들어 < >, HTML entity reference , 예를 들어 &lt; &gt; 로 바뀝니다. 그래서, 문자열 <script>alert(“Boo!”)</script> HTML 인코딩 되면, &lt;script&gt;alert(&quot;Boo!&quot;)&lt;/script&gt;. 로 바뀌게 된다. 인코딩 된 문자는 더 이상 자바스크립트로 실행되지 않습니다.

     

     

    Approach #2: HTML Encode in the Controller

     

    view에서 데이터가 화면에 보여질 때, HTML 인코딩 된 데이터 대신, 데이터베이스에 데이터를 전송하기 전에 데이터를 HTML 인코딩 할 수도 있습니다. 두 번째 방안은 아래의 코드처럼 controller 의 경우입니다.

     

    using System;

    using System.Web.Mvc;

    using CustomerFeedback.Models;

    namespace CustomerFeedback.Controllers

    {

         [HandleError]

         public class HomeController : Controller

         {

              private FeedbackDataContext db = new FeedbackDataContext();

     

              public ActionResult Index()

              {

                   return View(db.Feedbacks);

              }

     

              public ActionResult Create(string message)

              {

                   // Add feedback

                   var newFeedback = new Feedback();

                   newFeedback.Message = Server.HtmlEncode(message);

                   newFeedback.EntryDate = DateTime.Now;

                   db.Feedbacks.InsertOnSubmit(newFeedback);

                   db.SubmitChanges();

     

                   // Redirect

                   return RedirectToAction("Index");

              }

         }

    }

     

    Message의 값이 Create() action 내에서 데이터베이스로 전송되기 전에 HTML 인코딩 된 것을 확인해야 합니다. Message view에 다시 보여질 때, Message HTML 인코딩 되고, 자바스크립트 인젝션도 실행되지 않을 것입니다.

     

    전형적으로, 두 번째 접근 방식보다 첫 번째 접근 방식을 선호합니다. 두 번째 방식의 문제는 데이터베이스에 HTML 인코딩 된 데이터가 들어가는 것입니다. 다른 말로, 데이터베이스의 데이터가 보기에 좋지 않은 문자들로 가득할 것입니다.

     

    이것이 왜 나쁜 것일까요? 웹 페이지에서 더 이상 이 데이터베이스의 데이터를 표시할 필요가 없을 경우 문제가 생길 수 있습니다. 예를 들어 윈도우 어플리케이션에서 사용할 때 입니다.

    Posted by glycerine
    ,