잊지 않겠습니다.

게시판 작업을 하나 할 것이 있어서.. 기존의 TextArea를 사용하는 것보다 좀 더 나은 방법을 사용하고 싶은 생각을 가지고 있던 차에 집에서 끄적대다가 발견. 

1. FCKEditor 설치

ASP .NET MVC 모델로 Application을 작성하고, 자신이 생각하면 좋은 위치로 카피.
(여기서는 Scripts/fckeditor/ 폴더로 지정.)




설치가 다 되면 (설치위치)/_samples/default.html 로 들어가면 Sample web page가 다음과 같이 나오면 모든 설치는 완료. 



2. ASP .NET MVC에서 FCKeditor의 사용
먼저 간단히 Editor를 표시해보기 위해서 다양한 방법들이 존재하는데, 기본적으로 ASP .NET MVC는 Page의 Fire & Forget 형태이기 때문에 javascript를 이용한 방법이 가장 좋다. (asp, jsp 등의 다양한 방법들이 있지만, MVC에서 사용하기에는 안좋아 보인다.)

1) FCKeditor의 javascript를 추가한다. 
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>

2) FCKeditor의 인스턴스를 생성한다. 
<script type="text/javascript"> window.onload = function() { var oFCKeditor = new FCKeditor( 'MyTextarea' ) ; oFCKeditor.BasePath = "/fckeditor/" ; oFCKeditor.ReplaceTextarea() ; } </script>
3) textarea를 넣어주고, FCKeditor로 만들어준다. 
<textarea id="MyTextarea" name="MyTextarea">This is <b>the</b> initial value.</textarea>

3. FCKeditor의 값 얻어오기
MVC에서 주로 사용되는 Form을 이용한 POST 방식으로 값을 얻어오는 것이 안되기 때문에 javascript를 이용해서 값을 얻어와야지 된다. POST를 이용해서 FormCollection을 이용하면 값이 넘어오지 않고 Action이 실행되지도 않는다. 
<script type = "text/javascript" language = "javascript">"
    var fckValue = FCKeditorAPI.GetInstance('name').GetHTML();
</script>

주로 사용되는 것이 FCKeditor의 Instance를 생성하고, 그 값을 얻어오는 반복작업을 줄이기 위해서 HtmlHelper class를 작성.  
   public static class FckEditorHelper
    {
        public static string FckTextBoxBase = "../../Scripts/fckeditor/";
        public static string FckTextBox(this HtmlHelper u, string name)
        {
            return u.FckTextBox(name, null);
        }

        public static string FckTextBox(this HtmlHelper u, string name, object value)
        {
            return u.FckTextBox(name, value.ToString());
        }

        public static string FckTextBox(this HtmlHelper u, string name, string value)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(
                "<textarea name=\"{0}\" id=\"{0}\" rows=\"50\" cols=\"80\" style=\"width:100%; height: 600px\">{1}</textarea>",
                name, value);
            sb.Append("<script type=\"text/javascript\" language=\"javascript\">");
            sb.AppendFormat("var oFCKeditor = new FCKeditor('{0}');", name);
            sb.AppendFormat("oFCKeditor.BasePath    = '{0}';", FckTextBoxBase);
            sb.Append("oFCKeditor.Height=400;");
            sb.Append("oFCKeditor.ReplaceTextarea();");
            sb.Append("</script>");
            return sb.ToString();
        }

        public static string PrepareFckTextBox(this HtmlHelper u, string name)
        {
            StringBuilder sb = new StringBuilder("<script type = \"text/javascript\" language = \"javascript\">");
            sb.AppendFormat("var fckValue = FCKeditorAPI.GetInstance('{0}').GetHTML();", name);
            sb.Append("</script>");
            
            return sb.ToString();
        }
    }

사용법은 다음과 같이 사용해주면 된다. 
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <%=Html.FckTextBox("fckTextbox")%>
</asp:Content>

결과는 다음과 같다.

Posted by Y2K
,

C# Code Style Generator

2009. 1. 28. 20:46

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

ASP .NET MVC RC1

.NET Framework 2009. 1. 28. 13:28
ASP .NET MVC Framework가 1월 27일부로 RC 버젼이 나왔다. 
ActionResult의 추가와 ViewPage의 선언의 변화, 그리고 Visual Studio와의 UI 통합등 다양한 기능들이 포함되어서 개발자들의 편의성을 보다 더 높인 것 같다. 

기존의 Beta에서 RC로의 upgrade를 위해서 변경해야지 될 사항은 다음과 같다. 

1. web.config의 변경
기존의 Reference 를 변경시켜주기 위해서 assembly 선언을 변경시켜줘야지 된다. 
기존의 complation/assemblies의 항목을 다음과 같이 변경

    <compilation debug="false">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>

그리고 sectiongroup 도 다음과 같이 변경
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>

마지막으로, View 폴더에 있는 각 View Page에 대한 web.config를 다음과 같이 변경시켜주면 web.config의 변경은 모두 끝나게 된다. 

<?xml version="1.0"?>
<configuration>
  <system.web>
    <httpHandlers>
      <add path="*" verb="*"
          type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
    </handlers>
  </system.webServer>
</configuration>


2. MasterPage의 Page 선언 변경
MVC RC의 가장 큰 변화라고 하면, ViewPage에서의 code behind 파일이 모두 제거가 되었다는 점이다. 이 사항은 MasterPage 역시 마찬가지로, MasterPage의 code behind 파일을 모두 제거해주고, MasterPage의 선언을 다음과 같이 변경시켜주면 된다.

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


3. ViewPage, UserControl의 Page 선언 변경
MasterPage와 마찬가지로, code behind 파일을 모두 제거하고 각 Model에 맞는 Generic Template 형식으로 모든 View의 선언을 해준다. 이때 code behind 파일을 모두 제거하지 않으면 assembly에서 적당한 class를 찾지 못하는 dll hall과 비슷한 에러가 나타나게 된다. 반드시 project에서 모두 제거시켜줘야지 된다.

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


4. ViewData.Model에서 Model로 변경(Optional)
마지막으로, 좀더 코드를 깔끔하게 해주기 위해서 비록 지금 둘다 사용이 가능하지만 ViewData.Model에서 Model만 적어주는 것으로 Replace를 시켜주면 좀더 코드가 깔끔하게 보이게 된다. 

새로운 기능들이 너무나도 많이 추가가 되었다. Preview2에서 3, 4, 5 갈수록 기능이 많아진다는 느낌을 받고 있었는데, 이제는 정말 생각하지도 못했던 기능들 뿐 아니라 코드가 간결해지고 통일성이 생긴다는 느낌이 들게하는 RC 1의 발표로 ASP .NET MVC 모델이 더욱더 멋져진 느낌이다. ^^


Posted by Y2K
,