简体   繁体   中英

Dynamic Routing in asp.net webforms

Can we add dynamically routes to global.asax file

Suppose if I have multiple routes for the same page for example

While my actual URL for the page is like http://website.com/en/about-us .

My question now is: is there a way I can dynamically define these routes in global.asax file in such a way that it reads the URL entered by users like http://website.com/about and then compares it with database table and redirects it to the correct page which is http://website.com/en/about-us ?

Taking into consideration following Table Structure:

Id  URL_Name    URL                                 Actual_URL                              Page_Handler
1   Home        http://website.com/                 http://website.com/                     Default.aspx
2   About Us    http://website.com/about            http://website.com/en/about-us          About.aspx
3   About Us    http://website.com/about-us         http://website.com/en/about-us          About.aspx
4   About Us    http://website.com/en/about         http://website.com/en/about-us          About.aspx
5   Contact     http://website.com/contact          http://website.com/en/contact-us        Contact.aspx
6   Contact     http://website.com/en/contact       http://website.com/en/contact-us        Contact.aspx

Right now I have to configure each route manually in the global.asax :

        if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/about")
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
        }


        if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/en/about")
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
        }

A pointer to a good example or a solution is highly appreciated.

I find routes in global.asax are great for static resources especially if you want a nice, sexy extensionless URLs for SEO.

For dynamic pages/URLs though, I tend to have a catch all route that handles the request if it doesn't match any static routes.

eg

    // ignore
    routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));

    // sexy static routes
    routes.MapPageRoute("some-page", "some-sexy-url", "~/some/rubbish/path/page.aspx", true);

    // catch all route
    routes.MapPageRoute(
       "All Pages",
       "{*RequestedPage}",
       "~/AssemblerPage.aspx",
       true,
       new System.Web.Routing.RouteValueDictionary { { "RequestedPage", "home" } }
    );

So, when a request comes in it checks each static route in turn and executes the specified page. If no match is found, it drops through to the catch all and then AssemblerPage.aspx handles the request. This AssemblerPage will analyse the requested URL and redirect, rewrite path or stick some controls on the page to render - basically, it can do whatever you want it to do.

In your case, I'd have the AssemblerPage check the DB and compare the requested URL with the URLs in your table. Then simply redirect or rewrite path.

Would ASP.NET Routing help you here ?

Have a look at this: - ASP.net URL rewrite based off query string ID

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM