ASP.NET - URL routing issue asp.net 4.0

Asked By Tridip Bhattacharjee on 19-Nov-11 05:53 AM
i have small site which i am testing in my local machine.
 
my file structure is like
 
index.aspx
DE/index.aspx
US/index.aspx
UK/index.aspx
 
my routing code
 
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("default", "home", "~/default.aspx");
RouteTable.Routes.MapPageRoute("DE_Home", "DE", "~/DE/Index.aspx");
}
 
i want that when i will type url like http://localhost:7745/home then my root index.aspx should load. for this i route like below
 
RouteTable.Routes.MapPageRoute("default", "home", "~/default.aspx");
 
it is working fine.
 
i want that when i will type url like http://localhost:7745/DE then root/DE/Index.aspx should load but in this case my routing is not working. i want to load index.aspx fine in DE folder which is in root folder. i think there is some problem in my routing for that
 
what i need to change in the routing like
 
RouteTable.Routes.MapPageRoute("DE_Home", "DE", "~/DE/Index.aspx");
 
please guide me what should i write that when i will type http://localhost:7745/DE or http://localhost:7745/de or http://localhost:7745/De then my index.aspx file should load from de folder......thanks
Suchit shah replied to Tridip Bhattacharjee on 19-Nov-11 06:03 AM
I think for that u need to do the Chnage in to the Global.asax file like below one

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute(


"{resource}.axd/{*pathInfo}");

routes.MapRoute(



"Default", // Route name



"{controller}/{action}/{id}", // URL with parameters



new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

);

}

dipa ahuja replied to Tridip Bhattacharjee on 19-Nov-11 06:12 AM

Add The Gloabal.asax File by right click on website-> Add new item -> Global.asax

 

and add the below code inside Application_BeginRequest Event

 

void Application_BeginRequest(object sender, EventArgs e)

{

   string GetUrl = Request.Url.ToString();

  if (GetUrl.Contains("/Home-page") == true)

   {

     Context.RewritePath("home.aspx");

   }

  else if (GetUrl.Contains("/Default") == true)

   {

     Context.RewritePath("Default.aspx");

   }

}

 

.CS in aspx file

 

protected void Button1_Click(object sender, EventArgs e)

{

   Response.Redirect("Default");//redirect to default.aspx

}

http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

 

http://www.eggheadcafe.com/tutorials/aspnet/6592d2d4-bbf4-4ecd-93df-52898c6aa5d7/iis-70-extensionless-urlrewriting-short-urls.aspx

 

Hope you Get it...!