Asp.net 4.0(C#) use domain name or pseudo-static to visit homepage with route

Lionsure 2020-08-22 Original by the website

.net 4.0 adds a new address rewriting feature, which rewrites Url through route to achieve pseudo-static. To use this method, the address to be rewritten needs to be registered in the Routetable, and the registration needs to be completed in the Global file.

Using the route feature in .net 4.0(C#), you can rewrite the address of each page of website. As for the address to be rewritten, you can decide by yourself. It can be rewritten as a pseudo-static Url with an html extension or without an extension. If you want to rewrite the homepage to be accessible only by domain name or pseudo-static(not accessible by default.aspx or index.aspx), you only need to add its path to the Routetable.

Achieve address rewriting with route is mainly implemented using the MapPageRoute() method in .net 4.0(C#), and you only need to add the web page to be rewritten to the Routetable through this method. The following are two examples of using route to access the homepage of website using only domain name and pseudo-static(index.htm).

 

1. Use route to access the homepage of website using only domain name in .net 4.0(C#)

Just add the path of website homepage to the Routetable in the Global file, the specific code is as follows:

using System;
       using System.Web;
       using System.Web.SessionState;
       using System.Web.Routing;

namespace RouteEg
       {
              public class Global : System.Web.HttpApplication
              {
                     protected void Application_Start(object MapPageRoute, EventArgs e)
                     {
                            RegisterRoutes(RouteTable.Routes);
                     }

              public void RegisterRoutes(RouteCollection routes)
                     {
                            routes.MapPageRoute("homepage", "", "~/default.aspx");
                     }
              }
       }

For the MapPageRoute() method, the first argument is routeName, the second is routeUrl, and the third is physicalFile(the physical path to be rewritten, such as the website homepage ~/default.aspx). It should be noted that: rewriting the homepage can only be accessed by domain name, and the second argument can be left blank.

 

2. Use route to access the homepage of website using only pseudo-static(index.htm) in .net 4.0(C#)(Aspx how to launch html page)

The method is the same as the domain name access only, except that the second argument cannot be left blank and index.html is used. In addition, you need to add <modules runAllManagedModulesForAllRequests="true" /> to the website configuration file(Web.config) , The complete code is as follows:

using System;
       using System.Web;
       using System.Web.SessionState;
       using System.Web.Routing;

namespace RouteEg
       {
              public class Global : System.Web.HttpApplication
              {
                     protected void Application_Start(object MapPageRoute, EventArgs e)
                     {
                            RegisterRoutes(RouteTable.Routes); 
                     }

              public void RegisterRoutes(RouteCollection routes)
                     {
                            routes.MapPageRoute("homepage", "index.html", "~/default.aspx");
                     }
              }
       }

Add in the website configuration file(Web.config):

<system.webServer>
              <modules runAllManagedModulesForAllRequests="true" />
       </system.webServer>