Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.net MVC Urls with Routing

Options
  • 18-07-2010 5:50pm
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi,

    I'm trying to implement SEO friendly URLs in an ASP.net MVC 2 application.

    I am looking to get a URL like Blogs/Pagename using the Routing table, but when the link is generated, it is like Blogs/View?pageName=Test_Page.

    I have registered the routes like this:
    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    "Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );
    
                routes.MapRoute(
                   "ViewBlog",                                              // Route name
                   "{controller}/{pageName}",                           // URL with parameters
                   new { controller = "Blogs", action = "View", pageName = "" }  // Parameter defaults
                );
                routes.MapRoute(
                   "ÀddBlogComment",                                              // Route name
                   "{controller}/{action}",                           // URL with parameters
                   new { controller = "Blogs", action = "AddComment" }  // Parameter defaults
                );
    
            }
    

    I am creating the links using:
    <%= Html.ActionLink(b.Value.ToString(), "View", new { controller = "Blogs",pageName = b.Key.ToString() })%>
    

    Any ideas how I can fix this? I am led to believe from the documentation that it should be like this automatically. This is a new application that I have created in VS 2008 with MVC 2.

    Thanks in advance,
    John


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    I'm new to ASP.Net MVC2 myself, but the routes are done via pattern matching and the last two routes do the same thing, so perhaps it's a conflct?

    What happens if you manually construct the expected route in the address bar? Does it go to the desired page?


  • Closed Accounts Posts: 48 Ronan_


    RouteDebug should be able to help you with mapping the routes (will show you where you are going wrong anyway). It will list each of your routes and tell you which one's have passed / failed for each URL.

    AFAIK, The order you put your routes in is important too.. if two routes match the request, it takes the first one.

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


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    fasty wrote: »
    I'm new to ASP.Net MVC2 myself, but the routes are done via pattern matching and the last two routes do the same thing, so perhaps it's a conflct?

    What happens if you manually construct the expected route in the address bar? Does it go to the desired page?

    I have reworked the patterns so that they are unique, see below. If I enter the desired URL pattern then it does not work, the query string value is null.

    Thanks for the help.

    John


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Ronan_ wrote: »
    RouteDebug should be able to help you with mapping the routes (will show you where you are going wrong anyway). It will list each of your routes and tell you which one's have passed / failed for each URL.

    AFAIK, The order you put your routes in is important too.. if two routes match the request, it takes the first one.

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

    That helped me a lot, thanks very much :)


Advertisement