views:

56

answers:

2

For some reason my application isn't routing to my controller method correctly. I have a routelink like this in my webpage -

<%= Html.RouteLink("View", "Blog", new { id=(item.BlogId), slug=(item.Slug) }) %> 

In global.asax.cs I have the following routes -

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "MoreBlogs",
        "Blog/Page/{page}",
        new { controller = "Blog", action = "Index" }
    );

    routes.MapRoute(
        "Blog",
        "Blog/View/{id}/{slug}",
        new { controller = "Blog", action = "View"}
    );

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

And then I have a class BlogController that has a method -

   public ActionResult View(int id, string slug)
    {

       ... etc.
    }

I put a breakpoint in the first line of the View method but it's not getting hit at all. I checked with a route debugger for the format localhost/Blog/View/1/test and it matched my custom route. All I'm getting is a 404 while running this, I can't work out why the route won't post to the view method in my controller - any ideas?

A: 

(Assuming the route debugger was based on Phil Haack's debugger post)

If you're getting a 404, that makes me think the actual view page itself cannot be found.

Nick DeVore
Yes it was. If it was the view itself not getting found, shouldn't I have hit the breakpoint in the view method?
mcfroob
I found a workaround. I'd updated my database tables with the "slug" field, and brought them into my project again (I'm using Linq to SQL). For some reason the links wouldn't work whenever I tried to setup an ActionLink using item.Slug, however if I use the Title property it works correctly. Now I've moved the Slug into a property in a partial class that extends my Linq to SQL object and am doing slug generation in the getter, and things work fine. Not sure if it's a bug or what, I had cleaning and rebuilt my project etc. The links worked if I manually typed them, just not from an ActionLink.
mcfroob
A: 

Assuming that you are also using areas within the application, and that RouteLink is actually being called from within an Area, I think you may need to specify area="" in your routeValues object ( you also need to specify the controller). In general, I think that you will need to add the area="..." part with all your Routelink calls when using Areas.

This is something that I have picked up about RouteLink and Area, but cant seem to find any reference material detailing the limitations.

Ahmad