asp.net

Routing with Webform

คราวก่อนค้างเอาไว้ว่า ถ้าจะทำ Routing ใน ASP.NET Webform จะทำยังไง มาต่อเลยดีกว่า

เนื่องจาก Routing นั้นเป็นส่วนที่จะเพิ่มเข้ามาใหม่ในตัว Framework ของ ASP.NET 3.5 SP1 นั่นก็หมายความว่าเราสามารถใช้ ASP.NET Routing กับ Web Form ได้เหมือนกับที่เราใช้ความสามารถอื่นๆใน Framework นั่นเอง วิธีที่จะเอามาใช้นั้น Chris Cavanagh ได้ระบุวิธีการไว้ดังนี้

After a little experimentation I found some minimal steps that work pretty well:

  • Create a custom IRouteHandler that instantiates your pages
  • Register new Routes associated with your IRouteHandler
  • That’s it!

ถ้าให้แปลเป็นไทยก็คือ

  • สร้าง class ที่ implement IRouteHandler
  • สร้าง Route ให้ url ที่ต้องการ วิ่งไปที่ IRouteHandler ที่สร้างมาใหม่
  • จบเพียงเท่านี้

URL Routing and Rewriting

นี่เป็นคำถามที่อยู่ในหัวผมหลังจากที่อ่านเอกสารใน MSDNแบบผ่านๆในหัวข้อ ASP.NET Routing versus URL Rewriting พอ M3rlinez มาถามก็เลยลองไปหาอ่านละเอียดๆอีกที พบว่าการทำ Routing (ในบริบทของ ASP.NET) มันต่างกับ URL Rewriting อยู่นิดหน่อย

ข้อแรกที่แตกต่างคือ การทำ Routing รองรับการสร้าง URL โดยใส่พารามิเตอร์ที่ต้องการ ซึ่งตรงนี้ใน URL Rewriting ไม่มีกลไกตรงนี้ให้ต้องทำเอง (มันไม่ต่างกันไม่ใช่เรอะ แค่ไมโครซอฟท์ทำไว้ให้แล้ว) ตรงนี้จะว่าเป็นข้อดีก็ได้

ข้อแตกต่างข้อที่สองคือ Routing ทำงานในอีเวนท์ PostResolveCache และ PostMapRequestHandler ซึ่งจุดนี้แหละที่ต่างจากการทำ URL Rewrite จริงๆ

ปกติแล้วการทำ URL Rewrite จะใช้ HttpHandler หรือใช้ HttpModule มาดักตอนอีเวนท์ BeginRequest ซึ่งเป็นอีเวนท์แรกสุด ซึ่งตรงนี้ URL ที่เกิดรีเควส จะถูกแปลงให้อยู่ในรูปของ URL ที่ ASP.NET Webform ธรรมดาๆ เช่น "/products/15" ก็จะถูกเขียนใหม่เป็น "product.aspx?id=15" แล้วค่อยส่งไปให้ไฟล์ดังกล่าว ดังนั้นอีเวนท์ที่เกิดขึ้นด้านหลังอย่าง AuthenticateRequest จะเกิดหลังจากที่ทำการแปลง URL ไปแล้ว ทำให้การตรวจสอบสิทธิต่างๆทำกับไฟล์ปลายทาง (ซึ่งเป็น Physical file) แทนที่จะเป็นไฟล์ที่เห็นจริงๆ แต่สำหรับ URL Routing แล้วเราสามารถใช้งานระบบ Authentication/Authorization ของ ASP.NET กับ logical file ผ่าน web.config ได้ทันที สิ่งนี้ผมยังไม่ได้ทดลอง แต่สรุปเอาเองหลังจากการดูสายการทำงานของอีเวนท์นะครับ

<location path="products">
  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>
</location>

ผลพวงจากการที่ URL ไม่ได้ถูกเปลี่ยนในระหว่างการทำงานทำให้พารามิเตอร์ action ของอีลิเมนท์ form จะถูกตั้งให้เป็น URL ของเดิมจริงๆ ทำให้เราไม่ต้องใช้ Actionless Form เข้ามาใช้

URL Rewriting ที่ผมเคยคิดว่ามันจะกลายเป็นส่วนหนึ่งของ IIS7 นั้นถูกเปลี่ยนให้เป็น Routing แล้วผนวกเข้ามาใน ASP.NET นั้น จุดสำคัญน่าจะมาจากการที่ทีม ASP.NET นั้นได้เริ่มการพัฒนา ASP.NET MVC แล้วก็เลยจำเป็นต้องพัฒนาส่วน Routing ด้วย แล้วไหนๆก็ไหนๆแล้วก็เลยจับมันแยกออกมาเป็นคอมโพเนนท์เลยดีกว่า เพราะว่า ASP.NET Developer เดิมก็มีจำนวนไม่น้อยที่ต้องทำ URL Rewriting ด้วยตัวเอง

สรุปแล้ว URL Routing ไม่ได้มีอะไรพิเศษพิสดารต่างจาก URL Rewriting ซักเท่าไหร่ แนวคิดเดียวกันเป๊ะ แค่นำส่วนที่เราต้องทำแน่ๆอย่างการใช้ Regular Expression ในการสกัด URL และพารามิเตอร์เพื่อส่งให้ Action/WebForm ที่ถูก และการสร้าง URL รวมเข้ามาอยู่ในตัวเฟรมเวิร์กเลย และจุดประสงค์จริงๆก็คงจะเอามาใช้กับ ASP.NET MVC นั่นแหละ

Castle Active Record : Relationship

ต่อจากตอนที่แล้ว

สำหรับ Relation หรือความสัมพันธ์ที่ใช้ใน ActiveRecord จะมีหลักๆดังนี้

  • BelongsTo - ใช้ระบุความสัมพันธ์แบบ many-to-one หรือ one-to-one
  • HasMany - ใช้ระบุความสัมพันธ์แบบ one-to-many
  • HasAndBelongsToMany - ใช้ระบุความสัมพันธ์แบบ many-to-many

Blend to ASPX

ให้ตายสิไมโครซอฟท์ ออก Microsoft Expression Blend มาให้ใช้ แต่กลับไม่มีวิธีง่ายๆในการฝังไฟล์ Silverlight ลงใน ASP.NET !!!

เรื่องเริ่มต้นที่ว่าตอนหัดใช้ Silverlight ก็ต้องมานั่งแกะเองทีละขั้น ตอนนั้นก็คิดว่า ของใหม่เครื่องมือยังห่วยอยู่ ใช้งานยาก ก็น่าให้อภัย แต่ก็คิดว่า ซักวันมันต้องมีคนมาถามแน่ๆเลยว่าจะเอา XAML ไปใช้ยังไง

วันนี้เพื่อนมาถาม

นาย ม. says: วีน ถามหน่อย สมมุติทำของใน blend เส็ดแล้ว จะใส่เข้าไปใน aspx ยังไง

โอ้...พระเจ้า ไมโครซอฟท์ทำสิ่งที่เราฝันไว้ได้จริงๆด้วย

Castle Active Record : Intro

ไม่ต้องเสียเวลาโม้มาก สั้นๆ ง่ายๆเลยแล้วกัน

Castle LogoCastle ActiveRecord เป็นโปรแกรมที่(ลอก)ได้แรงบันดาลใจมาจาก Active Record ที่ใช้ใน Ruby On Rails ซึ่งเอาแนวคิดมาจาก Martin Fowler อีกที จุดมุ่งหมายคือต้องการสร้าง O/R mapper ที่ใช้งานได้ง่ายให้กับ MonoRails ที่เป็นโคลนของ RoR บน .NET ตัว ActiveRecord เองจริงๆแล้วก็สร้างขึ้นมาบน NHibernate อีกที ถึงแม้ว่า ActiveRecord จะถูกสร้างขึ้นมาสำหรับ MonoRails แล้ว แต่เราก็ดึงเอาแค่ ActiveRecord ออกมาใช้งานเฉยๆก็ย่อมได้เช่นกัน

encoding

บังเอิญได้รับงานเขียนเว็บเล็กๆมางานนึง (เล็กมากๆ) ความต้องการคร่าวๆคือมีข้อมูลบนฐานข้อมูล เอาขึ้นมาแสดงบนเว็บตามต้องการ เื่องด้วยปัจจัยหลายๆอย่างก็เลยเลือกใช้ PHP กับ MySQL

จริงๆแล้วก่อนที่จะหัดใช้ ASP.NET ก็เคยเขียน PHP มาอยู่ก่อนแล้ว ยิ่งช่วงหลังก็ได้กลับมาลองเล่นอยู่เนืองๆก็เลยไม่ค่อยเจอปัญหาเท่าไหร่ แต่ก็มีที่เจอคือเรื่ื่อง encoding นี่แหละ

Free ebooks from MS Press

As a new year gift (maybe), Microsoft Press is giving away its 3 ebooks freely included

Register today to download. Here

Flickr ASP.NET MVC app pt.2 - ajaxified

After we have finished the Flickr MVC app last time. This time will be sequel, ajaxified it.

Although the ASP.NET MVC do provide the abstract ajax helper class, only implementation exists publicly is Nikhil's. Certainly, ASP.NET Team will include the ASP.NET AJAX/Microsoft AJAX Library helper in the MVC framework for next CTP bits. As for this tutorial, I will continue with jquery instead of Microsoft AJAX Library because I'm more familiar with it.

The ajaxified instructions is mainly related to the view. In order to use jquery, put the library in ~/Content and add the reference to it in the layout.

<head runat="server">
    <title>My Sample MVC Application</title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../Content/jquery.js"></script>
</head>

Next, Open the ~/Views/Home/Index.aspx. Then, add a placeholder div called "result" in the view. We'll use this div to display the response from the ajax call. Additionally, add a eye-candy loading indicator to let user knows that the page is being loaded.

<%Html.Form("Search", "Home", FormExtensions.FormMethod.post, new { ID = "theform" });%>
    <label for="tags">
        Tags:
    </label>
    <%=Html.TextBox("tags", 30) %>
    <%=Html.SubmitButton("find", "Find", "") %>
    <img src="../../Content/loading.gif" id="spinner" style="display:none;" />
    </form>
<div id="result"></div>

Last but not least, add some lines of script to make an ajax call once the form is goin to be submitted.

 $(document).ready(function() {        
        $('#theform').submit(function (){            
            $.post('/Home/Search', {'tags':$('#tags')[0].value}, completeRequest);
            $('#spinner').show();                        
            $('#result').slideUp(1500);
            return false;            
        });
    });
    
    function completeRequest(r){            
        $('#result').html(r).slideDown(1500);                            
        $('#spinner').hide();        
    }

The script will be invoked when the DOM is ready. It'll hook the form submit event, post the textbox value to ~/Home/Search, make the loading indicator visible, hide the result div and then, cancel the event to prevent the full post process. And then, after the request is completed, completeRequest function will be called. It will replace the div's content with the result from the response and then reveal the div by using slide down effect. And lastly, hide the indicator.

Try the page and Voila! The ajaxified is completed.

Compare to the traditional web form, MVC has a better degree to integrate with the javascript library. Though I use the web form view engine, extension method on the HTML Helper that makes the markup more flexible, no generated id. However, integration with the jquery may not be the easiest one for ASP.NET MVC as ASP.NET Team will deliver its own Ajax helper.

In conclusion, AJAX in ASP.NET MVC has a lot of room for improvement, either officially or unofficially. ASP.NET AJAX becomes useless when using it with ASP.NET MVC. However, using the alternative libraries in MVC require less effort than the traditional way. jQquery and ExtJs are the most notable. Besides, mootools and YUI are also gaining their share. Anyway, Microsoft will push ASP.NET AJAX little ahead those 3rd party library through their Ajax Helper once again.

Flickr ASP.NET MVC app pt.1

After reading many blog post about ASP.NET MVC for a while, I decide to write a simple application for better understanding. Flickr app seems to be best fit this time as it can be implemented in an hour.

ASP.NET MVC Ajax

After the long weekend, Scott announced the ASP.NET 3.5 Extension which includes the preview version of long-waited MVC Framework, ASP.NET MVC. Today, there are many websites and blogs regarding ASP.NET MVC. However, just a few of them are talking about AJAX.

In Web form model of ASP.NET, ASP.NET AJAX, along with AJAX Control Toolkit, is the major player for AJAX library. Since ASP.NET AJAX relies on Web form, migrating it to use with ASP.NET MVC which is just a beta bit is not really worth. But in the end, Microsoft will push its AJAX product towards ASP.NET MVC as soon as the latter is ready for production. This can be seen in Nikhil's post about AJAX in ASP.NET MVC. Though Nikhil uses his Script# to create the javascript library, the concept utilizes the behavior/component things which is the concept of ASP.NET AJAX/AJAX Control toolkit. No doubt that ASP.NET AJAX will be the one that shipped with ASP.NET MVC.

As for alternative, jQuery seems to be the winner at the time of writing. Moreover, The latest survey reveals that the most favorite alternative javascript library among ASP.NET developer is jQuery and I don't think ASP.NET MVC will differs from its counterpart. jQuery, in fact , is the fourth but the first two places are in paired, the second actually is not the javascript library so I count jQuery as the most favorite one. However, mootools, prototype/script.aculo.us will gaining their share in MVC as many people will go to ASP.NET for its MVC.