Monday, December 13, 2010

Write your own .Net RSS feed in c#

a very good post to create your very own rss feed in CSharp
 

Sunday, December 12, 2010

asp.net ashx handler

Talking about URL Rewriting, Generic Handlers, Http response the images, and using images in email read notices. all can be addressed in this wonderful article that I found on dot net perls by sam allen.

 

ASP.NET ASHX Handler Tutorial

You want to create an ASP.NET file that is not a typical web forms page. Your file will need to dynamically return an image from a query string, or XML and other non-HTML web pages. Here is a simple tutorial for using an ASHX generic handler in ASP.NET, with code written in the C# programming language.

 

Using ASHX handlers

First we review the goal in using ASHX files in the ASP.NET web development framework. What we will be able to do is use the ASHX file in a URL, and have it return content dynamically. We will use the query string, and the final URLs will look like this:

http://dotnetperls.com/?file=name

Getting started. This part lists the steps you can take to add a new ASHX file. To do this, open your ASP.NET web site. Go to the Website menu and click on the very first menu item there, "Add New Item...". This will present the Add New Item dialog box. Select the "Generic Handler" item, and you will get a new file with some code in it called Handler.ashx.

Autogenerated code

Here we note what the autogenerated code in the ASHX file does. It defines two parts of the IHttpHandler interface. The important part is ProcessRequest(), which will be invoked whenever the Handler.ashx file is requested or pointed to. You shouldn't modify the interface inheritance or remove either of the members.

Mapping handler

It is often desirable to map an older URL or path to your new ASHX file. For backwards compatibility and for search engine optimization, you will probably want the new handler to take over an old URL in your site. To do this, use urlMappings; alternatively, you can use more complex RewritePath methods.

--- Part of Web.config file (XML) ---  <system.web>     <urlMappings enabled="true">         <add url="~/Default.aspx" mappedUrl="~/Handler.ashx"/>     </urlMappings>     ...

URL mappings. The above Web.config markup will automatically link one URL to another. Now, when the Default.aspx page is requested, your Handler.ashx file will take over. This means you can map the default page in a directory to your handler.

See urlMappings for Redirects.

Adding example image

Here we mention what you can do with the ASHX file involving images. Find your favorite image on your disk or on the Internet and add it to your website project. For my example, the image I chose was "Flower1.png". Next we will use this image in the Handler.ashx file.

Modifying Handler.ashx

Your handler has two parts, and here we must modify the ProcessRequest() method. We can change the ContentType of the file and the Response content. Modify your Handler.ashx to be similar to the following, with your image ContentType and file name.

~~~ ASHX code-behind file (C#) ~~~  <%@ WebHandler Language="C#" Class="Handler" %>  using System; using System.Web;  public class Handler : IHttpHandler {      public void ProcessRequest (HttpContext context) {         // Comment out these lines first:         // context.Response.ContentType = "text/plain";         // context.Response.Write("Hello World");          context.Response.ContentType = "image/png";         context.Response.WriteFile("~/Flower1.png");     }      public bool IsReusable {         get {             return false;         }     } }

Testing handler

Here we test the new configuration and ASHX file on the local machine. Now click the green arrow to run your website on the development server. You should see the image in your browser. This is the result of writing the image to the response in the handler.

 

Adding functionality

The example here so far is relatively useless. All it does is allow us to pipe an image through an ASHX handler. Note that you can add any logging code or referrer logic to the handler in the C# language. Developers commonly need to use the QueryString collection on the Request. You can use the Request.QueryString in the Handler just like you would on any ASPX web form page.

=== ASHX modified code-behind (C#) ===  <%@ WebHandler Language="C#" Class="Handler" %>  using System; using System.Web;  public class Handler : IHttpHandler {      public void ProcessRequest (HttpContext context) {          HttpResponse r = context.Response;         r.ContentType = "image/png";         //         // Write the requested image         //         string file = context.Request.QueryString["file"];         if (file == "logo")         {             r.WriteFile("Logo1.png");         }         else         {             r.WriteFile("Flower1.png");         }     }      public bool IsReusable {         get {             return false;         }     } }

What this does. The above code receives requests and then returns a different file based on the QueryString collection value. It will return one of two images from the two query strings. The strings it returns are shown next here.

              URL: http://dotnetperls.com/?file=logo File query string: logo      File written: Logo1.png                URL: http://dotnetperls.com/?file=flower File query string: flower      File written: Flower1.png

Testing query string

Does all this really work? Yes, it does, but it is always important to test it. Open your browser and on the path add query strings like shown in the above table. What happens is that ASP.NET internally maps the Default.aspx page to your Handler.ashx. Then, it receives the query string and write the appropriate file.

 

Uses

The code here could be used as a hit tracker that counts visitors and logs referrers. This could provide a more accurate visit count than server logs, because of browser and bot differences.

Handlers vs. web pages. ASP.NET web forms inherit from the Page class. This provides them with many events and a very detailed initialization and event model. You don't need that for dynamic images, XML, or binary files.

Performance

You are likely wondering if there is any performance advantage or change to using ASHX files. Here I mention Robbe Morris' benchmarking work, where he found that generic handlers are 5-10% faster than ASPX web forms that do the exact same operations—even without server controls. It is faster because it does a lot less. As you can imagine firing 10+ events each time a request is handled is a fair amount more expensive than only firing one event.

Choosing handlers

Here the author wants to propose some guidelines about when to use custom handlers and when to use ASPX web form pages. Handlers are better for binary data, and web forms are best for rapid development.

Use Web Forms: If you have simple HTML pages                ASP.NET custom controls                Simple dynamic pages  Use handlers: If you have binary files               Dynamic image views               Performance-critical web pages               XML files               Minimal web pages

Control trees

In the ASP.NET framework, web forms use a concept called control trees where the parts of web pages are stored in an object model. Use custom handlers when you do not require the custom control tree or the whole HTML web form framework. This will result in greater performance and much simpler code to debug.

IsReusable property

The author does not know precisely what the IsReusable property does in ASP.NET, as much of the supporting code is not in the ASHX file itself. His reading indicates that it can improve performance and decrease memory pressure by not destroying the handler repeatedly.

Summary

In this article, we saw how you can use ASHX custom handlers in your ASP.NET website. This could be modified to fill in many different important web site functions. Combine urlMappings with query strings on custom handlers to greatly simplify and streamline your back-end web site code.

Tuesday, December 7, 2010

String.Contains() method

// This example demonstrates the String.Contains() method using System;  class Sample  {     public static void Main()      {     string s1 = "The quick brown fox jumps over the lazy dog";     string s2 = "fox";     bool b;     b = s1.Contains(s2);     Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);     } } /* This example produces the following results:  Is the string, s2, in the string, s1?: True */  

C# Split String Examples

here is a good article I found on dotnetperl.com
 

You want to split strings on different characters with single character or string delimiters. For example, split a string that contains "\r\n" sequences, which are Windows newlines. Through these examples, we learn ways to use the Split method on the string type in the C# programming language.

Use the Split method to separate parts from a string.
If your input string is A,B,C --
Split on the comma to get an array of:
"A" "B" "C"

Using Split

To begin, we look at the basic Split method overload. You already know the general way to do this, but it is good to see the basic syntax before we move on. This example splits on a single character.

=== Example program for splitting on spaces (C#) ===  using System;  class Program {     static void Main()     {         string s = "there is a cat";         //         // Split string on spaces.         // ... This will separate all the words.         //         string[] words = s.Split(' ');         foreach (string word in words)         {             Console.WriteLine(word);         }     } }  === Output of the program ===  there is a cat

Description. The input string, which contains four words, is split on spaces and the foreach loop then displays each word. The result value from Split is a string[] array.

Multiple characters

Here we use either the Regex method or the C# new array syntax. Note that a new char array is created in the following usages. There is an overloaded method with that signature if you need StringSplitOptions, which is used to remove empty strings.

=== Program that splits on lines with Regex (C#) ===  using System; using System.Text.RegularExpressions;  class Program {     static void Main()     {         string value = "cat\r\ndog\r\nanimal\r\nperson";         //         // Split the string on line breaks.         // ... The return value from Split is a string[] array.         //         string[] lines = Regex.Split(value, "\r\n");          foreach (string line in lines)         {             Console.WriteLine(line);         }     } }  === Output of the program ===  cat dog animal person

StringSplitOptions

While the Regex type methods can be used to Split strings effectively, the string type Split method is faster in many cases. The Regex Split method is static; the string Split method is instance-based. The next example shows how you can specify an array as the first parameter to string Split.

=== Program that splits on multiple characters (C#) ===  using System;  class Program {     static void Main()     {         //         // This string is also separated by Windows line breaks.         //         string value = "shirt\r\ndress\r\npants\r\njacket";          //         // Use a new char[] array of two characters (\r and \n) to break         // lines from into separate strings. Use "RemoveEmptyEntries"         // to make sure no empty strings get put in the string[] array.         //         char[] delimiters = new char[] { '\r', '\n' };         string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);         for (int i = 0; i < parts.Length; i++)         {             Console.WriteLine(parts[i]);         }          //         // Same as the previous example, but uses a new string of 2 characters.         //         parts = value.Split(new string[] { "\r\n" }, StringSplitOptions.None);         for (int i = 0; i < parts.Length; i++)         {             Console.WriteLine(parts[i]);         }     } }  === Output of the program ===     (Repeated two times)  shirt dress pants jacket

Overview. One useful overload of Split receives char[] arrays. The string Split method can receive a character array as the first parameter. Each char in the array designates a new block.

Using string arrays. Another overload of Split receives string[] arrays. This means string array can also be passed to the Split method. The new string[] array is created inline with the Split call.

Explanation of StringSplitOptions. The RemoveEmptyEntries enum is specified. When two delimiters are adjacent, we end up with an empty result. We can use this as the second parameter to avoid this. The following screenshot shows the Visual Studio debugger.

See StringSplitOptions Enumeration.

 

Separating words

Here we see how you can separate words with Split. Usually, the best way to separate words is to use a Regex that specifies non-word chars. This example separates words in a string based on non-word characters. It eliminates punctuation and whitespace from the return array.

=== Program that separates on non-word pattern (C#) ===  using System; using System.Text.RegularExpressions;  class Program {     static void Main()     {         string[] w = SplitWords("That is a cute cat, man");         foreach (string s in w)         {             Console.WriteLine(s);         }         Console.ReadLine();     }      /// <summary>     /// Take all the words in the input string and separate them.     /// </summary>     static string[] SplitWords(string s)     {         //         // Split on all non-word characters.         // ... Returns an array of all the words.         //         return Regex.Split(s, @"\W+");         // @      special verbatim string syntax         // \W+    one or more non-word characters together     } }  === Output of the program ===  That is a cute cat man

Word splitting example. Here you can separate parts of your input string based on any character set or range with Regex. Overall, this provides more power than the string Split methods.

See Regex.Split Method Examples.

Splitting text files

Here you have a text file containing comma-delimited lines of values. This is called a CSV file, and it is easily dealt with in the C# language. We use the File.ReadAllLines method here, but you may want StreamReader instead. This code reads in both of those lines, parses them, and displays the values of each line after the line number. The final comment shows how the file was parsed into the strings.

=== Contents of input file (TextFile1.txt) ===  Dog,Cat,Mouse,Fish,Cow,Horse,Hyena Programmer,Wizard,CEO,Rancher,Clerk,Farmer  === Program that splits lines in file (C#) ===  using System; using System.IO;  class Program {     static void Main()     {         int i = 0;         foreach (string line in File.ReadAllLines("TextFile1.txt"))         {             string[] parts = line.Split(',');             foreach (string part in parts)             {                 Console.WriteLine("{0}:{1}",                     i,                     part);             }             i++; // For demo only         }     } }  === Output of the program ===  0:Dog 0:Cat 0:Mouse 0:Fish 0:Cow 0:Horse 0:Hyena 1:Programmer 1:Wizard 1:CEO 1:Rancher 1:Clerk 1:Farmer

Splitting directory paths

Here we see how you can Split the segments in a Windows local directory into separate strings. Note that directory paths are complex and this may not handle all cases correctly. It is also platform-specific, and you could use System.IO.Path. DirectorySeparatorChar for more flexibility.

See Path Examples.

=== Program that splits Windows directories (C#) ===  using System;  class Program {     static void Main()     {         // The directory from Windows         const string dir = @"C:\Users\Sam\Documents\Perls\Main";         // Split on directory separator         string[] parts = dir.Split('\\');         foreach (string part in parts)         {             Console.WriteLine(part);         }     } }  === Output of the program ===  C: Users Sam Documents Perls Main

Internal logic

The logic internal to the .NET framework for Split is implemented in managed code. The methods call into the overload with three parameters. The parameters are next checked for validity. Finally, it uses unsafe code to create the separator list, and then a for loop combined with Substring to return the array.

Benchmarks

I tested a long string and a short string, having 40 and 1200 chars. String splitting speed varies on the type of strings. The length of the blocks, number of delimiters, and total size of the string factor into performance. The Regex.Split option generally performed the worst. I felt that the second or third methods would be the best, after observing performance problems with regular expressions in other situations.

=== Strings used in test (C#) === // // Build long string. // _test = string.Empty; for (int i = 0; i < 120; i++) {     _test += "01234567\r\n"; } // // Build short string. // _test = string.Empty; for (int i = 0; i < 10; i++) {     _test += "ab\r\n"; }  === Example methods tested (100000 iterations) ===  static void Test1() {     string[] arr = Regex.Split(_test, "\r\n", RegexOptions.Compiled); }  static void Test2() {     string[] arr = _test.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); }  static void Test3() {     string[] arr = _test.Split(new string[] { "\r\n" }, StringSplitOptions.None); }

Longer strings: 1200 chars. The benchmark for the methods on the long strings is more even. It may be that for very long strings, such as entire files, the Regex method is equivalent or even faster. For short strings, Regex is slowest, but for long strings it is very fast.

=== Benchmark of Split on long strings ===  [1] Regex.Split:    3470 ms [2] char[] Split:   1255 ms [fastest] [3] string[] Split: 1449 ms  === Benchmark of Split on short strings ===  [1] Regex.Split:     434 ms [2] char[] Split:     63 ms [fastest] [3] string[] Split:   83 ms

Short strings: 40 chars. This shows the three methods compared to each other on short strings. Method 1 is the Regex method, and it is by far the slowest on the short strings. This may be because of the compilation time. Smaller is better. This article was last updated for .NET 3.5 SP1.

Performance recommendation. For programs that use shorter strings, the methods that split based on arrays are faster and simpler, and they will avoid Regex compilation. For somewhat longer strings or files that contain more lines, Regex is appropriate. Also, I show some Split improvements that can improve your program.

See Split String Improvement.

Escaped characters

Here we note that you can use Replace on your string input to substitute special characters in for any escaped characters. This can solve lots of problems on parsing computer-generated code or data.

See Split Method and Escape Characters.

Delimiter arrays

In this section, we focus on how you can specify delimiters to the Split method in the C# language. My further research into Split and its performance shows that it is worthwhile to declare your char[] array you are splitting on as a local instance to reduce memory pressure and improve runtime performance. There is another example of delimiter array allocation on this site.

See Split Delimiter Use.

=== Slow version, before (C#) ===  // // Split on multiple characters using new char[] inline. // string t = "string to split, ok";  for (int i = 0; i < 10000000; i++) {     string[] s = t.Split(new char[] { ' ', ',' }); }  === Fast version, after (C#) ===  // // Split on multiple characters using new char[] already created. // string t = "string to split, ok"; char[] c = new char[]{ ' ', ',' }; // <-- Cache this  for (int i = 0; i < 10000000; i++) {     string[] s = t.Split(c); }

Interpretation. We see that storing the array of delimiters separately is good. My measurements show the above code is less than 10% faster when the array is stored outside the loop.

Explode

In this part, we discuss the explode function from the PHP environment. The .NET Framework has no explode method exactly like PHP explode, but you can gain the functionality quite easily with Split, for the most part. You can replace explode with the Split method that receives a string[] array. Explode allows you to split strings based on a fixed size. The new article on this topic implements the logic in the C# language directly.

See Explode String Extension Method.

Summary

In this tutorial, we saw several examples and two benchmarks of the Split method in the C# programming language. You can use Split to divide or separate your strings while keeping your code as simple as possible. Sometimes, using IndexOf and Substring together to parse your strings can be more precise and less error-prone.

Sunday, December 5, 2010

3CMS - ContactUs page (.aspx and codebehind)

Front End contact From Panels
 
following is the contact us page code. you can also ajaxify by configuring the ajax manager.
 
Code :
 

<asp:Panel ID="pnlContactForm" runat="server">

<p>

<strong>Please fill in the following form to contact us</strong></p>

<ol>

<li>

<label for="name">

Your Name

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Fill in Your Name!"

ControlToValidate="rtbName" ValidationGroup="EmailFormValidate" Style="color: #FF0000">*</asp:RequiredFieldValidator><br />

</label>

<telerik:RadTextBox ID="rtbName" runat="server" EmptyMessage="Please Enter Your Name . . ."

Width="200px">

</telerik:RadTextBox>

</li>

<li>

<label for="email">

Email

<asp:RegularExpressionValidator ID="RegularExpressionValidator1"

runat="server" ErrorMessage="Please Fill in a valid Email Address!"

ControlToValidate="rtbEmail" ValidationGroup="EmailFormValidate"

Style="color: #FF0000"

ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter Your Email Address!"

ControlToValidate="rtbEmail" ValidationGroup="EmailFormValidate" Style="color: #FF0000">*</asp:RequiredFieldValidator>

<br />

</label>

<telerik:RadTextBox ID="rtbEmail" runat="server" EmptyMessage="Enter You Email Address . . ."

Width="200px">

</telerik:RadTextBox>&nbsp;</li>

<li>

<label for="subject">

Subject<br />

</label>

<telerik:RadTextBox ID="rtbSubject" runat="server" EmptyMessage="What Your Comments are About?"

Width="200px">

</telerik:RadTextBox>

</li>

<li>

<label for="message">

Your comments

<%--<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Please Enter Minimum of 5 and Maximum of 500 characters !"

MinimumValue="5" ControlToValidate="rtbComments" ValidationGroup="EmailFormValidate"

Style="color: #FF0000" MaximumValue="500">*</asp:RangeValidator>--%>

<label for="email">

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Enter Some Comments to Send."

ControlToValidate="rtbComments" ValidationGroup="EmailFormValidate"

Style="color: #FF0000" ForeColor="Red">*</asp:RequiredFieldValidator>

</label>

<br />

</label>

<telerik:RadTextBox ID="rtbComments" runat="server" TextMode="MultiLine" Rows="5"

Width="350" EmptyMessage="Please Enter you comments here . . .">

</telerik:RadTextBox>

</li>

<li>

<asp:CheckBox ID="CheckBox1" runat="server" Text="Send copy of this message to me"

Checked="True" />

</li>

<li>

<telerik:RadCaptcha ID="RadCaptcha1" runat="server" ValidationGroup="EmailFormValidate"

ErrorMessage="Code does not match the Image. Please try again."

EnableRefreshImage="True" ForeColor="Red"

CaptchaLinkButtonText="Cannot Read Code? Refresh !"

CaptchaTextBoxLabel="Please Enter the code from the image">

</telerik:RadCaptcha>

</li>

<li class="buttons">

<asp:Button ID="contact" runat="server" Text="Submit" OnClick="contact_Click" ValidationGroup="EmailFormValidate" /><br /><br />

<span style="color: #FF0000">

<asp:ValidationSummary ID="ContactValidationSummary" runat="server"

ValidationGroup="EmailFormValidate" CssClass="ValidationSummary" /></span>

</li>

</ol>

</asp:Panel>

<asp:Panel ID="pnlReply" runat="server" Visible="False">

<p>

<strong>Thank you for your intrest and time in our website. We have recieved your comments and have been forwarded to the concerned person. Should it require any further details, a member of our staff may contact you.<br /><br /> We yet again Thank You for your time.<br /><br />Management.</strong></p>

</asp:Panel>

 

 

CodeBehind :

 

protected void Page_Load(object sender, EventArgs e)

{

Page.DataBind();

Globals.CurrentPageId = 6;

}

protected void contact_Click(object sender, EventArgs e)

{

//Add Comment to Database

ClientEntities db = new ClientEntities();

Comment comment = new Comment();

CommentRep commentRep = new CommentRep();

comment.ClientId = AppSession.Configs.ClientId;

comment.Name = rtbName.Text;

comment.Email = rtbEmail.Text;

comment.Subject = rtbSubject.Text;

comment.IsCopyRequested = CheckBox1.Checked;

comment.ClientAdminEmail = AppSession.Configs.ClientAdminEmail;

comment.CreatedBy = User.Identity.Name;

comment.DateCreated = DateTime.Now;

commentRep.Add(comment);

commentRep.Save();

// send email to the recepient

if (this.CheckBox1.Checked == true)

{

sendRecepientEmail();

}

//send email to the Site Adminisrator

sendClientEmail();

this.pnlContactForm.Visible = false;

this.pnlReply.Visible = true;

}

private void sendClientEmail()

{

MailMessage mail = new MailMessage();

mail.From = new MailAddress(AppSession.Configs.ClientAdminEmail, rtbName.Text + "(" + rtbEmail.Text + ")");

mail.To.Add(AppSession.Configs.ClientAdminEmail);

mail.Subject = "You have got new comments at " + AppSession.Configs.SiteTitle;

mail.IsBodyHtml = true;

mail.Body = "Visitor's Email : " + rtbEmail.Text + "<br /><br />";

mail.Body += "Comments left by " + rtbName.Text + " are as below : <br /><br />";

mail.Body += "Comments: <br />" + rtbComments.Text + "<br />";

SmtpClient smtp = new SmtpClient();

smtp.Send(mail);

}

private void sendRecepientEmail()

{

MailMessage mail = new MailMessage();

mail.From = new MailAddress(AppSession.Configs.ClientAdminEmail, AppSession.Configs.ClientName);

mail.To.Add(rtbEmail.Text);

mail.Subject = "Thank you for your comments at " + AppSession.Configs.SiteTitle;

mail.IsBodyHtml = true;

mail.Body = "Dear " + rtbName.Text + "<br /><br />";

mail.Body += "Thank you for your comments as below : <br /><br />";

mail.Body += "Comments: <br />" + rtbComments.Text + "<br />";

SmtpClient smtp = new SmtpClient();

smtp.Send(mail);

}