Reputation: 20001
I am using ASP.Net web form Routing for my website but i want to make it more structure & hide all the Querystring ID's with proper Structure like Language/Category/PageName-Title
example: www.abc.com/en/sports/cricket-world-cup
but with my current routing technique i am able to make it work as
www.abc.com/en/1/13/cricket-world-cup
domain/english-folder/language-id/page-id/page-title
How can i make it more structured as `www.abc.com/en/sports/cricket-world-cup
Since i have few Query-string i designed it this way
//For Page.aspx
routes.MapPageRoute("Page_Route", "en/page/{LID}/{PID}/{PageName}", "~/en/Page.aspx", false,
new RouteValueDictionary {
{ "LID", "0"},
{ "PID", "0" },
{ "PageName", "Page-not-found" }},
new RouteValueDictionary {
{ "LID", "[0-9]{1,8}" },
{ "PID", "[0-9]{1,8}" },
});
Result of above Routing get me Friendly URL like this
www.abc.com/en/1/13/cricket-world-cup
But i want to further structure the URL like the one in example.
www.abc.com/en/sports/cricket-world-cup
Example: This url i found is more structure/
http://www.thenational.ae/news/world/europe/turkey-providing-jobs-a-future-for-more-greeks
How can i implement the same structure are they passing querystrings
as hiddenfields. Please suggest best approach for such url.
another example is http://mashreqbank.com/uae/en/corporate/lending/trade-finance.aspx
they are using asp.net but i am not sure if it is ASP.Net MVC or ASP.Net webform.
I have been struggling with this kind of routing for quite long and even i cant find a complete example which can take into consideration more than one query-string as most of the example are based on one query-string.
Any help from coding gurus with example would be highly appreciated.
UPDATE: Let us take this Table into consideration which i use to store page name, title, handler, language regions etc.. This is just a demo table & doesnt resemble the actual website which i am refering to for sample structured url. I am not even sure if they are going it using URL routing or these are actual physical folder & files like old style website where you create actual folder and place files in their etc..
Page_ID Page_Name Page_Title Page_Handler Parent_Page_ID Language_ID Region_ID
1 Home Home index.aspx 0 1 uae
2 Personal Personal index.aspx 0 1 uae
3 Accounts & Deposits Accounts & Deposits index.aspx 2 1 uae
4 Current Account Current Account current-account.aspx 3 1 uae
5 Current Gold Accounts gold Account gold-account.aspx 3 1 uae
6 Easy Saver Easy Saver Account saver-account.aspx 3 1 uae
7 Fixed Deposits Fixed Account fixed-account.aspx 3 1 uae
8 Loans Loans index.aspx 2 1 uae
9 Personal Loans Personal Loans index.aspx 8 1 uae
10 car Loans car Loans car-loan.aspx 8 1 uae
website example for above structure http://mashreqbank.com/
We can use a common page handler if the page design is same this is what i am assuming let us say page.aspx
,
Pleae feel to make changes to structure and data inorder to achieve the desired result.
Upvotes: 2
Views: 9793
Reputation: 15797
You could simply have something like:
routes.MapPageRoute(
"article-route",
"en/{category}/{pageName}",
"~/en/page.aspx");
And then on en/page.aspx
you can access the category and pageName, assuming you have a query to find the correct article based on those two variables:
string category = Page.RouteData.Values["category"] as string;
string pageName = Page.RouteData.Values["pageName"] as string;
Though, if you can't identify a page simply by category
and pageName
(which seems true per your updated question), you may need the id
in the route. In this case, you can ignore the category
and pageName
route values as they are only there for a nice-looking URL. The only parameter we care about is the id
since that is how you identify an article. For example, here are three examples of various routes
routes.MapPageRoute(
"uae-route",
"uae/en/{category}/{id}/{pageName}",
"~/en/index.aspx");
routes.MapPageRoute(
"gbr-route",
"gbr/en/{category}/{id}/{pageName}",
"~/en/index.aspx");
routes.MapPageRoute(
"account-route",
"en/{id}/{pageName}",
"~/en/current-account.aspx");
//then on en/index.aspx and current-account.aspx
int pageId= 0;
if (Int32.TryParse(Page.RouteData.Values["id"] as string, out pageId)) {
// get the page details (including parent page id and language id if needed)
// where Page_ID=pageId.
}
From the sounds of it though, you want to do the first example above, meaning you'll need a way to pull articles by simply category
and pageName
and not any type of id
.
UPDATE: You don't need to create a route for each path... that's the whole point of routing. If you have multiple handlers, then you'll at least need a path for each handler (.aspx
page).
It'd be easiest to use an id
in the URL, because according to your data structure, that is the only way to identify the page you want to pull. So let's use your example, using these routes:
www.abc.com/en/personal
www.abc.com/en/personal/acounts-deposits/
www.abc.com/en/personal/acounts-deposits/current-account
www.abc.com/en/personal/acounts-deposits/current-gold-account
www.abc.com/en/personal/acounts-deposits/easy-saver
You have one route:
routes.MapPageRoute(
"personal_route",
"en/personal/{category}/{pageName}",
"~/personal.aspx",
false,
new RouteValueDictionary { { "category", string.Empty }, {"pageName", string.Empty}}); //allow for no values
And the personal.aspx
has the following code:
protected void Page_Load(object sender, EventArgs e)
{
string category = Page.RouteData.Values["category"] as string;
string pageName = Page.RouteData.Values["pageName"] as string;
if (String.IsNullOrEmpty(category)) {
//no category... must be the /personal route. handle that
}
else if (String.IsNullOrEmpty(pageName)) {
//no page name, just load the category page content
//Psuedo query against your non-existant data schema
//SELECT * FROM SiteContent WHERE Page_Handler='Personal' AND Page_Category=category && Page_URL=""
}
else {
//SELECT * FROM SiteContent WHERE Page_Handler='Personal' AND Page_Category=category && Page_URL=pageName
}
}
As you can see, your problem is you have no way to identify pages without the IDs. You'd need to replace all those IDs in your table with unique URLs. It's doable.
Upvotes: 3