Đặt làm trang chủ     Ghi nhớ (bookmark)     RSS     Đăng ký     Đăng nhập ?>

PN2design ’s Blog

Internet is my life - Thủ thuật IT

Lưu trữ của danh mục ‘Website’

Adding Customized Tags in web.config

Thứ năm, 16 Tháng mười, 2008

The web.config file in ASP.NET is the central location for your web applications configuration. It contains settings such as authentication, handler settings, compilation settings, globalization settings, tracing and error settings, etc… But what happens when this is not enough? Or you want to add you own settings into the web.config file. This tutorial will explain how it’s done. To create your own custom configuration handler, it will require two parts: writing some code, and editing your web.config file.

The code
Here we have a small C# file with code to create a new handler that will be used in the web.config file.

  1. using System;
  2. using System.Collections;
  3. using System.Xml;
  4. using System.Configuration;
  5. using System.Web.Configuration;
  6.  
  7. namespace Devhood {
  8.    
  9.     internal class PageStyleHandler:IConfigurationSectionHandler {
  10.         public virtual object Create(Object parent, Object context, XmlNode node) {
  11.             PageStyle config = new PageStyle((PageStyle)parent);
  12.             config.LoadValuesFromConfigurationXml(node);
  13.             return config;
  14.         }
  15.     }
  16.    
  17.     public class PageStyle {
  18.         string _backColour;
  19.        
  20.         internal PageStyle(PageStyle parent) {
  21.             if (parent != null)
  22.                 _backColour = parent._backColour;
  23.         }
  24.        
  25.         internal void LoadValuesFromConfigurationXml(XmlNode node) {
  26.             XmlAttributeCollection attribCol = node.Attributes;
  27.             _backColour = attribCol["backColour"].Value;
  28.         }
  29.        
  30.         public string BackColour {
  31.             get {
  32.                 return _backColour;
  33.             }
  34.         }
  35.     }
  36. }

There are two classes here, the PageStyleHandler class which implements the IConfigurationSectionHandler, and the PageStyle class which is used to store and retrieve the configuration data.
The PageStyleHandler contains the Create method. It is used to create and instance of the PageStyle class to pass the data from the web.config file.
The PageStyle class will accept an XML node which comes from the web.config file, it reads the attribute from the XML node and it will save the data for future retrieval by the BackColour property.

The web.config file
To add your custom handler to the web.config file for this application, it requires simply editing the web.config file so that it will accept your new handler. Your new web.config file will look like this:

  1. <configuration>
  2.     <configSections>
  3.         <sectionGroup name="devhood">
  4.             <section name="pageStyle" type="Devhood.PageStyleHandler, PageStyle" />
  5.         </sectionGroup>
  6.     </configSections>
  7.        
  8.     <devhood>
  9.         <pageStyle backColour="navy" />
  10.     </devhood>
  11. </configuration>

Note that this example will only apply to the web application that this file resides in. If you would like this new handler to apply to all web applications on this server, the tag can be moved to the machine.config.

An example usage in an ASPX page
Here is an example of our new custom handler in action:

  1.   <%@ Import Namespace="Devhood" %>
  2.  
  3. <html>
  4.     <head>
  5.         <title>ASP.NET Configuration</title>
  6.         <script language="C#" runat="server">
  7.             void Page_Load(Object sender, EventArgs e) {
  8.                 PageStyle _pageStyle;
  9.                 _pageStyle = (PageStyle) Context.GetConfig("devhood/pageStyle");
  10.                 bodyTag.Attributes["bgcolor"] = _pageStyle.BackColour;
  11.             }
  12.         </script>
  13.     </head>
  14.     <body id="bodyTag" runat="server">
  15.         <table bgcolor="white" align="center" width="400"><tr><td>
  16.         <p align="center">
  17.             <font size=+2>This background is Navy!</font>
  18.         </p>
  19.         <p align="center">
  20.             <font size=+2>Created for <a href="http://www.devhood.com">www.devhood.com</a></font>
  21.         </p>
  22.         </td></tr></table>
  23.     </body>
  24. </html>

The custom configuration handler in ASP.NET is a useful addition for creating really flexible web pages. Usage for custom configuration handlers can be for: allowing the web applications style to be defined in one web.config file, saving information that is commonly used (ie. DSN), and whatever else you can think of.

Copyright © 2001 Andrew Ma. at devhood.com

Giải pháp tìm kiếm tiếng Việt kiểu Google

Thứ bảy, 20 Tháng chín, 2008

Từ trước đến nay mình đã nghe, thấy, gặp nhiều câu hỏi từ nhiều nguồn khác nhau về giải pháp tìm kiếm chuổi tiếng Việt có dấu, không dấu, in hoa, in thường hoặc lẫn lộn các thứ… nhưng mình chưa tìm được một tut nào hướng dẫn về việc này. Mình cũng đã từng nghe các bạn ca ngợi về cách tìm kiếm thông minh của Google, hoặc của một số website khác. Có lần mình bắt gặp một topic trên một diễn đàn tin học lớn kêu gọi thành viên ký tên để gởi thư yêu cầu MySQL hổ trợ tìm kiếm không dấu cho ngôn ngữ của chúng ta. 2 ngày hôm nay mình thử bỏ công tìm hiểu xem sao…

Chuyển đổi chuổi Unicode Upper - Lower

Thứ sáu, 19 Tháng chín, 2008

Lướt vào Blog Blog.TheGioiWebSite.Net của bạn Nguyễn Đức Mạnh, mình bắt gặp 2 hàm PHP tương đối có ích, đặt biệt cần thiết nếu bạn muốn xử lý tìm kiếm Tiếng Việt trên cơ sở dữ liệu. Chẳng hạn trong cơ sở dữ liệu có “thủ thuật lập trình”, bình thường nếu bạn truy vấn với từ khóa “thủ thuẬt” thì sẽ không cho ra kết quả nào dù sử dụng hàm chuyển đổi strtoupper hoặc strtolower có sẵn của PHP trước khi truy vấn. Lý do là các hàm này hoạt động không tốt với các chuổi Unicode.

PHP::Cắt bớt chuỗi Unicode

Thứ tư, 17 Tháng chín, 2008

Khi lập trình Web, bạn thường xuyên phải xử lý chuỗi Unicode. Việc cắt bỏ bớt chuổi (chẳng hạn như cắt bỏ nội dung để lấy một đoạn điển hình, hoặc để được một tiêu đề…) thường gặp phải vấn đề khi chuổi là chuổi Unicode. Trang web có thể bị vỡ (hiển thị sai),… do ký tự Unicode chiếm 2byte, trong khi các hàm thông thường xử lý chuổi các ký tự 1byte. Để giải quyết vấn đề này cần phải có hàm cắt chuổi chuyên dụng cho Unicode. Mình đã tìm thấy hàm này trên diễn đàn rapidshare.vn và gởi lên đây để chia sẽ cùng các bạn. Mã nguồn viết bằng PHP

PHP::Lấy nội dung output của một file được include

Thứ năm, 26 Tháng sáu, 2008

Mã nguồn tôi trình bày sau đây là một ví dụ cụ thể về cách code để lấy nội dung output của một file được include và lưu nội dung đó và một biến. Kỷ thuật này được ứng dụng để cache trong PHP.

PHP::Hàm load() - Lấy nội dung từ một URL

Thứ tư, 25 Tháng sáu, 2008

Đây là mã nguồn tôi sưu tầm được từ http://www.bin-co.com/php/scripts/load/. Nó rất hữu ích nếu bạn

muốn lấy dữ liệu từ Website khác. Rất dễ sử dụng, bạn có thể dùng nó để làm nhiều việc như: Lấy tin tức mới từ các RSS của các trang tin nổi tiếng, lấy bảng giá ngoại tệ, bảng giá vàng, …

Công cụ của lập trình viên PHP

Thứ ba, 24 Tháng sáu, 2008

Tôi sử dụng Windows XP, không phải là một lập trình viên nhưng tôi đã và đang thiết kế + lập trình Website, đối với tôi PHP là một niềm đam mê. Những website tôi truy cập nhiều nhất theo thứ tự có lẽ là: google.com, php.net, mysql.org,… Trong công việc thiết kế website của mình, tôi thường dùng nhiều công cụ khác nhau, nhưng những công cụ sau đây là hay dùng nhất (chắc có lẽ do thói quen _ cũng có thể nó có một số ưu điểm theo cảm nhận cá nhân)