Vivekh
Vivekh

Reputation: 4259

urlrewriting in asp.net button click

Hi all I have seen many articles on url rewriting but I didn't find any as per my requirement. Assume I have two pages Default.aspx and Default1.aspx.. On initial load I would like to re write my Default.aspx to some thing like urlrewrite\dummy.aspx and on my Default.aspx I will have a button when I click on this I am going to redirect to Default1.aspx I would like to rewrite this to urlrewrite\dummy1.aspx

I just post the sample rewrites but if there is any better way of redirecting can you please help me..

Also what is the best way to rewrite all pages if I have some 20-50 pages

my global.asax file

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Routing" %>
<script RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(System.Web.Routing.RouteTable.Routes);
    }
    public static void RegisterRoutes(RouteCollection routeCollection)
    {
        string root = Server.MapPath("~");
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(root);
        System.IO.FileInfo[] files = info.GetFiles("*.aspx", System.IO.SearchOption.AllDirectories);

        foreach (System.IO.FileInfo fi in files)
        {
            string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
            routeCollection.MapPageRoute(fi.Name + "Route", fi.Name, pageName);
        }

        routeCollection.MapPageRoute("DummyRouteName1", "Dummy", "~/Default2.aspx");
    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>

Upvotes: 1

Views: 1770

Answers (2)

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

You can add routes in your Global.asax file on application start:

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}

private void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("DummyRouteName", "Dummy", "~/Default.aspx");
    ....
}

Usage:

Response.Redirect("~/Dummy");

In url you will see: (server)/Dummy

Edit:

here is how to automatically add routes:

// Get root directory
string root = Server.MapPath("~");
DirectoryInfo info = new DirectoryInfo(root);
// Get all aspx files
FileInfo[] files = info.GetFiles("*.aspx", SearchOption.AllDirectories);

foreach (FileInfo fi in files)
{
    // Get relative path
    string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
    // Add route
    routes.MapPageRoute(fi.Name + "Route", fi.Name.Replace(".aspx", ""), pageName);
}

Upvotes: 3

Antonio Bakula
Antonio Bakula

Reputation: 20693

I assume that you got that rewriting part covered and only problem is postback, you can set postback to "friendly" URL by seting form action, like this :

Page.Form.Action = Page.Request.RawUrl;

Upvotes: 0

Related Questions