Monday, August 09, 2010

Rendering barcode in MVC project

To render barcode on web page, we could use TTS barcode font, some of them can be downloaded here for free.

I found some other method that doesn't requiring third-party fonts. One is call Zen Barcode Rendering Framework, it encapsulates the native rendering of barcode symbologies. Zen Barcode has a server side control BarcodeLabel, together with the barcode handler, it can display barcode on ASP.Net page.


To work with MVC project, I wrote a HTML helper method to render the image tag. Also to display a much friend jpg url, I also config a URL rewrite rule on IIS7. Below is the step by step guide:


1. First step is reference two DLLs:
Zen.Barcode.Core.dll
Zen.Barcode.Web.dll

2. Add HttpHandler to web.config so Zen Barcode will handling the .Barcode extension


type="Zen.Barcode.Web.BarcodeImageHandler, Zen.Barcode.Web,
Culture=Neutral, Version=2.0.3.0"/>


In IIS7, the handler also need to be added to system.webServer section.







Also add a namespace to web.config




3. Create HTML helper method for MVC view

With the helper function below, we can write the code in view like


<%= Html.BarcodeImage("634169458190082014")%>


Below is the helper function:


public static string BarcodeImage(this HtmlHelper htmlHelper, string text)
{
return BarcodeImage(htmlHelper, text, BarcodeSymbology.Code128);
}

public static string BarcodeImage(this HtmlHelper htmlHelper, string text, BarcodeSymbology symbology)
{
return BarcodeImage(htmlHelper, text, symbology, 2, 40, 1, 30);
}

public static string BarcodeImage(this HtmlHelper htmlHelper, string text, BarcodeSymbology symbology, int maxWidth, int maxHeight, int minWidth, int minHeight)
{
// Setup barcode image url
BarcodeImageUriBuilder builder = new BarcodeImageUriBuilder();
builder.Text = text;
builder.EncodingScheme = symbology;
builder.BarMinHeight = minHeight;
builder.BarMaxHeight = maxHeight;
builder.BarMinWidth = minWidth;
builder.BarMaxWidth = maxWidth;
string url = BuildEncodedParameter(builder.ToString());
return htmlHelper.Image(url, new { style = "border-width: 0px;" }).ToString();
}

private static string BuildEncodedParameter(string url)
{
if (string.IsNullOrEmpty(url))
url = "";
else
url = url.Replace(".Barcode","");

return "/barcode.jpg?" + url;

}


This helper method will render a img tag like below

 



The query string contains the base64 hash string of barcode information. It should be rewrited to an URL recognized by Zen HTTP handler.

4. URL Rewrite Rules in IIS7

 








This rule will rewrite the above URL to a format like below so Zen Handler can process

 
77u_QmFyY29kZVs0LDMwLDQwLDEsMl06NjM0MTY5NDU4MTkwMDgyMDE0OjE4MTQ4OTMyMQ2.Barcode



Done!


Tuesday, July 27, 2010

ASP.NET MVC 2 Localization complete guide

The post cover the following issues:
  1. Views validation
  2. Simple culture switching mechanism
  3. Model Validation messages localization
  4. DisplayName attribute localization
  5. OutputCache and Localization

Error Handling in MVC with ELMAH

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:

  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recoded exceptions.
  • A web page to remotely view the full details of any one logged exception.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.

Tuesday, May 25, 2010

Install Windows Azure SDK on Windows XP

To play with Azure on visual studio 2010, the cloud service project page will give you a button to download Windows Azure Tools For Microsoft Visual Studio.

As my OS is Windows XP, the install program will give me a error page saying "To install this software, you must be running a 32-bit edition of Windows Server 2008 or a 32-bit edition of Windows Vista with Service Pack 1."

A search on this topic took me to here . I followed the steps in this post to be able to install the tools on XP.

1. Create an empty directory somewhere on your file system called Exported.

2. Navigate to the Tools folder under the install directory for the Windows Installer SDK.
In my case, this is under C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

3. Launch MsiDb.exe

4.) In the MsiTable - Select Database for Import/Export dialog, browse to and select the WindowsAzureSDK-x86.msi file.

5.) In the MsiTable - Select Folder containing Text Files dialog, browse to and select the Exported directory you created earlier.

6.) In the MsiTable - Database table import/export window, click the Export radio button.

7.) In the list, select LaunchCondition, and then click OK.

8.) Click the Quit button.

8.) Open "...\Exported\LaunchCondition.idt" in Notepad

9.) Remove the following lines in this file:
(VersionNT = 600 AND ServicePackLevel >= 1) OR (VersionNT > 600) To install this software, you must be running a 32-bit edition of Windows Server 2008 or a 32-bit edition of Windows Vista with Service Pack 1.
IISVERSION AND ASPNET To use this software, you must enable Internet Information Services 7.0 with ASP.NET support. See the product release notes for details.

10.) Save the file.

11.) Navigate to the Tools folder under the install directory for the Windows Installer SDK

12.) Launch MsiDb.exe

13.) In the MsiTable - Select Database for Import/Export dialog, browse to and select the WindowsAzureSDK-x86.msi file.

14.) In the MsiTable - Select Folder containing Text Files dialog, browse to and select the Exported directory you created earlier.

15.) In the MsiTable - Database table import/export window, click the Import radio button.

16.) In the list, select LaunchCondition.idt, and then click OK.

17.) Click the Quit button.

18.) Launch the WindowsAzureSDK-x86.msi file, and enjoy.

Thursday, May 13, 2010

How to remove ANTI MALWARE DOCTOR

  • 重启到安全模式, 删除对应的文件和注册项
  • \enemies-names.txt
    \Antimalware Doctor.exe

    HKEY_CURRENT_USER\Software\Antimalware Doctor Inc\Antimalware Doctor
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\Antimalware Doctor
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run "Antimalware Doctor.exe"

  • 注意病毒文件是隐藏的, 要在"Folder Options"里显示隐藏文件才能看到病毒文件
  • 如果"Folder Options"被禁止, 使用如下注册表恢复:

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
    "NoFolderOptions"=dword:0000000
    [HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Restrictions]
    "NoBrowserOptions"=dword:00000000
  • 如果注册表工具已经被禁止
  • 把以下内容存成vbs后缀的文件, 并运行它:

    'Enable/Disable Registry Editing tools
    '?Doug Knox - rev 12/06/99

    Option Explicit

    'Declare variables
    Dim WSHShell, n, MyBox, p, t, mustboot, errnum, vers
    Dim enab, disab, jobfunc, itemtype

    Set WSHShell = WScript.CreateObject("WScript.Shell")
    p = "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System\"
    p = p & "DisableRegistryTools"
    itemtype = "REG_DWORD"
    mustboot = "Log off and back on, or restart your pc to" & vbCR & "effect the changes"
    enab = "ENABLED"
    disab = "DISABLED"
    jobfunc = "Registry Editing Tools are now "

    'This section tries to read the registry key value. If not present an
    'error is generated. Normal error return should be 0 if value is
    'present
    t = "Confirmation"
    Err.Clear
    On Error Resume Next
    n = WSHShell.RegRead (p)
    On Error Goto 0
    errnum = Err.Number

    if errnum <> 0 then
    'Create the registry key value for DisableRegistryTools with value 0
    WSHShell.RegWrite p, 0, itemtype
    End If

    'If the key is present, or was created, it is toggled
    'Confirmations can be disabled by commenting out
    'the two MyBox lines below

    If n = 0 Then
    n = 1
    WSHShell.RegWrite p, n, itemtype
    Mybox = MsgBox(jobfunc & disab & vbCR & mustboot, 4096, t)
    ElseIf n = 1 then
    n = 0
    WSHShell.RegWrite p, n, itemtype
    Mybox = MsgBox(jobfunc & enab & vbCR & mustboot, 4096, t)
    End If