Solved

C# Url routing /friendly URLs

Posted on 2014-03-14
8
536 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
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…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
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.

777 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