Solved

C# Url routing /friendly URLs

Posted on 2014-03-14
8
538 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
ID: 39930803
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
ID: 39931154
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
ID: 39931291
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39931293
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
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39931303
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
ID: 39931364
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
ID: 39931370
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
ID: 39931392
You could write custom routing handler but how would you differentiate somecategory and someproduct .
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

830 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