...

View Full Version : Question about custom routing in MVC



Satal Keto
10-03-2010, 11:20 PM
Hi all,

I have to admit in advance that I'm quite new to MVC, I've been going through the resources at www.asp.net/mvc, but I was wondering if you guys could help me with something.

I have been asked to create a ASP.NET version of an existing PHP website, this website has a huge number of existing links to it in a particular format, which I have to replicate in due to the amount of work to change all the existing links would be far too much.

The format of the existing links is;


/([A-Za-z0-9]{14})/([A-Za-z0-9_-]*)


My attempt at creating a custom route doesn't appear to be working. What I have done is change the RegisterRoutes method in Global.asax.cs file to be;


public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"ExistingLink",
"{LinkId}/{Title}",
new {controller="ExistingLinkController", action="Index"},
new {LinkId = @"([A-Za-z0-9]{14})", Title = @"([A-Za-z0-9_-]*)"});

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

}


I have also created the 'ExistingLinkController' with an 'Index' action of;


public ActionResult Index(string LinkId, string Title)
{
ViewData["LinkId"] = LinkId;
ViewData["Title"] = Title;
return View();
}


And a view which contains the code;


<h2>LinkId: <%: ViewData["LinkId"] %>
</h2>Title: <%: ViewData["Title"] %></h2>


But when I try to go to;


http://localhost:53499/55a3ef90c4b709/This-is-just-a-test_0-9

I get the following error;


Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /55a3ef90c4b709/This-is-just-a-test_0-9

I was wondering whether anyone can see what I am doing wrong and possibly point me in the right direction, perhaps pointing at the bit of code that's wrong if its a simple problem or pointing me to a article that will help me get a better understanding if I've got the wrong end of the stick with this routing stuff.

Thanks for any help in advance

Satal :D

Satal Keto
10-04-2010, 12:57 AM
Turns out the problem was being caused by me including "controller" in the route specification, it should have been;

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"ExistingLink",
"{LinkId}/{Title}",
new {controller="ExistingLink", action="Index"},
new {LinkId = @"([A-Za-z0-9]{14})", Title = @"([A-Za-z0-9_-]*)"});

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum