c# - Dynamic Routing in asp.net webforms -
can add dynamically routes global.asax
file
suppose if have multiple routes same page example
while actual url page http://website.com/en/about-us
.
my question is: there way can dynamically define these routes in global.asax
file in such way reads url entered users http://website.com/about
, compares database table , redirects correct page http://website.com/en/about-us
?
taking consideration following table structure:
id url_name url actual_url page_handler 1 home http://website.com/ http://website.com/ default.aspx 2 http://website.com/about http://website.com/en/about-us about.aspx 3 http://website.com/about-us http://website.com/en/about-us about.aspx 4 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 have configure each route manually in 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 example or solution highly appreciated.
i find routes in global.asax great static resources if want nice, sexy extensionless urls seo.
for dynamic pages/urls though, tend have catch route handles request if doesn't match 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 route routes.mappageroute( "all pages", "{*requestedpage}", "~/assemblerpage.aspx", true, new system.web.routing.routevaluedictionary { { "requestedpage", "home" } } );
so, when request comes in checks each static route in turn , executes specified page. if no match found, drops through catch , assemblerpage.aspx handles request. assemblerpage analyse requested url , redirect, rewrite path or stick controls on page render - basically, can whatever want do.
in case, i'd have assemblerpage check db , compare requested url urls in table. redirect or rewrite path.
Comments
Post a Comment