User Id :    Password :      New Member   Forgot Password  
 
Redirection in URL Routing
Description 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.   No. of Views     2518
  Rating     0
Author Jalpesh Vadgama   Posted On     17 Mar 2012
Tags ASP.NET,C#,Url Routing,ASP.NET : How To,C# : How To,ASP.NET 4.0    

Sample Code   Download Code

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.

 

msdn.microsoft.com/en-us/library/dd992853.aspx

 

Real Example of Response.RedirectToRoute



I am 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)
{
RegisterRoutes(RouteTable.Routes);
}
 
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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>This is my home page</h1>
<asp:button ID="btnGoToCustomer" runat="server"  Text="Goto first Customer"
onclick="btnGoToCustomer_Click"/>
</div>
</form>
</body>
</html>

 

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.

 

 

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..

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code