Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Question about custom routing in MVC

    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;

    Code:
    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;

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

    And a view which contains the code;

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

    But when I try to go to;

    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

  • #2
    New Coder
    Join Date
    Feb 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Turns out the problem was being caused by me including "controller" in the route specification, it should have been;
    Code:
    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
        );
    
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •