IIS: Redirection from non-www to www domain

The problem today, is that we have a great asp.net website but search engines are indexing the http://greataspnetwebsite.com instead of http://www.greataspnetwebsite.com, this is commonly seen on the web and there are several ways to archive a good result for making the non-www to www domain. This redirection should be a 301 Permanently Moved, otherwise you will might lose your search engine indexed page or become duplicate content for your non-www and www domain. Here are easy steps how to archive a quick and clean Permanent Redirection using IIS.

Consider the case where we already have a website in IIS called: greataspnetwebsite.com

  • Go to IIS Manager
  • Create a new website that point to the same directory as your existing one
  • Select the newly created website, open the properties box
  • In the option button “When connecting to this resource the content should come from” should be change to “A redirection to a URL
  • Specify the URL http://www.greataspnetwebsite.com
  • Select the check box that says “A permanent redirection for this resource.”

Copy-Clone custom object in C#

This post relates how to Copy/Clone custom object in C# (Deep and Shallow Clone).

In this example I will be having a base class that all my custom object will be inheriting from.

Updates: After quiet some research on the subject i recently found some interesting articles on this subject and wanted to share them on this post.

Object Cloning Using IL in C#

This subject is inspired on a session I followed on the TechDays 2008,
that addressed the fact that IL (Intermediate Language) can be used to clone objects, among other things, and that it’s not evil at all, and it can be pretty performant also.

You only have to see that you don’t overuse it tho, because otherwise the readability of your code is reduced, which is not a good thing for the maintenance.
And wrong usage of reflection (what IL is, or at least uses) can also result in poor performance.

Read more:
http://whizzodev.blogspot.com/2008/03/object-cloning-using-il-in-c.html

C# Class For Making a Deep Copy Clone of an Arbitrary Object

Here is a C# class that can create a deep copy clone of an arbitrary object. The thing that’s special about it is that it should work for any class that extends it, so that you don’t need to re-write a custom clone() function for every child class (as it seems the C# framework creators would like). This does a deep copy so be careful about members that recursively include one another.

Another way of doing this would be to use serialization . . . I just personally thought the reflection package would be more elegant.

Read more:
http://www.thomashapp.com/node/106

C# Object Clone Wars

Cloning C# objects is one of those things that appears easy but is actually quite complicated with many “gotchas.” This article describes the most common ways to clone a C# object.

Shallow vs. Deep Cloning

There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well.

hallow vs Deep Cloning

shallow vs Deep Cloning

Read more:
http://www.csharp411.com/c-object-clone-wars/

Clone C# Object using Serialization and De-serialization (Deep Clone)

In this example i’ll be using Serialization and De-Serialization to make a deep clone of the Person Class.

You must import the following namespaces to start with.
[csharp]
using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
[/csharp]

Let’s create a base class Person that all our other classes with inherit from. It will contain the following methods:

  • ToXMLString() : Returns an XML version of the current object
  • Deserialize(XmlDocument xml, Type type) : De-serialize the current XMLDocument cast it to the current type
  • Serialize(object o) : Serialize the current object and outputs an XMLDocument
  • Clone(): Runs a Serialization and De-serialization and returns a clone of the current Object

[csharp]
[Serializable()]
public abstract class Person
{
///

/// To XML string.
///

///
public string ToXMLString()
{
XmlSerializer serializer = new XmlSerializer(this.GetType());
MemoryStream dataStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(dataStream, Encoding.UTF8);
serializer.Serialize(dataStream, this);
return writer.ToString();
}

///

/// Deserializes an xml document back into an object
///

/// The xml data to deserialize /// The type of the object being deserialized /// A deserialized object
public static object Deserialize(XmlDocument xml, Type type)
{
XmlSerializer s = new XmlSerializer(type);
string xmlString = xml.OuterXml.ToString();
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
MemoryStream ms = new MemoryStream(buffer);
XmlReader reader = new XmlTextReader(ms);
Exception caught = null;

try
{
object o = s.Deserialize(reader);
return o;
}

catch (Exception e)
{
caught = e;
}
finally
{
reader.Close();

if (caught != null)
throw caught;
}
return null;
}

///

/// Serializes an object into an Xml Document
///

/// The object to serialize /// An Xml Document consisting of said object’s data
public static XmlDocument Serialize(object o)
{
XmlSerializer s = new XmlSerializer(o.GetType());

MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms, new UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.IndentChar = ‘ ‘;
writer.Indentation = 5;
Exception caught = null;

try
{
s.Serialize(writer, o);
XmlDocument xml = new XmlDocument();
string xmlString = ASCIIEncoding.UTF8.GetString(ms.ToArray());
xml.LoadXml(xmlString);
return xml;
}
catch (Exception e)
{
caught = e;
}
finally
{
writer.Close();
ms.Close();

if (caught != null)
throw caught;
}
return null;
}

///

/// Creates a new object that is a copy of the current instance.
///

///
/// A new object that is a copy of this instance.
///

public object Clone()
{
return Deserialize(Serialize(this), this.GetType());
}
}
[/csharp]

Here are sample classes that herits from the abstract base class and clone operation are performed upon them.

[csharp]

public class Clerk: Person
{
private string position;

///

/// Gets or sets the position.
///

/// The position.
public string Position
{
get { return position; }
set { position = value; }
}
}

public class Developer: Person
{
private string skill;

///

/// Gets or sets the skill.
///

/// The skill.
public string Skill
{
get { return skill; }
set { skill= value; }
}
}

[/csharp]

Using clone method.

[csharp]

public void UpdateSkills()
{
IList clerkList = new ArrayList();
IList newClerkList = new ArrayList();

foreach (Clerk clerk in clerkList)
{
newClerkList.Add((Clerk)clerk.Clone());
}
}

[/csharp]

Fatal error: require_once() [function.require]: Failed opening required ‘Text/Diff/Renderer.php’

Was just trying to view one of my post that i modified once after being published and bumbed up with a page with these error messages:

Warning: ini_set() has been disabled for security reasons in /home/wacdesig/public_html/wp-includes/pluggable.php on line 1517

Warning: require_once(Text/Diff/Renderer.php) [function.require-once]: failed to open stream: No such file or directory in /home/wacdesig/public_html/wp-includes/Text/Diff/Renderer/inline.php on line 17

Fatal error: require_once() [function.require]: Failed opening required ‘Text/Diff/Renderer.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/wacdesig/public_html/wp-includes/Text/Diff/Renderer/inline.php on line 17

Apparently this is due to my hosting provider that have disabled the use the the PHP function ini_set(). Great !!! how do i edit post that i have edited before… fortunately i found out someone that came up with a solution :).

Please note that this issue is only for WP 2.6 and is cause by the new Revision module that allows you to view a revision of your post before it was edited.

Here is the link to the solution for this problem:http://www.code-styling.de/english/wordpress-26-and-the-textdiff-fatal-errors

I also found another solution for those who don’t want to download the modified WP source file that is proposed in the first solution

Edit the wp-includes/Text/Diff.php, line 52 :
[php]
require_once ABSPATH . WPINC . ‘/Text/Diff/Engine/’ . $engine . ‘.php’;
[/php]

Edit the wp-includes/Text/Diff/Renderer/inline.php, line 17 :
[php]
require_once ABSPATH . WPINC . ‘/Text/Diff/Renderer.php’;
[/php]