Solved

C# Url routing /friendly URLs

Posted on 2014-03-14
8
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

752 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