I have used URL rewriting in my ASP.NET 4.0 project to achieve the URL routing. Here is a sample code from my Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("main", "Main", "~/Home.aspx");
routes.MapPageRoute("aboutUs", "About-Us", "~/AboutUs.aspx");
...//so on and so forth
}
I also added this to Application_BeginRequest to forward Home.aspx to Main in the same Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.AbsolutePath.Contains("Home.aspx"))
{
HttpContext.Current.Response.Redirect("~/Main");
}
}
This way, any request that tries to go to "Home.aspx" will be routed to "Main", which happens only in the first load. The routing works for all pages in my project.
Now, when I publish my code to my website and launch the website, this is what the URL comes up:
www.MyWebSite.com/Main
How can I hide "Main" at the end of the URL and make it look like this:
www.MyWebSite.com
~ Thanks
在Web服务器中将Home.aspx配置为“默认页面”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.