views:

46

answers:

2

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;

/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

+3  A: 

I think this:

 new {controller="ExistingLinkController", action="Index"},

Should just be this:

 new {controller="ExistingLink", action="Index"},

MVC adds the Controller part of the name itself - In the second route the controller is also called HomeController, but you only enter "Home" as the default for the controller argument.

AHM
Perfect thank you, I knew it would probably be something simple that I couldn't see due to lack of experience :D
Satal
+2  A: 

when i get stuck with my routes i use the route debugger from Phil Hack

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

its just epic

dbones