Over a million developers have joined DZone.

Redirection in URL Routing

DZone 's Guide to

Redirection in URL Routing

· ·
Free Resource
In one of the my earlier post I have written How easily we do URL rewriting in ASP.NET Web forms. In this post I am going to explain redirection in URL Routing.

In web application it’s a common scenario that we are redirecting our page to the one from the another and those who are familiar with the ASP.NET Web Forms will also know how we can use  Response.Redirect() to redirect from one page to another page.  In ASP.NET 4.0 Web Forms they have given same kind of Method to redirect to a particular route. The method is Response.RedirectToRoute(). With the help of this method you can redirect to any particular route. Response.RedirectToRoute() has different overload method you can find more information about it from following link.


Real Example of Response.RedirectToRoute

Iam going to use same example which I have used in my earlier post. We are going to add a new page called Index.aspx. After adding the page. I am going to add following code to global.asax to map index.aspx to launch by default.
using System;
using System.Web.Routing;

namespace UrlRewriting
public class Global : System.Web.HttpApplication

protected void Application_Start(object sender, EventArgs e)

public static void RegisterRoutes(RouteCollection routeCollection)
routeCollection.MapPageRoute("RouteForCustomer", "Customer/{Id}", "~/Customer.aspx");
routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/Index.aspx");

As you can see in above I have added a default route to map index.aspx. Following is HTML code for index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="UrlRewriting.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<h1>This is my home page</h1>
<asp:button ID="btnGoToCustomer" runat="server"  Text="Goto first Customer" 

As you can see in above code I have created a ASP.NET Button called for redirection and on click of that button I have written following code for that.
protected void btnGoToCustomer_Click(object sender, EventArgs e)
Response.RedirectToRoute("RouteForCustomer",new {Id=1});
As you can see in above code I have redirect it to ‘Customer' route and I am passing Id as “1” so it will load details of customer whose id is 1.
Let’s run this example via pressing F5. So it will directly load index.aspx page as homepage like following.

HomePageForURLRedirection- ASP.NET 4.0 URL Routing and revwriting

Now once you click on button It will redirect to custom page and will display customer information.


So that’s it. You can see it’s very easy redirect pages with URL routing. Hope you like it. Stay tuned for more.Till then Happy programming..

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}