Search code examples
asp.net-mvcroutesactionlink

ASP.Net MVC Outbound Route Matching Problem When Using ActionLink


Hoping for some help after reading into MVC routing and not coming up with the answer myself.

I have the following routes registered:

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

        routes.MapRoute(
            null,
            "YourFeedback/Article/{resourceId}",
            new { controller = "YourFeedback", action = "Index", contentTypeId = new Guid(ConfigurationManager.AppSettings["ArticleLibraryId"]) });

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

I have the following ActionLink in an aspx view:

<%=Html.ActionLink("Your Feedback", "Article", "YourFeedback", new
 {
     resourceId = Model.ContentId.ResourceId
 }, new { @class = "yourFeedback" })%>

My understanding of MVC routing is that this would render a anchor link with href of "/YourFeedback/Article/101" where 101 comes from Model.ContentId.ResourceId.

Yet the anchor link href is rendered as "YourFeedback/Article/resourceId=101".

Any ideas where I'm going wrong?

Thanks in advance.


Solution

  • This is because your actionlink will match the second route and not the first. The reason is that you have some strange default values in your first route. You have set the controller to "YourFeedback" and the action to "Index". That means that you will have to set that in your actionlink as well if you want to match that route.

    To match the route you will have to use this actionlink:

    <%=Html.ActionLink("Your Feedback", "Index", "YourFeedback", new
    {
        resourceId = Model.ContentId.ResourceId
    }, new { @class = "yourFeedback" })%>
    

    Or change the route.