Solved

C# Url routing /friendly URLs

Posted on 2014-03-14
8
531 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

863 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

28 Experts available now in Live!

Get 1:1 Help Now