0

I have a MVC 5 app in ASP.Net targeting .Net 4.5 that has a mix of views(.cshtml) and webforms (.aspx). The app compiles successfully and even renders the starting view in browser. However, a link generated by Html.ActionLink in starting view is using the first route in the routing table when it should be using the third route. I am confused why Html.ActionLink uses the first route.

I have two webforms called WebForm1.aspx and Webform2.aspx. Each of these webforms have a route assigned.

Question

Why is Html.ActionLink using the first route in my routings table and not the third route?

My Routings Table

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
       //routes for webforms below
       routes.MapPageRoute("webform1", "routeexample", "~/WebForm1.aspx", true);
       routes.MapPageRoute("webform2", "routeexample2", "~/WebForm2.aspx", true);

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

        //routes for MVC views below
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Markup for ActionLink

<li>@Html.ActionLink("Home", "Index", "Home")</li>

Generated output by above markup

https://localhost:44394/routeexample?action=Index&controller=Home

Expected output of above markup using the third route from routings table

https://localhost:44394 OR https://localhost:44394/Home/Index

Sunil
  • 20,653
  • 28
  • 112
  • 197
  • `Why is Html.ActionLink using the first route in my routings table and not the third route?` - because it is supposed to. It will use the first route, in order of declaration, that matches the requested parameters. – GSerg Jun 14 '20 at 07:11
  • 1
    @GSerg, Could you please explain how the first route gets matched? There is no action or controller specified for first route. – Sunil Jun 14 '20 at 07:13
  • Possible duplicate of [MVC MapPageRoute and ActionLink](https://stackoverflow.com/q/4441222/11683) – GSerg Jun 14 '20 at 07:16
  • @GSerg, I looked at the duplicate posts, but I am still am not clear why first route is getting matched by Html.ActionLink. The duplicate posts are not explaining this, rather they are providing a solution to getting around this issue. I just want to know why first route is matched. I am thinking when no action or controller is specified for a route then it means that a route's action or controller could be anything and so the first route will be matched for any values of controller and action. – Sunil Jun 14 '20 at 07:25
  • It matches because there isn't a reason for it to not match (which is why the solutions are about giving it a reason to not match). "Action" and "Controller" (and also "Area" if you are using areas) are mere URL parameters. It happens so that a normal MVC route displays them as parts of the URL path, but it's legal to display them just as parameters, as far as the URL generation logic is concerned. – GSerg Jun 14 '20 at 07:32
  • 1
    It won't work the other way - that is, if you have an MVC app without any webforms and just one default route of `{controller}/{action}/{id}`, and you try to access `?action=Index&controller=Home`, it won't take the controller and action from the parameters. But as far as the generation of the URL is concerned, this is legal, and when generating an URL, the framework looks for the first rule in the order of declaration for which generating a URL is legal (as opposed to makes sense). – GSerg Jun 14 '20 at 07:37
  • @GSerg, Ok that helps. So, the first route alllows any action or controller as I had thought in my last comment. Also, I noticed that if I add a fourth RouteValueDictionary type parameter to webforms MapPageRoute so the controller=null, action=null then the webform routes do not get matched. – Sunil Jun 14 '20 at 07:39
  • I added rvd to MapPageRoute as a fourth parameter and then it worked. `RouteValueDictionary rvd = new RouteValueDictionary(); rvd.Add("controller", null); rvd.Add("action", null);` Not sure why ActionLink now doesn't match the first route after introduction of this parameter. – Sunil Jun 14 '20 at 07:42
  • 1
    I'm not immediately sure either. Another solution btw: https://stackoverflow.com/q/10178276/11683 – GSerg Jun 14 '20 at 07:55

0 Answers0