Friday 20 January 2012

IIS URL Rewrite Module Configuration Reference

What is URL Rewrite Module


The URL Rewrite Module rewrites request URLs to simple, user-friendly, and search-engine friendly addresses that are displayed to users or in Web applications. URL Rewrite uses defined rules to evaluate and then map the request URL to the address defined in the rule before it is processed by an IIS Web server. You can define URL rewriting logic that includes regular expressions and wildcards, and rules can be applied based on the request URL, HTTP headers, and server variables. While the primary purpose of the module is to rewrite request URLs to more friendly URLs, you can also use the module to define rules that perform redirects, send custom responses, or abort requests.

URL Rewrite Module Overview



A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful.

Rewrite rules consists of the following parts:


  • Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings.

  • Conditions – The optional conditions collection is used to specify additional logical operations to perform if a URL string matches the rule pattern. Within the conditions, you can check for certain values of HTTP headers or server variables, or verify if the requested URL corresponds to a file or directory on a physical file system.

  • Action – The action is used to specify what to do if the URL string matches the rule pattern and all the rule conditions are met.



Click here to read the full reference from learn.iis.net.

Using server variables in rewrite rules



Server variables provide additional information about current HTTP requests. You can sue this information to make rewriting decisions or to compose the rewritten URL.

Using back-references in rewrite rules



Parts of rules or conditions inputs can be captures in back-references. These can be then used to construct substitution URLs within rules actions or to construct input strings for rule conditions.

Back-references are generated in different ways, depending on which kind of pattern syntax is used for the rule. When an ECMAScript pattern syntax is used, a back-reference can be created by putting parenthesis around the part of the pattern that must capture the back-reference. For example, the pattern ([0-9]+)/([a-z]+)\.html will capture 07 and article in back-references from this requested URL: 07/article.html.

Using String functions in rewrite rules



There are three string functions available for changing the values within a rewrite rule action, as well as any conditions:

ToLower - returns the input string converted to lower case.
UrlEncode - returns the input string converted to URL-encoded format. This function can be used if the substitution URL in rewrite rule contains special characters (for example non-ASCII or URI-unsafe characters).
UrlDecode - decodes the URL-encoded input string. This function can be used to decode a condition input before matching it against a pattern.

The functions can be invoked by using the following syntax:
{function_name:any_string}

Where "function_name" can be on eof the following: "ToLower", "UrlEncode", "UrlDecode". "Any_string" can be either a literal string or a string built by using server variables or back-references. For example, the following are valid invocations of string functions:
{ToLower:DEFAULT.HTM}
{UrlDecode:{REQUEST_URI}}
{UrlEncode:{R:1}.aspx?p=[résumé]}

Rewrite maps



A rewrite map is an arbitrary collection of name-value pairs that can be used within rewrite rules to generate the substitution URL during rewriting. Rewrite maps are particularly useful when you have a large set of rewrite rules and all of these rules use static strings (that is, when there is no pattern matching used).

Click here to read the full reference from learn.iis.net.

Is HTML case sensitive?

HTML tags are not case sensitive: <P> means the same as <p>. Many web sites use uppercase HTML tags.

World Wide Web Consortium (W3C) recommends lowercase in HTML 4, and demands lowercase tags in XHTML.

Source

Monday 9 January 2012

Basics of anonymous Methods in C# 2.0

Anonymous method is a new language feature in C# 2.0.

Anonymous methods allow us to define a code block where a delegate object is acceptable. This facility saves us an extra step of creating a delegate for small code blocks that we want to pass to a delegate. It also removes the cluttering of small methods in the class code.

For example, that let us say we have a string collection class named MyCollection. This class has a method that retrieves all the items in the collection that satisfies a user supplied criteria, i.e., the caller decides whether a particular item in the collection qualifies for being retrieved as part of the return array from this method.


public class MyCollection
{
public delegate bool SelectItem(string sItem);

public string[] GetFilteredItemArray(SelectItem itemFilter)
{
List sList = new List();
foreach(string sItem in m_sList)
{
if (itemFilter(sItem) == true) sList.Add(sItem);
}
return sList.ToArray();
}

public List ItemList
{
get
{
return m_sList;
}
}

private List m_sList = new List();
}


We can write code as shown below to use the above defined class:


protected void Page_Load(object sender, EventArgs e)
{
MyCollection objMyCol = new MyCollection();
objMyCol.ItemList.Add("Aditya");
objMyCol.ItemList.Add("Tanu");
objMyCol.ItemList.Add("Manoj");
objMyCol.ItemList.Add("Ahan");
objMyCol.ItemList.Add("Hasi");

// get an array of string items in the collection that start

// with letter 'A'

//

string[] AStrings = objMyCol.GetFilteredItemArray(FilterStringWithA);
Console.WriteLine("----- Strings starting with letter 'A' -----");
foreach (string s in AStrings)
{
Console.WriteLine(s);
}

// get an array of string items in the collection that start

// with letter 'T'

//

string[] TStrings = objMyCol.GetFilteredItemArray(FilterStringWithT);
Console.WriteLine("----- Strings starting with letter 'T' -----");
foreach (string s in TStrings)
{
Console.WriteLine(s);
}
}

public static bool FilterStringWithA(string sItem)
{
if (sItem[0] == 'A')
return true;
else
return false;
}

public static bool FilterStringWithT(string sItem)
{
if (sItem[0] == 'T')
return true;
else
return false;
}


Here the problem is for each simple criteria we want to provide, we should define a method (static or instance). This soon clutters the class code. With anonymous methods, the code becomes much natural. Below is the re-written code using anonymous methods.


protected void Page_Load(object sender, EventArgs e)
{
MyCollection objMyCol = new MyCollection();
objMyCol.ItemList.Add("Apple");
objMyCol.ItemList.Add("Grape");
objMyCol.ItemList.Add("Orange");
objMyCol.ItemList.Add("Blackberry");
objMyCol.ItemList.Add("Blueberry");

// get an array of string items in the collection that start

// with letter 'A'

//

string[] AStrings = objMyCol.GetFilteredItemArray(delegate(string sItem)
{
if (sItem[0] == 'A')
return true;
else
return false;
});
System.Diagnostics.Debug.WriteLine("----- Strings starting with letter 'A' -----");
foreach (string s in AStrings)
{
System.Diagnostics.Debug.WriteLine(s);
}

// get an array of string items in the collection that start

// with letter 'T'

//

string[] TStrings = objMyCol.GetFilteredItemArray(delegate(string sItem)
{
if (sItem[0] == 'B')
return true;
else
return false;
});
Console.WriteLine("----- Strings starting with letter 'B' -----");
foreach (string s in TStrings)
{
System.Diagnostics.Debug.WriteLine(s);
}
}


As shown in the above sample, we were able to define the criteria with in-line code blocks instead of defining a new method to represent each criteria. Truly speaking, using such inline code may look natural and avoid defining new methods, but if this technique is used for larger inline code blocks, then the code soon becomes unmanageable and may lead to duplications. So, be selective in using methods vs. inline anonymous methods as delegates/event handlers.

Now, this is the basics of anonymous methods. The rest of the article deals with how anonymous methods work internally in different scenarios. Understanding how anonymous methods are implemented and work internally is important for their correct usage. Else, the results of your code using anonymous methods will look unpredictable.

Anonymous methods always start with a delegate keyword, followed by parameters to be used inside the method and the method body itself. As seen from the above code sample, users need not specify the return type of the anonymous method. It is inferred from the return statement within the method body. The .NET CLR cannot execute free flowing code blocks like anonymous methods. CLR requires that every method it executes is part of a type and should be a static method or instance method. So when you write anonymous methods in a class code and compile the code, the C# compiler silently creates a static or instance method within the same class where you defined the anonymous method. So anonymous methods are just a convenient syntax for defining your own methods inside the class to pass to delegates (delegate handlers/event handlers).

When you compile the above sample, the C# compiler creates two private static methods inside the class, where we defined the anonymous methods. It then replaces the anonymous methods with the address of those static methods. How the compiler decides to create static methods or instance methods depends on the usage of the static or instance data members of the class within which the anonymous methods are defined.

Click here to read more about the internal of anonymous methods.

Where does Console.WriteLine go in ASP.NET?

If you wonder where you can see the output from a Console.WriteLine() statement in an ASPX page here is the answer - you can't see it anywhere, because it is not outputted anywhere.

The reaseon is:

if you look at the Console class in Reflector, you'll find that if a process doesn't have an associated console, Console.Out and Console.Error are backed by Stream.Null (wrapped inside a TextWriter), which is a dummy implementation of Stream that basically ignores all input, and gives no output.

The alternative to Console.WriteLine is System.Diagnostics.Debug.WriteLine()



If you use System.Diagnostics.Debug.WriteLine() instead of Console.WriteLine(), then you can see the results in the Output window of Visual Studio.

Friday 6 January 2012

ASP.NET IsDebuggingEnabled Property

HttpContext.Current.IsDebuggingEnabled:

- It is a property of HttpContext object that identifies whether debug property of <compilation> element in web.config file is set to true or not.

How to remove a property from a JavaScript object

JavaScript's 'delete' operator can be used for this.

The delete operator deletes a property of an object. It is part of JavaScript 1.2.

Syntax



delete expression

where expression should evaluate to a property reference, e.g.:

delete variableName

delete objectExpression.property

delete objectExpression["property"]

delete objectExpression[index]

Parameters



objectName
The name of an object.

property
The property to delete.

index
An integer representing the array index to delete.

Returns



Returns false only if the property exists and cannot be deleted. It returns true in all other cases.

Example




var myJSONObject = {};
myJSONObject.id = 101;

delete myJSONObject.id;
//OR
delete myJSONObject['id'];