URL Rewriting-asp.net url rewriting has lots of
benefits, listing its main benefits
1. SEO Friendly URL
2. Secured URL
3. No need to change bookmark with change in site
structure.
URL Rewriting Scenario
Here i am trying to
describe the scenario where we use url rewriting.suppose we have a page called
"DisplayProducts.aspx" that takes a category name as a querystring
argument, and filters the products according that querystring value. The corresponding URLs to this
DisplayProducts.aspx page look like
this:
http://usetricks.com/DisplayProducts.aspx?CID=books
http://usetricks.com/DisplayProducts.aspx?CID=bikes
http://usetricks.com/DisplayProducts.aspx?CID=pens
Rather than use a
querystring to expose each category, we want to modify the application so that
each product category looks like a unique URL to a search engine, and has the
category keyword embedded in the actual URL (and not as a querystring argument).
URL Rewriting Using Request.PathInfo Parameters
Instead of QueryStrings
To understand this see the
diffrence between below two urls.
http://usetricks.com/DisplayProducts.aspx?CID=books
and
http://usetricks.com/DisplayProducts.aspx/books
One thing you'll notice
with the above URLs is that they no longer have Querystring values - instead
the category parameter value is appended on to the URL as a trailing /param
value after the DisplayProducts.aspx page handler name.Simply use the
Request.PathInfo property, which will return the content immediately following
the DisplayProducts.aspx portion of the
URL.
protected string Getcategory()
{
string
CATNAME = "";
if
(Request.PathInfo.Length == 0)
{
CATNAME= "";
}
else
{
CATNAME=
Request.PathInfo.Substring(1);
}
return
CATNAME;
}
In this technique there is no server
configuration changes are required in order to deploy an ASP.NET application.
Using HttpContext.RewritePath() to Perform URL
Rewriting
This method allows a
developer to dynamically rewrite the processing path of an incoming URL, and
for ASP.NET to then continue executing the request using the newly re-written
path.
For example, we could
choose to expose the following URLs to the public:
http://usetricks.com/books.aspx
http://usetricks.com/pens.aspx
http://usetricks.com/bikes.aspx
This looks to the outside
world like there are three separate pages on the site (and will look great to a
search crawler).Now we have to use Application_BeginRequest event in
Global.asax.
void
Application_BeginRequest(object sender, EventArgs e)
{
string
fullOrigionalpath = Request.Url.ToString();
if
(fullOrigionalpath.Contains("/Books.aspx"))
{
Context.RewritePath("/DisplayProducts.aspx?CID=books");
}
else if (fullOrigionalpath.Contains("/pens.aspx"))
{
Context.RewritePath("/DisplayProducts.aspx?CID=pens");
}
}
The c# regex.match method is typically used to validate a string or to ensure that a string conforms to a particular pattern without retrieving that string for subsequent manipulation.Below is a string extension method that uses c# regex matches to check if the string is alphanumeric.It will return true if given string contain only number and alphabets.
using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Text.RegularExpressions;
public static class CheckString
{
public static bool
IsAlphanumeric(string source)
{
Regex
pattern = new Regex("[^0-9a-zA-Z]");
return
!pattern.IsMatch(source);
}
}
// THE Below
Example USe Code.
class Program
{
static void Main(string[]
args)
{
string
testString = Console.ReadLine();
if
(CheckString.IsAlphanumeric(testString))
Console.WriteLine("Yes string is Alphanumeric!");
else
Console.WriteLine("No string is not Alphanumeric!");
Console.ReadKey();
}
}
I have implemenetd a common method to clear the Text of few Controls. One of that is Textbox.If sometimes you requires to clear all the input fields of a web page then just call the below c# method this method will clear all textbox values to empty.
public void ClearAllTextBOX(ControlCollection
ctrls)
{
foreach
(Control ctrl in
ctrls)
{
if
(ctrl is TextBox)
((TextBox)ctrl).Text
= string.Empty;
ClearInputs(ctrl.Controls);
}
}
}
Often we need to capitalize the first letters of some word
or some text (for example when we want to display users name or city name etc).
Since string class does not have a method to do this we
could think that there is no built-in solution in C# for this problem.
Here i am giving two solution for this problem.
Solution 1-
We can use
ToTitleCase method of TextInfo class in System.Globalization
namespace for this problem.
public static string
Capitalize(string value)
{
return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
}
Solution 2-
The below method will return Capitalize Words.
public static string
CapitalizeWords(string value)
{
if
(value == null)
throw
new ArgumentNullException("value");
if
(value.Length == 0)
return
value;
StringBuilder
result = new StringBuilder(value);
result[0] = char.ToUpper(result[0]);
for (int i = 1; i < result.Length; ++i)
{
if
(char.IsWhiteSpace(result[i - 1]))
result[i] = char.ToUpper(result[i]);
}
return
result.ToString();
}
When we thinking how to solve this is problem JavaScript comes in our mind And off course,
that IS THE WAY TO GO.
ASP.NET 2.0 introduced DefaultFocus and DefaultButton properties for HtmlForm class that
you can easily use for requirements like this.
DefaultFocus property gets
or sets the child control on the HtmlForm that will receive the focus when the
HtmlForm is loaded.
DefaultButton property gets or sets the child control of the HtmlForm that causes postback when enter key is pressed on the page.
Here is an example on how to use this two
properties in your pages:
<form id="formtest" runat="server" defaultfocus="txtfirstname"
defaultbutton="btnsubmit ">
defaultbutton="btnsubmit ">
<div>
Name:
<asp:TextBox ID="txtfirstname btnsubmit" runat="server"></asp:TextBox><br />
Address:
<asp:TextBox ID="txtadress" runat="server"></asp:TextBox><br />
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
<asp:Button ID="btnsubmit" runat="server" Text="Cancel" />
</div>
</form>
NOte: In order for this to work, your form must have runat="server" attribute set.