Thursday, December 22, 2011

Redirection in URL Routing

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.

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

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

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.

CustomerDetailsPage

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

Shout it
kick it on DotNetKicks.com
Share:

3 comments:

  1. Look this site, This is Proffessional website for the Website Development Co.
    http://webzinfoway.com/

    ReplyDelete
  2. Javascript is not working with Response.RedirectToRoute();
    When I redirect with Response.Redirect() my javascript code works.

    Any suggestion for me please.

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews