Solved

C# Url routing /friendly URLs

Posted on 2014-03-14
8
522 Views
Last Modified: 2014-03-21
Hi Experts!
I am trying to make a webapplication and I would like to create friendly url's.

The idea is to have some urls at the lowest level ex. localhost:12345/samsung-phones
while other urls goes to a category part of my site, ex. localhost:12345/Category/new-phones
and a third types which points directly to a product, but also on root level, ex .localhost:12345/model-x

I have created the following routes, but it seams like there is problems with the 1 first route and the third route, and I am not able to see why.

The routes are generated from a database, and the structure of the db table is as follows.

id,      Menutitle      Friendlyurl      Sitefolder      webpage
1      Samsung Phones      samsung-phones      Category      c.aspx
2      New phones      new-phones      Category      c.aspx      
3      Model X            model-x            Product            p.aspx


This is my url routes

routes.MapPageRoute("samsung-phones", "{id}", "~/Category/c.aspx");
routes.MapPageRoute("new-phones", "Category/{id}", "~/Category/c.aspx");
routes.MapPageRoute("model-x", "{id}", "~/Product/p.aspx");



This is the htmlcode creating the links
<a href="localhost:12345/samsung-phones">Samsung phones</a>
<a href="localhost:12345/Category/new-phones">New Phones</a>
<a href="localhost:12345/model-x">Model X</a>

Is there any one who can see what I am doing wrong?
0
Comment
Question by:gfk76
  • 3
  • 3
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
What errors are you getting?

For this MVC web application, how is the route mapped to a controller?  

In this article, there are some examples of routes:

ASP.NET Routing
http://msdn.microsoft.com/en-us/library/cc668201.aspx

{controller}/{action}/{id}
http://server/application/Products/show/beverages
/Products/show/beverages
0
 

Author Comment

by:gfk76
Comment Utility
Thank you for taking time to look at this.

From these 3 routes

routes.MapPageRoute("samsung-phones", "{id}", "~/Category/c.aspx");
routes.MapPageRoute("new-phones", "Category/{id}", "~/Category/c.aspx");
routes.MapPageRoute("model-x", "{id}", "~/Product/p.aspx");

the 2 first routes Works fine, but the third is routed to ~/Category/c.aspx

If i change the order,to

routes.MapPageRoute("model-x", "{id}", "~/Product/p.aspx");
routes.MapPageRoute("new-phones", "Category/{id}", "~/Category/c.aspx");
routes.MapPageRoute("samsung-phones", "{id}", "~/Category/c.aspx");

The first one still Works but the Third is routed to ~/Product/p.aspx
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Oh, I misunderstood before.

You are using this form:

RouteCollection.MapPageRoute Method (String, String, String)
http://msdn.microsoft.com/en-us/library/dd992955.aspx

public Route MapPageRoute(
      string routeName,
      string routeUrl,
      string physicalFile
)

These two map to the same URL:

routes.MapPageRoute("model-x", "{id}", "~/Product/p.aspx");
routes.MapPageRoute("samsung-phones", "{id}", "~/Category/c.aspx");

You would need to find a way to make those different.
0
 
LVL 16

Expert Comment

by:SriVaddadi
Comment Utility
That's 'coz first one and third one are the same routes in terms of their formation though the route names are different.

First route matches any url that is in the form of localhost/{id}.  Change your routes as below

routes.MapPageRoute("new-phones", "Category/{id}", "~/Category/c.aspx");
routes.MapPageRoute("model-x", "product/{id}", "~/Product/p.aspx");

Pls note URL should be formatted accordingly
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:SriVaddadi
Comment Utility
In this case, your routes should always start with "SiteFolder" (Your database column) so that no two routes conflict.

In addition, the routes starting with same "SiteFolder" should differ in number of parameters.

This way you can make sure all your routes are unique.
0
 

Author Comment

by:gfk76
Comment Utility
Thank you both of you for youre inputs.

Does that mean that it is not possible to create multiple routes like http://localhost/someproduct and http://localhost/somecategory with MapPageRouting even tough the routes are unique?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
Comment Utility
You can map them as long the URLs are unique, but {id} can't be used for two URLs, you would need a string to differentiate them.
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 250 total points
Comment Utility
You could write custom routing handler but how would you differentiate somecategory and someproduct .
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now