Friday, July 8, 2011

ASP.NET Routing

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

Let's take an example to understand how it works.
I have created a demo project with two aspx screens. Project structure is like:


Step 1: Add following code in web.config to enable routing in your project:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web.Routing, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"
/>
</modules>
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*"
path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
/>

</handlers>
</system.webServer>
</configuration&gt;

Step 2: Create a function in Global.asax which registers all the route mappings:

Using System.Web.Routing;
private static void RegisterRoutes()
{
//Maps Screen1.aspx to FirstScreen in URL.
RouteTable.Routes.MapPageRoute("MainScreen", "FirstScreen",
"~/Screen1.aspx");

//Maps Screen2.aspx to SecondScreen/{ScreenName} in URL.
RouteTable.Routes.MapPageRoute("SecondaryScreen", "SecondScreen/{ScreenName}",
"~/Screen2.aspx");
}

MapPageRoute function provides a way to define routes for web forms.
It takes 3 parameters:
  1. RouteName - Unique Name for each Route.
  2. RouteURL - New URL, which will be displayed in the browser.
  3. PhysicalFile - aspx page URL.

Step 3: Call this function in Application_Start() in Global.asax:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}

Step 4: Add 1 textbox and button on Screen1.aspx:
<h2>
Screen Name to be displayed in URL
</h2>
<asp:TextBox runat="server" ID="txtScreenName" />

<asp:Button ID="Button1" runat="server" Text="Next Screen"
OnClick=
"Button1_Click" />



Also, add following code on Screen1.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("SecondScreen/" + Server.UrlEncode(txtScreenName.Text.Trim()));
}


The URLEncode method applies URL encoding rules, including escape characters, to a specified string.
URLEncode converts characters as follows:
  • Spaces ( ) are converted to plus signs (+).
  • Non-alphanumeric characters are escaped to their hexadecimal representation.
This will redirect the screen to Screen2.aspx and the URL displayed in the browser will be somewhat like :
Something/SecondScreen/ValueEnteredInTextBox


This URL was getting created on the basis of value entered in the textbox at runtime. Suppose we want to hardcode a url, in that case we will simply write:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/FirstScreen");
}

We have already defined the mapping for FirstScreen in above mentioned Global.asax file.
Read More..